Managing threads with Queue and SizedQueue

Threads in Ruby

To make our applications do multiple things faster at the same time, we can use Ruby threads. For a long-running application, re-using a pool of such threads can provide great performance benefits. Thread pool is a way to have a fixed set of threads doing the work, instead of creating new ones each time.

Considering a simple program as below, we realise the importance of threads.

You will get the sum of each array as output but the sleep(4) instruction will pause execution for 4 seconds and then continue. Thus, group1 will get the sum after 4 seconds, group2 after 8 seconds and so on, which is not feasible. In such cases it is more economical to have threads, since we can have the sum of each arrays calculated independently.

Threads allow us to execute different parts of our program independently. For implementing threads, after initialising each array,

The add_element method definition is same but we wrapped method call in a Thread.new block.

Now, instead of getting the sum of each array after 4 seconds, 8 seconds and 12 seconds respectively, you will get the sum of all arrays after 4 seconds. This indicates better performance and efficiency which is the power of threads.

Queue for managing threads

To safely exchange information between threads, we can use Queue in the standard library. The tasks that are added first in the queue are retrieved first. PUSH and POP are the two main methods in Queue that adds and retrieves an item respectively.

Consider the following example.

To create new queue instance, use new() method.

5 items are inserted into the queue.

Output as follows,

Now, to pop off items from queue,

It produces the following output

Sized queue for fixed-length queue

Sized queue is useful in situations where rate of production is higher than consumption.

In the following example,

We see, 10 items are produced and 4 items …

Read More

Make unit tests great again – Integrate Jasmine into Rails

Jasmine is a framework to write tests for the Javascript code in the Behaviour Driven Development (BDD) style. In this article, you will learn how to integrate Jasmine into your existing rails application and how to write clean and beautiful unit tests. Let us make tests great again!

 

Install Jasmine

To make Jasmine available to your Rails app, you just have to place the jasmine-gem (link) in your Gemfile. That will do the magic. Just make sure you have it under Development and Test group in the Gemfile as follows:

Then run this to install the gem:

After all the gems are installed, run this code to generate necessary files for Jasmine to run:

This will create the jasmine helper file and the yml file where you configure how it should run the tests.

Run tests

You can use Jasmine right after it’s installed. It can be run in several ways, the most important ones being,

  • In your browser
  • Continuous Integration Mode (CI)
  • The CI mode is usually used when you have to integrate it into your build system.

    Browser Mode

    You have to start the Jasmine server to run it in a browser. This server runs all the tests and serves the results to a webpage. Run this to start the Jasmine Server:

    With the default settings, you can view the output in:

    But this page would be pretty empty since you don’t have any tests written for your Javascript code. There is a method provided to generate sample tests. Try running this:

    Now refresh the webpage and you can see something similar to this:

    Jasmine test page

    Configurable Settings

    Clicking on the options button in the top right corner will display a list of options that change how Jasmine runs the tests. Let’s get into each one of them:

    Raise Exceptions

    This …

    Read More

    Readability of text using odyssey

    When you are writing something like article, text, document etc you are focusing on readability. If you are not then you should. As readability influence how a reader can read and understand the content, how you are presenting the content etc. It would also influence how much likely one is to share your article as well. To find the readability there are a lot of statistical tests. Few are listed below.

    • Flesch-Kincaid readability test
    • Flesch Kincaid Grade Level

    • Gunning Fog Score

    • SMOG

    • Coleman Liau Index

    • Automated Readability Index (ARI)

    Recently in a project that we worked on we were asked to find the readability of various pages of a website. We decided to start with Flesch-Kincaid test, as we found this to be a popular one in our research.

    Flesch-Kincaid readability test is designed to indicate how difficult a passage in English is to understand. In this test higher score indicates how easier to read and a lower score indicates how difficult it is to read.The formula to find Flesch-Kincaid reading-ease score is

    206.835 – 1.015 * (total words / total sentences) – 84.6 * (total syllables / total words)

    The scores can be interrupted as

    Score
    School Level
    Notes

    100.00-90.00
    5th grade
    Very easy to read.

    90.00-80.00
    6th grade
    Easy to read.

    80.00-70.00
    7th grade
    Fairly easy to read.

    70.00-60.00
    8th & 9th grade
    Plain English.

    60.00-50.00
    10th to 12th grade
    Fairly difficult to read.

    50.00-30.00
    College
    Difficult to read.

    30.00-0.00
    College Graduate
    Very difficult to read.

     

     

    Since we were not experts we wanted the ability to tweak and play around with it. We found an already build gem called Odyssey which had all these various tests and also provided the ability to extend this feature as well. So here in this article, we will discuss how to use Odyssey gem to find readability of an article and a web page.

    Install Odyssey

    Add in your Gemfile.

    Usage

    So if we want to use the Flesch-Kincaid test, we write the code as below.

    To find …

    Read More

    to_json vs as_json in Rails API

    Recently we have been working on Rails API. During that time, we were curious about the difference between as_json and to_json. In this article, we are sharing the difference we learned.

    to_json in Rails API

    Let’s discuss how we started out building our APIs using ‘to_json’. to_json will return JSON string representing the hash. Option are passed to each element.

    In to_json without any option,  the returned JSON string will include all the model attributes

    to_json had some great options of ActiveRecord objects. We could just tell the method to only render certain attributes, or to include association or method calls. We had:

    • only – Only show column names in the output as specified in the list
    • except – Show all column names except the ones specified in the list

    to_json works fine and seamlessly ok, as long as the database schema is deeply coupled with the API output.When it takes the next level where we want to render a certain attribute in json it start to break.

    This will start to generate a bit load to controllers. Such methods of generating json don’t feel quite right and begin to break down. This is because the to_json is interested in ‘serializing’ a database object while most of the developers try to put relevant representation for their API.

    Anytime to_json is called on an object, as_json is invoked to create the data structure, and then that hash is encoded as a JSON string using ActiveSupport::json.encode. This happens for all types: Object, Numeric, Data, String etc.

    Now the creation of the json is separated from the rendering of the json.  as_json is used to create the structure of the json as a Hash, and the rendering of that hash into JSON string is left up-to ActiveSupport::json.encode.

    as_json in Rails API

    as_json Returns a hash representing the model. Like in to_json, option are passed to each …

    Read More

    Setup your rails application with one command

    Generally setting up a rails app is running some same set of commands, for example installing gem, creating database, loading schema and start rails server.

    We can create a script and run it to setup our application since Rails 4.2 a bin/setup file is generated by default.

    Here is the example of setup script.

    Setup script sets your rails application for development environment instantly.  This is helpful when a new developer trying to set up your application or when setting up an application on a new machine. We should keep the setup script updated.

    Let’s understand the setup script step by step and modify according to our requirements.

    First setup basic required library and path of our application in APP_ROOT

    Now system! method here execute the commands passed to it and shows the backtrace of error in case of command fails.

    system will execute the given rails command and abort will stop script execution and will show us the backtrace of failed command, we can modify it if we don’t want to stop the execution of the script and get some custom error messages.

    In the below snippet of the script, we are going to our application root and performing operations.

    To create config file if not already exist, modify your script as

    Read More

    Authorization with Pundit gem

    Security is an important aspect of application development. Two main components of security are Authentication (Who are you?) and Authorization (are you supposed to be here?). Authentication verifies the user’s identity while authorization verifies whether that user has access rights on certain resources to perform actions on them.

    Two popular gems for authorization in the rails world are CanCanCan and Pundit, we at Red Panthers prefers pundit over CanCanCan we get to write Pure Ruby Objects and keep the logic for each part separate.

    The gem CanCanCan isolates (encourages) all authorization logic into a single class. It is a drawback when the complexity of application increases. Pundit gem provides object oriented design patterns to build our own authorization system that meets project’s requirements. It enables us to keep the models and controllers free from authorization code and allows to keep the resource logic separately. This flexibility and simplicity of Pundit gem help to use it with ease.

    To start with Pundit

    Add Pundit gem to your gem file and run bundle install.

    Integrate Pundit to Rails application by adding the following line to ApplicationController.

    If you run the Pundit’s generator as below, it will generate app/policies folder which contains application_policy.rb by default.

    Then restart the Rails server.

    Base class policy looks like

    Create policies

    Policy classes are the core of Pundit. In the app/policies folder, we can write our own policies. Each policy is a Ruby class. Each policy class should be named after a model they belong to, followed by the word Policy. For example, use CollectionPolicy for Collection model. Pundit can also be used without an associated model.

    Pundit uses the current_user method to get the first argument in initialize method in ApplicationPolicy. But if current_user is not the method that should be invoked by Pundit, simply define a method in your controller.

    Logically, Pundit can be used outside controllers, for …

    Read More

    Deploying Rails application on AWS, using Nginx, Puma and Mina

    In this tutorial we are going to setup an AWS EC2 instance for deployment of rails application.

    Server Setup

    Following is the setup instruction for ubuntu 16.04 using ruby 2.4, Postgres 9.6 and rails 5.

    Installing Ruby

    Setting up dependencies for ruby.

    Next step is to install ruby using RVM.

    Last step is to install bundler.

    Installing Rails

    Installing NodeJS, this lets you use Coffee script and the Asset Pipeline in Rails which combines and minifies your javascript to provide a faster production environment.

    Next is to install rails.

    You can verify the installation of rails version.

    Installing PostgreSQL

    The postgres installation doesn’t setup a user for you, so you’ll need to follow these steps to create a user with permission to create databases.

    Install Nginx

    Following are some basic commands to manage Nginx:

    Nginx Configuration

    First disable default site by removing the symlink.

    Now create a new virtual host config file.

    edit the my_app.conf file:

    Puma Configuration

    Change config/puma.config file according to following example:

    Create puma upstart script

    Let’s create an Upstart init script so we can easily start and stop Puma.

    Now open the provided puma.conf file, so we can configure the Puma deployment user.

    Look for the two lines that specify setuid and setgid, and replace “apps” with the name of your deployment user and group.

    Now copy the scripts to the Upstart services directory.

    The puma-manager.conf script references /etc/puma.conf for the applications that it should manage. Let’s create and edit that inventory file now.

    Each line in this file should be the path to an application that you want puma-manager to manage. Add the path to your application now. For example.

    Jungle upstart script provides following commands to manage puma app server:

    Install Mina

    Add in your Gemfile.

    and run bundle install to install mina.

    Create deployment script

    It will create config/deploy.rb, let’s edit it as following:

    Now to setup directory structure run:

    To deploy the application run:

     

    REFERENCES:

    https://gorails.com/setup/ubuntu/16.04

    https://github.com/mina-deploy/mina

    https://github.com/puma/puma

     

    Read More

    [Tip] Mina: Find last git commit released

    A project that we were handling hadn’t had a deployment in some time. So we were confused on what all commits were going to go to production ( bad karma for us for not keeping a release or change log). We use mina for all our deployment as we found it to be faster that Capistrano. So we were sure that there would be something on our server to help us settle this dilemma. We finally solved this by going through the various folders and files.

    Edit the file inside the file with your branch name located at /path/to/project/scm/refs/heads. Like for example if you have your project in /var/www folder and you are deploying master then you should edit the file called master found at the following location./var/www/project/scm/refs/heads/master that file would have only one line and that’s the last git commit released.

     

    Read More

    Taking screenshots of webpages using Ruby

    Recently we have been working on taking screenshots for web page while generating reports based on that website. During this endeavor, we came across some tools to achieve that. We are sharing the information we gathered here.

    The tools that we will discuss for screencapture, in  Ruby,  are:

    • PhantomJS
    • Screencap gem
    • Webshot gem

    Screencap and webshot are gems in Ruby that depends on a tool called PhantomJS. It is basically a web browser without a user interface, primarily used for testing websites. Such type of browsers are generally referred to as headless browsers,

    Screenshots with PhantomJS

    PhantomJS is quite easy to install and is multiplatform, available on major operating systems.

    To start, our script must require a reference to the webpage module then use it to create an instance:

    Use the instance.open() method and pass it the arguments, the url of the webpage that we want to capture screenshot.

    instance.render() method captures the screenshot and saves it to the file specified in the argument.
    Run the script as,

    Screenshot is saved in the  directory where we run the script.

    Now what we have above is all JavaScript, but to use the phantom JS in our rails application we have gems that provide us with an easy interface to acheive the same.

    Screencap gem

    The gem ‘screencap’ is introduced in Ruby to make screenshot capture easy. However, the gem depends on  ‘PhantomJS’, so we need to have PhantomJS installed on machine to capture screenshots with screencap gem.
    But screencap does not work with PhantomJS 2.1.1, which is the latest version.
    So we need to install version 1.9.8

    check version

    To install screencap gem in your application, add this line to gemfile

    Or install it as

    Usage

    To capture screenshot with screencap

    • Fetcher class accepts url of the website to take screenshot.
    • Screenshot is taken when fetch method is called.
    • Also, fetch method supports various optional parameters to specify the area to capture the screenshot.
    • We can specify where to store the …
    Read More

    Difference between Date, Time and DateTime

    Date and time are one of the most important aspects which every coder has to deal with in Ruby. Well, let’s get to know how we keep it up alive and functional.
    There are 3 different classes in Ruby that handles date and time. They are Date, Time and DateTime. Date and DateTime classes are both from date library. And Time class from its own time library.

    In this article we’ll see how Date and Time works. Let’s have a look at each one of them.

    Date

    When you need a string format of year, month and day, you have to go through Date class.

    • Has date attributes only (year, month, day)
    • Based on integer whole-day intervals from an arbitrary “day zero”
    • Can handle date arithmetic in units of whole days
    • Date object is created with ::new, ::jd, ::ordinal, ::commercial, ::parse, ::strptime, ::today, Time#to_date etc.
    • Takes 4 bytes to store.

    Eg:

    Time

    If you need both date and time values, we can make use of Time class.

    • Has date and time attributes (year, month, day, hour, min, sec, subsec)
    • Can handle negative times before unix time
    • Can handle time arithmetic in units of seconds

    Eg:

    Also rails provide a really good time class called ActiveSupport::TimeWithZone. It contains all the features the Time class have, plus many improvements, such as the support for configurable time zones.

    DateTime

    • Has date and time attributes (year, month, day, hour, min, sec)
    • It  is formatted as YYYY-MM-DD HH:MM:SS
    • Based on fractions of whole-day intervals from an arbitrary “day zero” (-4712-01-01)
    • Can handle date arithmetic in units of whole days or fractions
    • Takes 8 bytes to store, and has a precision of .001 seconds.
      • A four-byte integer packed as YYYY×10000 + MM×100 + DD
      • A four-byte integer packed as HH×10000 + MM×100 + SS
    • Valid ranges go from the year 1000 to the year 9999
    • It is created with ::new, ::jd, ::ordinal, ::commercial, ::parse, ::strptime, ::now, Time#to_datetime etc.

    Eg:

    Let’s see the Differences among all of them which makes …

    Read More