Thursday 30 March 2017

How to use Amazon DynamoDB with Ruby on Rails

In this blog we will discuss about Amazon DynamoDB and how to use it with Ruby on Rails application.
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.
NoSQL databases are designed for scale, but their architectures are sophisticated, and there can be significant operational overhead in running a large NoSQL cluster.
With DynamoDB, you can create database tables that can store and retrieve any amount of data, and serve any level of request traffic.
DynamoDB allows you to delete expired items from tables automatically to help you reduce storage usage and the cost of storing data that is no longer relevant.

Now, we will integrate it with Rails application. For this we can use aws-sdk gem.

So, in Gemfile
    gem 'aws-sdk', '~>2'


Run $ bundle

Now, we will configure Aws access. for this create config/initializers/aws.rb

In aws.rb
     Aws.config.update({
        region: 'us-west-2',
        credentials: Aws::Credentials.new(
'REPLACE_WITH_ACCESS_KEY_ID', 
'REPLACE_WITH_SECRET_ACCESS_KEY'),
      })



We can started by initializing a client object of DynamoDB by
    dynamodb = Aws::DynamoDB::Client.new


Now, we can create a table
    dynamodb.create_table({
        table_name: 'VenueOfferTable',
        attribute_definitions: [
          {attribute_name: 'Title',attribute_type:'S'},
          {attribute_name: 'LongDescription', attribute_type: 'S'}
        ],
        key_schema: [
          {attribute_name: 'Title', key_type: 'HASH'},
        ],
        provisioned_throughput: {
          read_capacity_units: 1,
          write_capacity_units: 1
        }
      })


Above code will create a table in DynamoDB.

Now we have a table so, we can add items by
    dynamodb.put_item({
      table_name: 'VenueOfferTable',
      item: {
        'Title' => 'Mill Valley Kitchen',
        'LongDescription' => 'Restaurant and Bar '
      }
    })


Update an item
    dynamodb.update_item({
        table_name: 'VenueOfferTable',
        key: {
          'Title' => 'Mill Valley Kitchen'
        },
        update_expression: 'SET LongDescription = :LongDescription',
        expression_attribute_values: {
          ':LongDescription' => 'Premium Restaurant and Bar'
        }
      })


Fetching the items from table
    response = dynamodb.get_item({
      table_name: 'VenueOfferTable',
      key: {
        'Title' => 'Mill Valley Kitchen'
      }
    })


We can scan full table. scan method can be used to perform a full table scan, which by default will return every item in a table

    response = dynamodb.scan(table_name: 'VenueOfferTable')

    items = response.items



There are many api's for querying to meet your requirement. You can get this from here.

Sunday 26 March 2017

Assignment of Ruby object_id

As we know that in ruby, nearly everything is an object and an object id is a unique identifier for that object. It is much like a fingerprint. All objects have an object id. In Ruby, the method .object_id returns an object’s unique identifier, which is presented by a number or a string of numbers.

For performance reasons true, false, nil and Fixnums are handled specially in ruby. For these objects there is a fixed value of object_id.

Let's check it on irb.

    2.3.0 :001 > false.object_id
     => 0
    2.3.0 :002 > true.object_id
     => 20
    2.3.0 :003 > nil.object_id
     => 8

Note that here we are using ruby 2.3.0. Value of true and nil object has changed from ruby 2.0.0. Before it is for true is 2 and for nil is 4.
You can get why it is changed from this link.

Now, what about fixnums?

Fixnums is in ruby also handled specially. Object id assignment to a fixnums is working on i*2+1.

    2.3.0 :004 > 1.object_id
     => 3
    2.3.0 :005 > 2.object_id
     => 5
    2.3.0 :006 > 3.object_id
     => 7

But, how it's works?

Ruby doesn't use the VALUE as a pointer in every instance. For Fixnums, Ruby stores the number value directly in the VALUE itself. That keeps us from having to keep a lookup table of every possible Fixnum in the system.

Actually, Fixnums are stored in the upper 31 bits, which means a shift left of one. Ruby use the lowest bit to mark that it is a Fixnum.

So, for example when we convert fixnum 4 into it's binary number. It will be 100.

Now, shift it to left and mark it is a fixnum. it will become now 1001. You can see that it is binary value of 9.

From above points, we can conclude that object_id of any other object like string or symbol always will be an even object_id.

Thursday 16 March 2017

How to verify facebook token and gmail acceess token and fetch user info.

Suppose that we didn't have facebook app_id, app_secret or gmail client_id and client_secret but we have to verify the provided token.

In this blog, we will verify token with using open graph api's of facebook. For sending a request we will use HTTParty.

Now in controller,

For Facebook -


    def verify_facebook_token
      token = params[:token]   
      response = HTTParty.get("https://graph.facebook.co/me?fields=email,name&access_token=#{token}")
          if response.code == 200
            user_data = JSON.parse(response.body)
      end       
    end

For Gmail -


    def verify_gmail_token
      token = params[:token]   
      response = HTTParty.get("https://www.googleapis.com/oauth2/v2/userinfo", headers: {"Access_token" => token, "Authorization" => "OAuth #{token}"})
          if response.code == 200
            user_data = JSON.parse(response.body)
      end       
    end


Above method return result like this -

    {"email"=>"er.sonukr@gmail.com", "name"=>"Sonu", "id"=>"416806898668956"}

Note - Gmail provides three types of token. i.e. id_token, access_token, refresh_token. We have to use access_token for userinfo api's.

Sunday 12 March 2017

How to make a class method and callback in ruby module



1) Callback in module -

Let's assume we have two model Provider and Consumer. Both model have same callback of before_create :generate_refresh_token. So, in this case we can make a module.

Let's create a module in models/concerns/token_generator.rb

   
    module TokenGenerator

      extend ActiveSupport::Concern

      included do
        before_create :generate_refresh_token
      end

   
      def generate_token
        SecureRandom.urlsafe_base64
      end           
   
      private

      def generate_refresh_token
        if self.refresh_token.blank?
          begin
        self.refresh_token = generate_token
          end while self.class.exists?(refresh_token: self.refresh_token)
        end
      end

    end


Now, in include above module in models Consumer.rb and Provider.rb

    class Consumer < ApplicationRecord

      #==== Modules ==================
      include TokenGenerator

    end


    class Provider < ApplicationRecord

      #==== Modules ==================
      include TokenGenerator

    end


2) Class method in module

Now, suppose that we have same class method of regenerate_refresh_token in both models. From this method we want to find the object and regenerate refresh token and update the same attribute.


So, in models/concerns/token_generator.rb


      def self.included(resource)
        def resource.regenerate_refresh_token(refresh_token)
          resource = self.where(refresh_token: refresh_token).take
          if resource
        resource.update_attributes(refresh_token: resource.generate_token)
        resource
          end
        end
      end


Now we can call above method as a class method like

        Consumer.regenerate_refresh_token

            OR

        Provider.regenerate_refresh_token           

Thursday 2 March 2017

How to use Docker

In this blog, We are going to explain the concepts of Docker and integrate it with Rails app.

Let's first understand what is Docker ?

    Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they're running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.

    Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.

Now, You are thinking what are containers?

    Operating system (OS) virtualization has grown in popularity over the last decade as a means to enable software to run predictably and well when moved from one server environment to another. Containers provide a way to run these isolated systems on a single server/host OS.

    Containers sit on top of a physical server and its host OS, e.g. Linux or Windows. Each container shares the host OS kernel and, usually, the binaries and libraries, too. Shared components are read-only, with each container able to be written to through a unique mount. This makes containers exceptionally “light” – containers are only megabytes in size and take just seconds to start, versus minutes for a VM.

    The benefits of containers often derive from their speed and lightweight nature; many more containers can be put onto a server than onto a traditional VM. Containers are “shareable” and can be used on a variety of public and private cloud deployments, accelerating dev and test by quickly packaging applications along with their dependencies. Additionally, containers reduce management overhead. Because they share a common operating system, only a single operating system needs care and feeding (bug fixes, patches, etc). This concept is similar to what we experience with hyper-visor hosts; fewer management points but slightly higher fault domain. Also, you cannot run a container with a guest operating system that differs from the host OS because of the shared kernel – no Windows containers sitting on a Linux-based host.


We can conclude, how Containers are differ from Virtual Machines ?

    Using containers, everything required to make a piece of software run is packaged into isolated containers. Unlike VMs, containers do not bundle a full operating system - only libraries and settings required to make the software work are needed. This makes for efficient, lightweight, self-contained systems that guarantees software will always run the same, regardless of where it’s deployed.

Install Docker in you system -

    Here we are going to install Docker in Ubuntu 16.10. If you have different Os please refer Get Docker

    You can download this docker.sh script and keep it in home directory and install by running -

        $ sh docker.sh

    OR

    You can install docker by running these command manually -

    $ sudo apt-get install -y --no-install-recommends \
        apt-transport-https \
        ca-certificates \
        curl \
        software-properties-common

    $ curl -fsSL https://apt.dockerproject.org/gpg | sudo apt-key add -
        apt-key fingerprint 58118E89F3A912897C070ADBF76221572C52609D

    $ sudo add-apt-repository \
           "deb https://apt.dockerproject.org/repo/ \
           ubuntu-$(lsb_release -cs) \
           main"

    $ sudo apt-get update
   
    $ sudo apt-get -y install docker-engine

    $ sudo apt-get update


Verify that Docker installed correctly by running the hello-world image.

   
    $ sudo docker run hello-world


Integrate Docker with Rails application

    Go to your app directory and make a file Dockerfile. In which we install all the system dependencies packages.

    In Dockerfile


        FROM ubuntu:latest
        MAINTAINER Sonu Kumar <er.sonukr@gmail.com>
        # Ignore APT warnings about not having a TTY
        ENV DEBIAN_FRONTEND noninteractive
        # Ensure UTF-8 locale
        RUN locale-gen en_US.UTF-8
        ENV LANG en_US.UTF-8
        ENV LC_ALL en_US.UTF-8
        RUN dpkg-reconfigure locales
        # Set the Ruby version of your preference
        ENV RUBY_VERSION 2.3.0
        # Install build dependencies
        RUN apt-get update -qq && \
            apt-get install -y -qq \
              build-essential \
              ca-certificates \
              curl \
              git \
              libcurl4-openssl-dev \
              libffi-dev \
              libgdbm-dev \
              libpq-dev \
              libreadline6-dev \
              libssl-dev \
              libtool \
              libxml2-dev \
              libxslt-dev \
              libyaml-dev \
              software-properties-common \
              wget \
              zlib1g-dev \
              mysql-client \
              libmysqlclient-dev \
              libsqlite3-dev \
              imagemagick \
              libmagickwand-dev \
              nodejs \
              zip
        # Install ruby via ruby-build
        RUN echo 'gem: --no-document --no-ri' >> /usr/local/etc/gemrc &&\
            mkdir /src && cd /src && git clone https://github.com/sstephenson/ruby-build.git &&\
            cd /src/ruby-build && ./install.sh &&\
            cd / && rm -rf /src/ruby-build && ruby-build $RUBY_VERSION /usr/local
        # Install bundler currently  we're using this but we should not.
        RUN gem install bundler
        ENV APPLICATION_NAME=Your_app_name
        # Clean up APT and temporary files when done
        RUN apt-get clean -qq && \
            rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
        RUN mkdir -p /$APPLICATION_NAME
        WORKDIR /$APPLICATION_NAME
        ADD Gemfile /$APPLICATION_NAME/Gemfile
        ADD Gemfile.lock /$APPLICATION_NAME/Gemfile.lock
        RUN bundle install
        ADD . /$APPLICATION_NAME


Above file prepare all the system dependencies of our application.

Now, make another file in docker-compose.yml. These settings will download the image and will create containers. Here, I am using services for mysql and memcached. You can use different services as per your application requirements.

    In docker-compose.yml



        version: '2'
        services:
          db:
            restart: always
            image: 'mysql:5.7'
            environment:
              MYSQL_ROOT_PASSWORD: *******
            ports:
              - "3306:3306"
            volumes:
              - "../shared/.mysql-data:/var/lib/mysql"

        memcached:
            restart: always
            image: memcached
            ports:
              - "11211:11211"

       
        web:
            depends_on:
              - 'db'
              - 'memcached'
            build: .
            environment:
              RAILS_ENV: development
              MEMCACHED_HOST: memcached
            command: bundle exec rails s -p 3000 -b '0.0.0.0'
            volumes:
              - .:/your_app
            ports:
              - "3000:3000"

We have to configure our database.yml for using above host. So, add host as db in database.yml

        In database.yml


            development: &default
              adapter: mysql2
              encoding: utf8mb4
              collation: utf8mb4_unicode_ci
              reconnect: true
              database: docker_development
              pool: 15
              reaping_frequency: 25
              username: root
              password: ********
              host: db
              port: 3306

Now, we have to build docker services

   
    $ sudo docker-compose build

Up the docker infrastructure by running. It will run you rails server also 

    $ sudo docker-compose up

You can run all rake commands by preceding docker-compose exec web

    ex-

        docker-compose exec web rake db:create
       
        docker-compose exec web rails c
       
        docker-compose exec web mysql -u root -p

        docker-compose exec web tail -f log/*.log