In the previous article, we have seen how to configure DigitalOcean Droplet. Now let's proceed further to set up our application repository and Ngnix with Phusion Passenger on the DigitalOcean droplet.

1. Install RVM and Ruby

We will install ruby using Ruby Version Manager(RVM) as it gives more flexibility to manage multiple ruby versions on the same system over rbenv, chruby, etc. Import RVM GPG key, before installing RVM:

gpg --keyserver hkp://keys.gnupg.net --recv-keys...

Read complete blog post

String encoding is something that we don't really think until we see

 Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT 

Or when users complains about missing special characters like "" (apostrophe copied from Microsoft Word) or when "菜医生" becomes "иЏњеЊ»з”џ".

Before we go into encoding problems, lets understand what encoding is.

A string can be considered as an array of bytes:

irb(main):001:0> "world".bytes
=> [119, 111, 114, 108,...

Read complete blog post

Recently while working on one of our project we came across requirement where we wanted Hubspot integration with our application for ease of sales team to manage user's data and understand their interactions and trends in application. I will quickly explain few things about Hubspot here in my article.

What is Hubspot?

In brief, Hubspot is a CRM which helps you monitor and fine tune many things regarding your application. Based on my analysis I will shortly list down few most important of them...

Read complete blog post

In software development you are destined to deal with lots of code that is not written by you. What would be more stressful than debugging the code that you barely understand. You might have been in the situation where you have been assigned a bug and you have to traverse through lots of file which you have no idea of, just get to root cause of the issue. Being in these situations many times, I have learned few tips and tricks for debugging and would like to share it here.

Checking where the...

Read complete blog post

A regular expression is a pattern describing a certain amount of text and is a type of shorthand to describe a search pattern. It can be used to find text which matches a pattern within a larger text, to replace the matching text or to split the matching text into groups. Regular expressions power of extracting specific text from documents resides in their ability to replace many lines of code with as little as one line.

Some terms used in regular expressions:

  1. Literal - A literal is any...

Read complete blog post

Ruby is too dynamic. We can do almost anything in runtime, from creating the classes at runtime to creating methods dynamically.

If you are coming from some other language, it would be shocking for you too know that nothing is private in ruby. You can access private and protected method from anywhere you want.

You can call this a flexibility or a curse. But a smart developer knows when to utilize the power of extreme flexibility that Ruby provides and when to stay away from it.

Is this all...

Read complete blog post

What is Lazy Enumerator

Ruby 2.0 lazy enumerator allows you to iterate over infinite values and take the values you want for your calculation.This code would result in an infinite loop.

range = 1..Float::Infinity
range.collect { |x| x*x }.first(10)

The code results in an infinite loop because the Enumerable#collect is eager.It means that first collect processes all the values from the first array and then save it in the second array.Since we have an infinite range here first...

Read complete blog post

Ruby Comments

There are basically two types of comments in Ruby. And they work in the same way as follows:

The block comment

The comment block is created with the =begin and =end delimiters.

def welcome
 =begin
 This will print the welcome message.
 You can also add your custom message.
 =end
 puts "Welcome to ruby comments learning :)"
end

The line comment

This is the simple comment where you place an octothorpe(#) as the first non-whitespace...

Read complete blog post

Few weeks back I was reading a blog about concurrency limitations in Ruby (which we all are aware since long) and how Elixir is evolving. Thus I was extremely curious to know this new dynamic functional programming language "Elixir", the two decades old Erlang language & Erlang Virtual Machine (VM) known for running low-latency, distributed and fault-tolerant systems. This article is a result of my curiosity about Elixir and Erlang.

This article does not cover (i.e. out of scope)...

Read complete blog post

This is very useful Ruby command to share your local folder with everyone on the same network.

Thanks to Rohan! As we were working on something and he wanted me to share my local folder quickly.

Rohan shared this Ruby command with me and since then every now and then I am using it in office to share stuff with teams. Command is:

ruby -rwebrick -e 'WEBrick::HTTPServer.new(:Port=PORT_NUMBER,:DocumentRoot=”DIRECTORY_PATH").start'
e.g.
 ruby -rwebrick -e...

Read complete blog post

If you have an application which creates notes, learning how to integrate evernote to sync notes will be very handy.

You may refer this ruby gem - evernote_oauth which has useful methods to access your existing evernote notebooks, create notebooks and notes, etc. The gem is well documented.

Refer this blog to understand and see code to create note on evernote - Create Note on Evernote

If you refer the code in the above blog to create a note - just this particular snippet -

n_body = "<?xml...

Read complete blog post

In our current app, there was a requirement to have a nice editor which will provide the basic functionalities like bold,italic and also image upload. We decided to use redactor-rails gem Github-Documentation-Link(Github Doc) .

So i took following steps:

  • In Gemfile i included:
gem 'redactor-rails'
  • Then for image uploading and storing i included two more gems as:
gem 'carrierwave'
gem 'mini_magik'
  • Bundle it.
rails generate redactor:install
rails generate redactor:config
  • In...

Read complete blog post

If you want to send any notification message from your rails application, its easy to send it with following steps:

1) To send notification to your flowdock group, you will need email-address of flowdock grouYou can get it from flow-settings of your group.

example: "xxxx@xxxx.flowdock.com"

2) In your application, define mailer class and send email-message to above email.

class UserMailer < ActionMailer::Base
 default from: "xxxx@example.com"

 def flowdock_notification(post)
 flow_email = "xxxx@xxxx.flowdock.com"
 body = post.user.name + " posted a new article: \n#{post.subject}"
 mail(to: flow_email, subject: "New Post") do |format|
 format.text { render text: body }
 end
 end
end

Thanks.

Read complete blog post

Apipie-rails is a DSL and Rails engine for documenting you RESTful API. Instead of traditional use of #comments, Apipie lets you "describe the code by code". This brings advantages like:

  • It uses ruby syntax
  • Reusable doc for other purposes (Best for validations)
  • Easier to extend and maintain

apipie-rails gem owner has documented it in very nice way with examples. Refer Github Documentaion(github doc)

Path to documentation:

The documentation is available right in your app (by default under...

Read complete blog post

Code Review

Hello Everyone, I have reviewed a codebase of one of our projects. It was a good experience.

Few good things.

1) Really clean code.
2) Small methods.
3) I had to travel 5 months back commits to find some bad code. :)

Things to improve on:

1) Found less specs, But it is possible that specs have been covered already.

Overall Thoughts:

The code is super clean, liked it!. fantabulous work folks!

Snippets:

1) Code:

 Menu.pluck(:hyphenated_name)

Suggestion:
This can be moved to class...

Read complete blog post

In one of the projects(ACES), we came across a functionality: to parse huge(50mb+) XML files. Here efficiency was very critical piece, as file size was huge. Also we wanted the results of parsing quickly for further analysis so the effective time was less and application performance was unhampered.

We chose SAX Parsing Technique(DOM vs SAX)

There are two ways to parse an xml file, DOM and SAX. SAX being more efficient as it process the xml file incrementally, thereby memory utilization is...

Read complete blog post

This topic is about using Redis and LUA script in Rails.

Before proceeding, you may like to read Introduction to LUA script with Redis

Redis doesn't have any command to calculate total number of keys

We'll use zrangebyscore to find number of members between index range.

Instal redis-rb

Start rails console

Prepare a dataset based on timestamp

  t10 = 10.minutes.ago
= 1386935151
 t20 = 20.minutes.ago
= 1386934568
 t30 = 30.minutes.ago
= 1386933971
 t40...

Read complete blog post

Run Helper methods in the console

$rails c
= helper.number_to_currency(100)
= "$100.00"
= helper.time_ago_in_words(3.days.ago)
= "3 days"

Shorthand Migrations

$ rails g resource person first_name:string last_name:string email:string token:string

You can write above command as-

$ rails g resource person first_name last_name email token

Both will generate same migration:

class CreatePeople < ActiveRecord::Migration
 def change
 create_table :people do |t|
 t.string :first_name
 t.string :last_name
 t.string :email
 t.string :token

 t.timestamps
 end
 end
end

Add Indexes to migrations

$rails g resource person...
< ActiveRecord::Migration def change create_table :people do |t| t.string :first_name t.string :last_name t.string :email t.string :token t.timestamps end end end

Read complete blog post

There are several practices followed in Rails.
Since ruby is the backbone of rails, here are few practices that are recommended:

Lets consider a class ABC:

class Animal
 attr_accessor :id, :type
 def self.mouse(object, a1, a2)
 puts "#{self}"
 end
 def animal
 puts "I m in animal instance method"
 yield if block_given?
 end
 def id
 @id.to_i
 end
 def initialize(id, type)
 @id = id
 @type =...

Read complete blog post

Problem case to solve : Crawl any given site(url), scrape each page uniquely for content from given selector or Xpath

Available options for Crawling and Scraping :

1) With libxml2 Nokogiri is one of the fastest HTML/XML parser. Almost all of the scraping tools written in Ruby such as Mechanize, Wombat, Anemone, etc. uses Nokogiri as there base DSL for sraping.

2) Mechanize - Is a beast in this category. It efficiently uses Nokogiri for HTML scrapping. Do see Abi's post on Automating browser...

Read complete blog post