Rails error while installing nokogiri with native extensions

Installing nokogiri 1.10.3 with native extensions
Errno::EACCES: Permission denied @ rb_sysopen – /Users/raghu/workspace/fleet/vendor/bundle/ruby/2.5.0/gems/nokogiri-1.10.3/LICENSE-DEPENDENCIES.md
An error occurred while installing nokogiri (1.10.3), and Bundler cannot continue.
Make sure that `gem install nokogiri -v ‘1.10.3’ –source ‘https://rubygems.org/’` succeeds before bundling.

 

the solution that worked for me is to run it with sudo bundle

Java pass by value explained

Hey friends!

pass by value means the actual value is passed on by creating a copy of it.

Pass by reference means that a number (called a memory address) is passed on, this address defines where the actual value is stored.

I will try to explain why java is pass by value by taking an example class Person.

public class Person {
  private String name;
  private int age;
  
  Person(String name, int age){
    this.name = name;
    this.age = age;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  
}

Through out this post I will modify the person object in different scenarios and explain whats happening behind the scenes.

Initial state: Let’s create a person object with name `Tom Cruise` and age 57.

public class ReferenceTest {public static void main(String[] args) {
    Person person = new Person("Tom Cruise", 57);
    System.out.println(person.getName());
  }}

Output: Tom Cruise

Scenario 1:

Now let’s try to change the name of the person object.

  Person person = new Person("Tom Cruise", 57);
  modifyPerson(person);
  System.out.println(person.getName());
  
  private static void modifyPerson(Person person) {
    person.setName("Brad Pitt");
  }

Output: Brad Pitt

No surprises.

Scenario 2:

Let’s now modify the state of person. I will try to nullify the object.

//excluded other code for brevity
private static void modifyPerson(Person person2) {
 person2 = null;
}

Output: Tom Cruise

What ? Yes that’s because java passes a copy of reference (don’t get confused this with physical memory address reference)pointing to the actual object. So what ever you do to change the state of the reference the actual object remains untouched. The image will explain more. person is untouched as person2 is a reference copy of person.

Let’s do one last thing to confirm this.

//excluded other code for brevity
private static void modifyPerson(Person person2) {
 person2 = new Person("Johnny Depp", 56);
}

Output: Tom Cruise

Still no change. Look at the image to see whats happening.

person2 is pointing to a whole new object in the memory.

Same thing applies to primitive data types.

Last but not the least you can pass the reference of a variable by adding an & in front of the method argument, but we can just return the value instead of doing this.

Hope this helps!

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!

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.

Rails date validation pit falls

When ever you want to validate a date field against a time frame don’t forget to add context to it. For example I have the following class

Movie – name, release_date and collections

class Movie
  validates :release_date, presence: true
  validate :release_date_future?

  private

  def release_date_future?
    if release_date < Date.today
      errors.add(:release_date, "can't be in the past")
    end
  end
end

This Class looks absolutely legit. Let’s say I have a Movie object called Avengers endgame.

Movie.new(name: ‘Avengers endgame’, release_date: ’04-26-2019′).save

So far so good but when i want to update the `collections` at later point of time(after the movie got released) i can never do that because the Movie obj goes invalid (as it once again checks the release_date with current_date).

so the right thing to do is to add context to the date validation. Like we are interested in only while creation.

validate :release_date_future?, on: :create

Hope this helps.