Execute shell commands in Node script

If you are wondering how to execute commands in shell during a node script this is for you. We can achieve this using node’s child_process library.

declare a const for child_process

const exec = require('child_process').exec

Let’s say i want to execute a bunch of git commands like below

git config --global github.user testbot
git config --global user.name "test"
git config --global user.email bot@test.com

I also want to do something else when all of them succeeds. I wrapped a promise around exec and used Promise.all to achieve this.

Promise.all([
    execPromise('git config --global github.user testbot'),
    execPromise('git config --global user.name "test"),
    execPromise('git config --global user.email bot@test.com)
  ])
.then(console.log('git configured successfully'))
.catch(error => validationFailed(error))
function execPromise(cmd){
  console.log(cmd)
  return new Promise(function (resolve, reject) {
    exec(cmd, (error, stdout, stderr) => {
      if (error) {
        reject({cmd: cmd, message: error})
      } else{
        resolve(stdout)
      }
    })
  })
}
function validationFailed (error = {cmd: '', message: ''}) {
  console.warn(`Error occurred while ${error.cmd}:     ${error.message}`)
}

Happy coding!

Rails: Reinstall all gems again

I was messing with a gem in my ruby on rails and ended up deleting all the installed gems manually on my local machine while experimenting.

I tried to reinstall using `bundle` which did not help as it thinks that gems are already installed looking at the `Gemfile.lock` file.

After googling a little bit I found bundle install --force to forcefully reinstall all of them.

Happy coding!

Adding devise_token_auth to an existing Rails app

Ruby on rails is still the frontrunner for getting an MVP(minimum viable product) out of the door. Eventual when things go our way we want to introduce F/E framework on the customer facing side for various reasons(continue using the admin part as traditional rails app). If you are already using devise and wondering how to use it for api calls this post is for you.

The devise_token_auth is a gem on top of devise which works with tokens and leverages on devise. We can even configure it to either refresh the token for every single request or not.

Without further delay let’s jump in.

Add the following to your Gemfile. We need rack-cors to get support for Cross-Origin Resource Sharing (CORS).

gem ‘devise_token_auth’

gem rack-cors

Then install the gem using bundle:

bundle install

Assuming we are using User model run the following command to prepare for the setup.

rails g devise_token_auth:install User auth

This is going to generate the following files

create  config/initializers/devise_token_auth.rb
insert  app/controllers/application_controller.rb
gsub  config/routes.rb
create  db/migrate/20190712184641_devise_token_auth_create_users.rb
insert  app/models/user.rb

So far we have done exactly as mentioned in the docs which thinks there is no devise functionality already implemented.

As we want new token implementation to work on top of our existing admin side(traditional rails app using devise) do the following changes.

  • routes.rb: Move mount_devise_token_auth_for User, at: auth into api routes. As we only want this routes to be used in the context of api.
 mount_devise_token_auth_for 'User', at: 'auth'
namespace :api do
  namespace :v1 do
    mount_devise_token_auth_for 'User', at: 'auth', defaults: {format: 'json'}
  • application_controller.rb:                                                                                                Remove include DeviseTokenAuth::Concerns::SetUserByToken from application_controller.rb and move to your api base controller. Else this will mess up with your devise authentication.
class Api::V1::BaseController
  include DeviseTokenAuth::Concerns::SetUserByToken
  • Rename migration to xxx_devise_token_auth_update_users and make sure you remove all the existing devise module columns which you already have.
class DeviseTokenAuthCreateUsers < ActiveRecord::Migration[5.2]   def change     change_table(:users) do |t|       ## Required       t.string :provider, :null => false, :default => "email"
      t.string :uid, :null => false, :default => ""
      t.boolean  :allow_password_change, default: true

      ## Tokens
      t.json :tokens
    end
  end
end
  • User.rb: Remove the modules you already included. I don’t need omniauth at this moment so i removed it.
# Include default devise modules.
devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :trackable, :validatable,
        :confirmable
include DeviseTokenAuth::Concerns::User

Apart from the above i made the following changes to make it work.

class Api::V1::BaseController < JSONAPI::ResourceController
  include DeviseTokenAuth::Concerns::SetUserByToken

before_action :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_in, keys: [:email, :password])
end

In application_controller.rb

class ApplicationController < ActionController::Base

protect_from_forgery with: :exception
protect_from_forgery with: :null_session, if: -> { request.format.json? }

 wrap_parameters false

In development.rb

resource '*', headers: :any,
         expose: %w(etag access-token uid expiry token-type client Rate-Search-Uuid),
         methods: [:get, :post, :delete, :put, :patch, :options, :head]

Now you can send a curl command like below.

curl -X POST http://localhost:3000/api/v1/auth/sign_in
-H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data;
  -F email=email@company.com \
  -F password=password

The success response consists of access_token, uid, client from headers. Don’t forget to send these back and forth on the consecutive requests.

Drop comments if you are stuck or have any concerns.

Thanks!

How to downgrade a rails gem

This is for someone who wants to downgrade a rails gem. For example let’s take devise gem.

  1. In Gemlock file add the downgraded version you are interested in.
gem 'devise', '~> 2.5'

2. run bundle update devise

Important notes:

  1. Do not runbundle update as it updates all the gems.
  2. Never make changes directly to Gemfile.lock
  3. Make sure to restart spring if running in background.

Ruby singleton_class

Let’s learn about singleton_class with an example. I have a Car which knows to drive.

class Car
  def drive
    puts "I am driving"
  end
end

car = Car.new
car.drive # I am driving

Cool so what! Now at later point of time I want to add honk to my car only when it is stuck in traffic. I don’t want it to be added for every Car object.

There might be more than one way of doing this in ruby but in the interest of this post i will implement this with singleton_class.  Ruby singleton_class inserts a new anonymous class into the inheritance hierarchy as a container and adds the new method to it.

class Car
  def drive
    puts "I am driving"
  end
end

module Honking
  def honk
    puts "Get away on my way"
  end
end

car = Car.new

traffic = true # more logic here

if traffic
  car.singleton_class.include(Honking)  
end

car.honk

p car.singleton_methods # [:honk]

race_car = Car.new
race_car.honk # undefined method `honk'

singleton_class creates a new singleton class if car does not have one. From then what ever changes you make on that is going to be affected to that particular class. Thats the reason race_car doesn’t know anything about honk.

I also used singleton_classto add additional validations to the object in a specific context.

You can check singleton methods on a class using singleton_methods.

Happy coding!

Ruby mixins explained

Hi Devs,

In this post i will try to explain how to use ruby mixins for DRYing up your code. Let’s say I have Car which can drive and honk. It can find a car with vin number or return count.

class Car
  def self.find(vin)
    puts "return car with VIN number #{vin}"
  end

  def self.count
    puts "return number of cars"
  end
  def honk
    puts "I like to yell at people"
  end

  def drive
    puts "will try to avoid speed tickets"
  end
end

# A Car can drive, honk

car = Car.new
car.drive
car.honk

# Find car by its VIN number
Car.find("123")
# Get total number of cars
Car.count

Now we get a requirement to add Trucks to our inventory which do most of the things aCar does. Let’s start DRYing the code.

First I will extract instance methods using a module and use include to make them available. In ruby we use include to add instance level methods from a module.

module VehicleBehavior
  def honk
    puts "I like to yell at people"
  end

  def drive
    puts "will try to avoid speed tickets"
  end
end

class Car
  include VehicleBehavior

  def self.find(vin)
    puts "return car with VIN number #{vin}"
  end

  def self.count
    puts "return number of cars"
  end
end

Making progress. Now i will extract the class level methods into a module and use theextend keyword to make them available. In ruby we use extend to add class level methods from a module.

module VehicleBehavior
  def honk
    puts "I like to yell at people"
  end

  def drive
    puts "will try to avoid speed tickets"
  end
end

module VehicleData
  def self.find(vin)
    puts "return car with VIN number #{vin}"
  end

  def self.count
    puts "return number of cars"
  end
end

class Car
  include VehicleBehavior
  extend VehicleData
end

Good. But is there a way to combine both modules into one ? yes.

Every time a class includes module – Ruby will trigger the self.included method on that module. It will also pass class as a parameter. In our case the Car class will be the klass argument.

module Vehicle
  def self.included(klass)
    klass.extend(ClassMethods)
  end

  module ClassMethods
    def find(vin)
      puts "return car with VIN number #{vin}"
    end

    def count
      puts "return number of cars"
    end
  end

  def honk
    puts "I like to yell at people"
  end

  def drive
    puts "will try to avoid speed tickets"
  end
end

class Car
  include Vehicle
end

Looking good. But is there is a cleaner way to do this YesActiveSupport::Concern

require 'active_support/concern'

module Vehicle
  extend ActiveSupport::Concern

  def honk
    puts "I like to yell at people"
  end

  def drive
    puts "will try to avoid speed tickets"
  end

  class_methods do
    def find(vin)
      puts "return car with VIN number #{vin}"
    end

    def count
      puts "return number of cars"
    end
  end
end

class Car
  include Vehicle
end

car = Car.new

Car.find("blah")
car.drive
car.honk

ActiveSupport::Concern makes the syntax better and also has the advantage of  gracefully handling module dependencies.

Now our Truck class is as simple as this

class Truck
  include Vehicle
end

Pretty cool right? Less code! Less bugs!! YaY.