Friday, 12 December 2014

Installing and Maintaining Multiple Versions of Rails in mac


         In this post, I would like to share how we can easily install multiple version of Rails in mac and isolating one from another
so, that you can build two Rails applications at a time in your system which uses different Rails versions.

 
Step1: If you don’t have rvm already installed in your mac, Install rvm in mac for easy version managing. To install rvm, use the 
following command                       
$ \curl -sSL https://get.rvm.io | bash -s stable
 
Step2:  Once rvm is installed, you can use rvm Gemsets  for maintaing multiple Rails  versions.In order to view the already 
available gem sets in your system type                                                    
$rvm gemset list
 
Step3:  Now create a new gem set. In order to create a new gem set, type                                              
$rvm gemset create gemsetname
 
Step4: Now use this gem set                                           
$rvm gemset use gemsetname
 
Step5: Now you can install the Rails version that you want for creating the New project by typing                                 
$gem install rails -v railsversion
 
Step6: Once the rails is installed. Now your gem set uses the  recently installed Rails, whenever you use that particular gem set. 
Now you can view the list of available gem sets by typing                                                  
$rvm gemset list
 
Step7: Now your system uses the recently created gem set for selecting Rails version during New Project creation. You can use any 
of the Ruby Interpreter version with this gem set by typing                               
$rvm use rubyversion @gemsetname
You can also specify the *default* gem set for the given Ruby interpreter by typing
$rvm use rubyversion @gemsetname —default
Now your New project uses Rails railsversion and Ruby rubyversion for it’s Interpretation.  
Step8: Now once you have finished your job in the New Project, you can easily switch back to earlier gem set by typing                                     
$rvm use earliergemsetname
OR, You can create one more gem set following the same above procedure.  
 
Conclusion: Using gem sets you can easily create as many gem sets as you want and easily switch between them.

Monday, 18 August 2014

Httparty Timeout Error when Calling a local Controller from an API

When we try to call a local controller from an API it forms a loop as the Web application trying to send the request and the one trying to receive the request are same.


This can be solved by setting a timeout in the Httparty post or get call

    response = HTTParty.post(url, :timeout => 0, :body => {}.to_json, :head => {})

The default  timeout will be "60" seconds and by setting it to "0" will solve the Timeout problem but we even have to handle the Timeout exeption so, we can do as given below

 begin
    response = HTTParty.post(url, :timeout => 0, :body => {}.to_json, :head => {})
 rescue Timeout::Error
       p "Could not post to #{url}: timeout"     
 end  
 

Thursday, 24 July 2014

Ruby gem to generate a default Image for a contact using contact's name


Label Images

This gem can be used for generating a default image for a contact to display in your app if you don't  have any image of contact.

It provides a helper method which will return you a css template of the image.

Installation

Add this line to you Gemfile
     
gem 'label_images'

and then execute

$bundle

Or install it yourself using command

$gem install label_images



Usage 

Require the gem 

require 'label_images'

Now you can call the helper method using:
  
generate_image(args1,args2,....).html_safe


This method takes the following arguments and all parameters are OPTIONAL

first_name ==> Name of the contact. Default value is empty string

last_name ==> This is optional parameter and use it if you need two alphabets to be displayed in the  image. Default value is empty string

shape ==> You can set the shape of the image.this parameter takes any of "circle" or "square" as value. Default value is square

radius ==> Set this  if you set the shape parameter to circle.this should be specified in pixels.  Default value is 40px.

width ==> Set this if you set the shape parameter to square.this should be specified in pixels. Default value is 80px. 

bg_color  ==> You can set the background color of the image.  Default value is generated randomly from few available color codes.

font_size ==> You can set the font size of the alphabet to be displayed in the image. this should be specified in pixels. Default value is 20px. 

font_family ==> You can set the font family of the alphabet to be displayed in the image. Default font family is verdana.

font_color  ==> You can set the font color of the alphabet to be displayed in the image. Default font color is white.

Example usage:

require 'label_images'
generate_image(first_name:"john", last_name:"peter", shape:"circle", radius:60, font_size:20).html_safe

output screenshot: