<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Fun On Rails</title>
    <description>Journal of a Web Developer #ruby #rails #JS 
</description>
    <link>http://funonrails.com/</link>
    <atom:link href="http://funonrails.com/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Tue, 13 Sep 2016 17:43:37 +0530</pubDate>
    <lastBuildDate>Tue, 13 Sep 2016 17:43:37 +0530</lastBuildDate>
    <generator>Jekyll v2.4.0</generator>
    
      <item>
        <title>Grape API authentication using Devise Auth Token</title>
        <description>&lt;p&gt;In &lt;a href=&quot;http://funonrails.com/2014/03/building-restful-api-using-grape-in-rails/&quot;&gt;previous post&lt;/a&gt; we saw how to buil RESTful API using Grape. In this post we will see how to add &lt;em&gt;devise auth token&lt;/em&gt; to &lt;em&gt;users&lt;/em&gt; and how to use it in &lt;em&gt;Grape API&lt;/em&gt; authentication.&lt;/p&gt;

&lt;p&gt;Lets see how this can be done assuming you already have devise setup ready.&lt;br /&gt;
## Add &lt;em&gt;token_authenticable&lt;/em&gt; to devise modules (works with devise versions &amp;lt;=3.2)&lt;br /&gt;
In &lt;em&gt;user.rb&lt;/em&gt; add &lt;em&gt;:token_authenticatable&lt;/em&gt; to the list of devise modules, it should look something like below:&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;c1&quot;&gt;# ..code..&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  &lt;span class=&quot;n&quot;&gt;devise&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:database_authenticatable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;ss&quot;&gt;:token_authenticatable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;5&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;ss&quot;&gt;:invitable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;6&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;ss&quot;&gt;:registerable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;7&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;ss&quot;&gt;:recoverable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;8&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;ss&quot;&gt;:rememberable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;9&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;ss&quot;&gt;:trackable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;10&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;ss&quot;&gt;:validatable&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;11&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;12&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  &lt;span class=&quot;n&quot;&gt;attr_accessible&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:authentication_token&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;13&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;14&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  &lt;span class=&quot;n&quot;&gt;before_save&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:ensure_authentication_token&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;15&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;c1&quot;&gt;# ..code..&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;16&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;!-- more --&gt;
&lt;p&gt;## Generate Authentication token on your own (If devise version &amp;gt; 3.2)&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;class User &amp;lt; ActiveRecord::Base
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;# ..code..
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  devise :database_authenticatable,
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    :invitable,
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;5&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    :registerable, 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;6&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    :recoverable, 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;7&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    :rememberable, 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;8&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    :trackable, 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;9&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    :validatable
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;10&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;11&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  attr_accessible :name, :email, :authentication_token
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;12&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;13&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  before_save :ensure_authentication_token
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;14&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;15&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  def ensure_authentication_token
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;16&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    self.authentication_token ||= generate_authentication_token
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;17&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  end
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;18&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;19&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  private
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;20&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;21&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  def generate_authentication_token
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;22&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    loop do
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;23&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      token = Devise.friendly_token
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;24&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      break token unless User.where(authentication_token: token).first
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;25&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    end
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;26&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  end&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;h2 id=&quot;add-migration-for-authentiction-token&quot;&gt;Add migration for authentiction token&lt;/h2&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;rails g migration add_auth_token_to_users
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      invoke  active_record
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      create    db/migrate/20140326204628_add_auth_token_to_users.rb&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Edit migration file to add &lt;code&gt;:authentication_token&lt;/code&gt; column to users&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;# db/migrate/20140326204628_add_auth_token_to_users.rb
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;class AddAuthTokenToUsers &amp;lt; ActiveRecord::Migration
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  def self.up
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    change_table :users do |t|
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;5&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      t.string :authentication_token
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;6&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    end
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;7&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;8&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    add_index  :users, :authentication_token, :unique =&amp;gt; true
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;9&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  end
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;10&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;11&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  def self.down
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;12&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    remove_column :users, :authentication_token
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;13&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  end
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;14&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;end&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;h4 id=&quot;run-migrations&quot;&gt;Run migrations&lt;/h4&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;rake db:migrate&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h4 id=&quot;generate-token-for-existing-users&quot;&gt;Generate token for existing users&lt;/h4&gt;
&lt;p&gt;We need to call save on every instance of user that will ensure authentication token is present for each user.&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;User.all.each(&amp;amp;:save)&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;h4 id=&quot;secure-grape-api-using-auth-token&quot;&gt;Secure Grape API using auth token&lt;/h4&gt;
&lt;p&gt;You need to add below code to the &lt;em&gt;API::Root&lt;/em&gt; in-order to add token based authentication. If you are unware of &lt;em&gt;API::Root&lt;/em&gt; then please read &lt;a href=&quot;http://funonrails.com/2014/03/building-restful-api-using-grape-in-rails/&quot;&gt;Building RESTful API using Grape&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In below example, We are authenticating user based on two scenarios&lt;br /&gt;
- If user is logged on to the web app then use the same session&lt;br /&gt;
- If session is not available and &lt;em&gt;auth token&lt;/em&gt; is passed then find user based on the token&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/root.rb&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Grape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;    &lt;span class=&quot;s1&quot;&gt;&#39;api&#39;&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;5&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;    &lt;span class=&quot;ss&quot;&gt;:json&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;6&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;7&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;n&quot;&gt;rescue_from&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:backtrace&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;8&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;n&quot;&gt;error_formatter&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ErrorFormatter&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;9&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;10&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;n&quot;&gt;before&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;11&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      &lt;span class=&quot;n&quot;&gt;error!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;401 Unauthorized&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;401&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;authenticated&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;12&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;13&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;14&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;n&quot;&gt;helpers&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;15&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;warden&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;16&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;        &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;warden&#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;17&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;18&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;19&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;authenticated&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;20&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;warden&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;authenticated?&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;21&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;        &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:access_token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find_by_authentication_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:access_token&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;22&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;23&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;24&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;current_user&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;25&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;        &lt;span class=&quot;n&quot;&gt;warden&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@user&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;26&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;27&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;28&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;29&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;n&quot;&gt;mount&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;V1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Root&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;30&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;n&quot;&gt;mount&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;V2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Root&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;31&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;32&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

</description>
        <pubDate>Thu, 27 Mar 2014 02:18:53 +0530</pubDate>
        <link>http://funonrails.com/2014/03/api-authentication-using-devise-token</link>
        <guid isPermaLink="true">http://funonrails.com/2014/03/api-authentication-using-devise-token</guid>
        
        
        <category>authentication</category>
        
        <category>grape</category>
        
        <category>devise</category>
        
        <category>token</category>
        
      </item>
    
      <item>
        <title>Building RESTful API using Grape in Rails</title>
        <description>&lt;p&gt;While developing a &lt;em&gt;rich client side web&lt;/em&gt; application or &lt;em&gt;mobile app&lt;/em&gt;, we need &lt;em&gt;RESTful JSON API&lt;/em&gt; which interacts with the &lt;em&gt;front-end javascript framework&lt;/em&gt;. Here you may use &lt;strong&gt;backbone.js&lt;/strong&gt;, &lt;strong&gt;ember.js&lt;/strong&gt; or &lt;strong&gt;angular.js&lt;/strong&gt; on the front-end side of application.&lt;/p&gt;

&lt;p&gt;Here we’ll be using &lt;em&gt;Ruby on Rails&lt;/em&gt; on the &lt;em&gt;back-end&lt;/em&gt; which will serve &lt;em&gt;JSON API&lt;/em&gt; consumable by fron-end framework. If you look at the &lt;a href=&quot;https://www.ruby-toolbox.com/categories/API_Builders&quot;&gt;ruby toolbox&lt;/a&gt; you’ll see many API Builder gems available but it seems &lt;em&gt;grape&lt;/em&gt; can be a good choice.&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;left no-border&quot; src=&quot;https://raw.githubusercontent.com/wiki/intridea/grape/grape_logo.png&quot; width=&quot;186&quot; height=&quot;67&quot; alt=&quot;Grape&quot; /&gt; &lt;em&gt;Grape is a RESTful API microframework built to easily and quickly produce APIs for Ruby-rooted web applications.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s see how we can build RESTful JSON apis using &lt;i&gt;Grape&lt;/i&gt; library:&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;h2 id=&quot;getting-started&quot;&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;Add &lt;em&gt;grape&lt;/em&gt; to your &lt;em&gt;Gemfile&lt;/em&gt; and then run &lt;em&gt;bundle&lt;/em&gt; install&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;gem &#39;grape&#39;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;modularizing-api-directory-structure&quot;&gt;Modularizing API directory structure&lt;/h2&gt;
&lt;p&gt;Place API files into &lt;em&gt;lib/api&lt;/em&gt;. You need to create &lt;em&gt;api&lt;/em&gt; folder inside &lt;em&gt;lib&lt;/em&gt; directory. &lt;br /&gt;
As we are placing api directory inside lib you don’t need to explicitly load it inside &lt;em&gt;application.rb&lt;/em&gt; &lt;br /&gt;
If you want to place &lt;em&gt;api&lt;/em&gt; directory at some other place then add below lines to to &lt;em&gt;application.rb&lt;/em&gt;&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paths&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;app/api&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;glob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;**/*.rb&amp;quot;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;autoload_paths&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Dir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#&amp;#x7b;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Rails&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;&amp;#x7d;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/app/api/*&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;First, Let’s create &lt;em&gt;API::Root&lt;/em&gt; class that will mount available api versions.&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/root.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Grape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;api&amp;#39;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;mount&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;V1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Root&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;# mount API::V2::Root (next version)&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Now, create a &lt;em&gt;API::V1::Root&lt;/em&gt; class that will mount resources for version 1&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/v1/root.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;V1&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Grape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;mount&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;V1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Posts&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;mount&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;V1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Authors&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Now, add resource &lt;em&gt;Posts&lt;/em&gt; available for api access in json format&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/v1/posts.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;V1&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Posts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Grape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;v1&amp;#39;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:json&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;resource&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:posts&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Return list of recent posts&amp;quot;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;10&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;11&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;          &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;12&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;        &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;13&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;14&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;15&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;16&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Now, lets add one more resource &lt;code&gt;Authors&lt;/code&gt; to version v1&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/v1/authors.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;V1&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Authors&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Grape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;v1&amp;#39;&lt;/span&gt; 
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:json&lt;/span&gt; 
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;resource&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:authors&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Return list of authors&amp;quot;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;10&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;11&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;          &lt;span class=&quot;no&quot;&gt;Author&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;all&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;12&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;        &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;13&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;14&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;15&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;16&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;mounting-api-under-rails-routes&quot;&gt;Mounting &lt;em&gt;API&lt;/em&gt; under rails routes&lt;/h2&gt;
&lt;p&gt;Mount &lt;em&gt;API::Root&lt;/em&gt; under routes pointing to rails root&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;# config/routes.rb
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;SampleApp::Application.routes.draw do
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  mount API::Root =&amp;gt; &#39;/&#39;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;end&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;customize-json-api-errors&quot;&gt;Customize JSON API Errors&lt;/h2&gt;
&lt;p&gt;We can control the api raised errors and customize them so that response is in our own format whenever there are exceptions.&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/error_formatter.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ErrorFormatter&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;backtrace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;p&quot;&gt;&amp;#x7b;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:response_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;error&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;#x7d;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_json&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Now, you can plug this module inside &lt;em&gt;API::Root&lt;/em&gt;&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/root.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Grape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;#...&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;error_formatter&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ErrorFormatter&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;#...&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;You can override error formatter for particular api version. Let’s customize errors for &lt;em&gt;API::v1::Root&lt;/em&gt;:&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;#lib/api/v1/error_formatter.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;V1&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;ErrorFormatter&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;backtrace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;        &lt;span class=&quot;p&quot;&gt;&amp;#x7b;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:response_type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;error&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;#x7d;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_json&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;10&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;11&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;12&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/v1/root.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;13&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;14&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;V1&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;15&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Grape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;16&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;c1&quot;&gt;#...&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;17&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;error_formatter&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;V1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ErrorFormatter&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;18&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;c1&quot;&gt;#...&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;19&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;20&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;21&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;accessing-api-routes&quot;&gt;Accessing &lt;em&gt;API&lt;/em&gt; routes&lt;/h2&gt;
&lt;p&gt;If you do &lt;code&gt;rake routes | grep api&lt;/code&gt; then it will list only mount path for api but do not list all the paths.&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;rake routes | grep api
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    api_root        /api                API::Root&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;So, in-order to list all api paths, you may have to create api routes task:&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/tasks/routes.rake&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;n&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:api&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;API Routes&amp;quot;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:routes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:environment&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Root&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;nb&quot;&gt;method&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route_method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ljust&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route_path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gsub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;:version&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;route_version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;     &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#&amp;#x7b;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;&amp;#x7d;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#&amp;#x7b;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;&amp;#x7d;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;10&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;11&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Now, run task and it should print routes like this:&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;rake api:routes
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    GET        /api/v1/posts(.:format)
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    GET        /api/v1/authors(.:format)&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;securing-api&quot;&gt;Securing API&lt;/h2&gt;
&lt;p&gt;Now we have got Grape &lt;em&gt;API&lt;/em&gt; ready and working properly. Lets see how we can secure &lt;em&gt;API&lt;/em&gt;. There are many approaches to authenticate API. Here lets first get it working with simple &lt;em&gt;HTTP Basic authentication&lt;/em&gt;. &lt;/p&gt;

&lt;h4 id=&quot;http-basic-authentication&quot;&gt;HTTP Basic authentication&lt;/h4&gt;
&lt;p&gt;In our case, lets add basic authentication to the &lt;em&gt;API::Root&lt;/em&gt; and it will get applied to all versions of API.&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/root.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Grape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;#...&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;http_basic&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find_by_email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid_password?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;10&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;#...&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;11&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;12&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Requesting API using basic http auth credentials:&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;curl http://localhost:3000/api/products -u &quot;admin:secret&quot;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h4 id=&quot;authenticate-using-email-and-password&quot;&gt;Authenticate using email and password&lt;/h4&gt;
&lt;p&gt;Grape provides us with &lt;em&gt;before block&lt;/em&gt; inside that we can add authenctication code.&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# lib/api/root.rb&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Grape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;API&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;#...&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;before&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;n&quot;&gt;error!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;401 Unauthorized&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;401&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;authenticated&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;n&quot;&gt;helpers&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;10&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;authenticated&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;11&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find_by_email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:email&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;12&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;        &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid_password?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:password&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;13&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;14&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;15&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;c1&quot;&gt;#...&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;16&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;17&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

</description>
        <pubDate>Wed, 26 Mar 2014 12:39:27 +0530</pubDate>
        <link>http://funonrails.com/2014/03/building-restful-api-using-grape-in-rails</link>
        <guid isPermaLink="true">http://funonrails.com/2014/03/building-restful-api-using-grape-in-rails</guid>
        
        
        <category>restful</category>
        
        <category>api</category>
        
        <category>grape</category>
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>Understanding RESTful vs SOAP web services</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;A Web service&lt;/strong&gt; is a method of communications between two electronic devices over the World Wide Web.&lt;br /&gt;
It is also defined as a software system designed to support &lt;a href=&quot;http://en.wikipedia.org/wiki/Interoperability&quot;&gt;interoperable&lt;/a&gt; machine-to-machine interaction over a network. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simple words, “Web services are developed to use by other software applications”&lt;/p&gt;

&lt;p&gt;Web services communicate via either SOAP or REST messages.&lt;/p&gt;

&lt;h2 id=&quot;rest&quot;&gt;REST&lt;/h2&gt;
&lt;p&gt;REST stands for REpresentational State Transfer. It is a term coined by Roy Fielding in his Ph.D. dissertation &lt;br /&gt;
It is an architecture style for designing networked applications which typycally runs runs over HTTP.&lt;/p&gt;

&lt;p&gt;REST is an architecture and not a “standard”. There is no w3c recommendation for REST. It is concept for better web and widely accepted.&lt;/p&gt;

&lt;p&gt;RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.&lt;/p&gt;

&lt;p&gt;It expects client to have knowlege of what to send and what to expect in response. It is focused on accessing named resources through a single consistent interface.&lt;/p&gt;

&lt;h4 id=&quot;highlights&quot;&gt;Highlights:&lt;/h4&gt;

&lt;ol&gt;
  &lt;li&gt;It is simple &lt;/li&gt;
  &lt;li&gt;It is lightweight and faster &lt;/li&gt;
  &lt;li&gt;Platform-independent &lt;/li&gt;
  &lt;li&gt;Language-independent &lt;/li&gt;
  &lt;li&gt;runs over HTTP &lt;/li&gt;
  &lt;li&gt;Response can be returned in “XML/JSON” format&lt;/li&gt;
&lt;/ol&gt;

&lt;!--more--&gt;
&lt;p&gt;Like Web Services, REST offers no built-in security features, encryption, session management, QoS guarantees, etc. But also as with Web Services, these can be added by building on top of HTTP:&lt;/p&gt;

&lt;p&gt;For security, username/password tokens are often used.&lt;br /&gt;
For encryption, REST can be used on top of HTTPS (secure sockets).&lt;/p&gt;

&lt;h4 id=&quot;example&quot;&gt;Example:&lt;/h4&gt;
&lt;p&gt;The Web is comprised of resources. A resource is any item of interest. For example, A software company may have many products. Clients may access particular product with this URL:&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;http://www.&amp;lt;company&amp;gt;.com/products/7&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;soap&quot;&gt;SOAP&lt;/h2&gt;
&lt;p&gt;SOAP, originally defined as Simple Object Access Protocol. It is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks.&lt;/p&gt;

&lt;p&gt;SOAP is an XML based protocol for accessing Web Services. &lt;br /&gt;
It is designed to handle distributed computing environments.&lt;/p&gt;

&lt;p&gt;It is well defined mechanism for describing the interface e.g. WSDL+XSD, WS-Policy. As it is standardised it becomes complex t&lt;br /&gt;
It provides a large number of supporting standards for security, reliability, transactions.&lt;/p&gt;

&lt;p&gt;Here content of the message decides what to perform and reponse (payload) must comply with SOAP schema.&lt;/p&gt;

&lt;h4 id=&quot;example-1&quot;&gt;Example&lt;/h4&gt;

&lt;p&gt;It defines 3 fundamental properties:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;What service does: Operations provides&lt;/li&gt;
  &lt;li&gt;How service is accessed: Data format and protocol details&lt;/li&gt;
  &lt;li&gt;Where a service is located: Adress (URL) details&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;sample-soap-request-structure&quot;&gt;Sample SOAP request structure:&lt;/h4&gt;
&lt;p&gt;In the example below, a GetStockPrice request is sent to a server. The request has a StockName parameter, and a Price parameter that will be returned in the response. The namespace for the function is defined in “http://www.example.org/stock”.&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;POST /InStock HTTP/1.1
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;Host: www.example.org
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;Content-Type: application/soap+xml; charset=utf-8
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;Content-Length: 299
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;SOAPAction: &amp;quot;http://www.w3.org/2003/05/soap-envelope&amp;quot;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!- -&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot;?&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;soap:Envelope&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:soap=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;http://www.w3.org/2003/05/soap-envelope&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;nt&quot;&gt;&amp;lt;soap:Header&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;10&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/soap:Header&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;11&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;nt&quot;&gt;&amp;lt;soap:Body&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;12&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;m:GetStockPrice&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xmlns:m=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;http://www.example.org/stock&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;13&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;      &lt;span class=&quot;nt&quot;&gt;&amp;lt;m:StockName&amp;gt;&lt;/span&gt;IBM&lt;span class=&quot;nt&quot;&gt;&amp;lt;/m:StockName&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;14&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/m:GetStockPrice&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;15&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/soap:Body&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;16&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;/soap:Envelope&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h4 id=&quot;sample-soap-response-structure&quot;&gt;Sample SOAP response structure:&lt;/h4&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;HTTP/1.1 200 OK
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;Content-Type: application/soap+xml; charset=utf-8
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;Content-Length: nnn
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&amp;lt;soap:Envelope
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;xmlns:soap=&quot;http://www.w3.org/2001/12/soap-envelope&quot;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;soap:encodingStyle=&quot;http://www.w3.org/2001/12/soap-encoding&quot;&amp;gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;10&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&amp;lt;soap:Body xmlns:m=&quot;http://www.example.org/stock&quot;&amp;gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;11&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &amp;lt;m:GetStockPriceResponse&amp;gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;12&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &amp;lt;m:Price&amp;gt;34.5&amp;lt;/m:Price&amp;gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;13&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &amp;lt;/m:GetStockPriceResponse&amp;gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;14&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&amp;lt;/soap:Body&amp;gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;15&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;16&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&amp;lt;/soap:Envelope&amp;gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h4 id=&quot;highlights-1&quot;&gt;Highlights&lt;/h4&gt;
&lt;p&gt;Here are few &lt;strong&gt;reasons&lt;/strong&gt; for which you may want to use SOAP: &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Platform and laguage independent  &lt;/li&gt;
  &lt;li&gt;Follows W3C standard  &lt;br /&gt;
WSDL (Web services description language) is a W3C standard based on xml and is used to describe web services.&lt;/li&gt;
  &lt;li&gt;Security  &lt;br /&gt;
It supports SSL (just like REST). It also provides a standard implementation of data integrity and data privacy. Being “Enterprise” it’s more secure.  &lt;/li&gt;
  &lt;li&gt;Atomic Transactions  &lt;br /&gt;
It supports ACID Transactions (WS- Atomic Transactions) so it can provide two phase commit over distributed transactional resources.  &lt;/li&gt;
  &lt;li&gt;Reliable Messaging   &lt;br /&gt;
It has successful/retry logic built in and provides end-to-end reliability even through SOAP intermediaries &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whenever complex or likely to change API getting developed then SOAP could be a good choice.&lt;/p&gt;

&lt;h4 id=&quot;references-used&quot;&gt;References used&lt;/h4&gt;
&lt;ol&gt;
  &lt;li&gt;http://rest.elkstein.org/&lt;/li&gt;
  &lt;li&gt;http://www.xfront.com/REST-Web-Services.html&lt;/li&gt;
  &lt;li&gt;http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm&lt;/li&gt;
  &lt;li&gt;http://www.w3schools.com/webservices/ws_soap_example.asp&lt;/li&gt;
  &lt;li&gt;http://www.slideshare.net/kverbert/soapbased-web-services&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Mon, 17 Mar 2014 15:09:33 +0530</pubDate>
        <link>http://funonrails.com/2014/03/understanding-restful-vs-soap-web-services</link>
        <guid isPermaLink="true">http://funonrails.com/2014/03/understanding-restful-vs-soap-web-services</guid>
        
        
        <category>rest</category>
        
        <category>soap</category>
        
      </item>
    
      <item>
        <title>Migrating from blogspot to octopress</title>
        <description>&lt;p&gt;I was happily writing my blog at &lt;code&gt;blogspot&lt;/code&gt; almost three plus years. From last 2-3 days, i was evaluating with &lt;a href=&quot;http://octopress.org&quot;&gt;octopress&lt;/a&gt; for its way of writing posts, customizations, themes, and plugins. As a result of evaluation, found &lt;code&gt;octopress&lt;/code&gt; blog as very quick &amp;amp; easy to write hence decided to  &lt;em&gt;shift&lt;/em&gt; from &lt;em&gt;blogger&lt;/em&gt; to &lt;em&gt;octopress&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Below post explains steps involved in migration:&lt;/p&gt;

&lt;h2 id=&quot;export-content-from-blogger&quot;&gt;Export content from blogger&lt;/h2&gt;
&lt;p&gt;First, you need to export your posts from blogger.&lt;br /&gt;
Goto &lt;code&gt;blogger admin area&lt;/code&gt; and then go to &lt;code&gt;settings &amp;gt;&amp;gt; other &amp;gt;&amp;gt; Export Blog&lt;/code&gt; and click on it. Please wait sometime and popup will comeup allowing you to download blog in &lt;code&gt;xml&lt;/code&gt; format.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/export-blog.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you still finding some problem with exporting then see &lt;a href=&quot;http://www.freetech4teachers.com/2013/01/how-to-export-your-blogger-and.html&quot;&gt;here&lt;/a&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;

&lt;h2 id=&quot;formatting-posts-for-octopress&quot;&gt;Formatting posts for octopress&lt;/h2&gt;
&lt;p&gt;Now the big challenge ! We have &lt;code&gt;xml posts&lt;/code&gt; downloaded from blogger but octopress posts format is different.&lt;br /&gt;
So, we need to convert xml posts to markdown formatted octopress posts.&lt;br /&gt;
If you do it manually then its not an easy task but don’t worry. There is already a ruby code that will do it for you.&lt;br /&gt;
Thanks to &lt;a href=&quot;https://gist.github.com/juniorz&quot;&gt;Reinaldo de Souza &lt;/a&gt; for sharing the script. Download the &lt;code&gt;import.rb&lt;/code&gt; script from &lt;a href=&quot;https://gist.github.com/baldowl/1578928&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;run-the-script-against-downloaded-xml-file&quot;&gt;Run the script against downloaded xml file&lt;/h2&gt;
&lt;p&gt;Script requires &lt;code&gt;nokogiri&lt;/code&gt;. if you not don’t have already then install using &lt;code&gt;gem install nokogiri&lt;/code&gt;&lt;br /&gt;
After successful run, this will be creating static markdown octopress formatted posts inside &lt;code&gt;_posts&lt;/code&gt; and &lt;code&gt;_drafts&lt;/code&gt; directories.&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;ruby import.rb blog-02-09-2014.xml&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;h2 id=&quot;copy-generated-posts-to-octopress-blog-posts&quot;&gt;Copy generated posts to octopress blog posts&lt;/h2&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;mv _posts/* blog/_posts/`
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;mv _drafts/* blog/_drafts/`&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;This finishes migration and run &lt;code&gt;rake preview&lt;/code&gt; and you will start seeing your blogger posts.&lt;/p&gt;
</description>
        <pubDate>Sun, 09 Feb 2014 19:00:56 +0530</pubDate>
        <link>http://funonrails.com/2014/02/moving-from-blogspot-to-octopress</link>
        <guid isPermaLink="true">http://funonrails.com/2014/02/moving-from-blogspot-to-octopress</guid>
        
        
        <category>octopress,</category>
        
        <category>blogger</category>
        
      </item>
    
      <item>
        <title>Writing Octopress blog posts using markdown</title>
        <description>&lt;p&gt;This is an example post which shows various markdown syntax that can be used while writing octopress markdown posts.&lt;br /&gt;
## Bold text&lt;br /&gt;
&lt;strong&gt;Hello Octopress!&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;blockquote&quot;&gt;Blockquote&lt;/h2&gt;
&lt;figure class=&quot;quote&quot;&gt;&lt;blockquote&gt;&lt;p&gt;Don’t stop when you’re tired, stop when you’re done.&lt;/p&gt;&lt;/blockquote&gt;&lt;figcaption class=&quot;quote-source&quot;&gt;&lt;span class=&quot;quote-author&quot;&gt;&lt;a class=&quot;quote-link&quot; href=&quot;http://twitter.com/TheJeetBanerjee&quot;&gt;@TheJeetBanerjee&lt;/a&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;
&lt;!--more--&gt;
&lt;figure class=&quot;quote&quot;&gt;&lt;blockquote&gt;&lt;p&gt;We have to go for what we think we’re fully capable of, not limit ourselves by what we’ve been in the past.&lt;/p&gt;&lt;/blockquote&gt;&lt;figcaption class=&quot;quote-source&quot;&gt;&lt;span class=&quot;quote-author&quot;&gt;@Vivek Paul &lt;/span&gt;&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2 id=&quot;ruby&quot;&gt;Ruby&lt;/h2&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;abc&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;    &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;hii&amp;#39;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;console&quot;&gt;Console&lt;/h2&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;$ sudo apt-get install cakePHP&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;$ git clone git@github.com:imathis/octopress.git # fork octopress&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;coffeescript&quot;&gt;Coffeescript&lt;/h2&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;.help&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;html&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;(?)&amp;quot;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;print something&amp;quot;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# sum function&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&lt;span class=&quot;nv&quot;&gt;sum = &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;(a, b) -&amp;gt;&lt;/span&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;gist-embedding&quot;&gt;Gist Embedding&lt;/h2&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;figcaption class=&quot;code-highlight-caption&quot;&gt;&lt;span class=&quot;code-highlight-caption-title&quot;&gt;multiple-ssh-config&lt;/span&gt;&lt;a class=&quot;code-highlight-caption-link&quot; href=&quot;https://gist.github.com/cc14d6039f1ff35b4be3&quot;&gt;link&lt;/a&gt;&lt;/figcaption&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;# Default GitHub user (sandip)
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;Host github.com
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  HostName github.com
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  User git
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  IdentityFile /home/sandip/.ssh/id_rsa
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;6&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;7&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;# github user (client)
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;8&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;Host github-client
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;9&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  HostName github.com
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;10&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  User git
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;11&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  IdentityFile /home/sandip/.ssh/id_rsa_client
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;12&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt; &lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;13&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;# github user (friend)
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;14&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;Host github-swapnil
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;15&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  HostName github.com
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;16&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  User git
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;17&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  IdentityFile /home/sandip/.ssh/id_dsa_swapnil
&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;include-code-snippets&quot;&gt;Include Code Snippets&lt;/h2&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&amp;#x7b;% include_code path/to/file [title] [lang:language] [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false] %&amp;#x7d;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;h2 id=&quot;selecting-tags-from-sentence&quot;&gt;Selecting tags from sentence&lt;/h2&gt;
&lt;p&gt;This is a &lt;code&gt;ruby&lt;/code&gt; and &lt;code&gt;rails&lt;/code&gt; blog&lt;/p&gt;

&lt;h2 id=&quot;pullquote&quot;&gt;Pullquote&lt;/h2&gt;
&lt;p&gt;## Images&lt;br /&gt;
&lt;img src=&quot;http://placekitten.com/890/280&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;angularjs-tutorial---youtube&quot;&gt;AngularJS Tutorial - YouTube&lt;/h2&gt;
&lt;p&gt;AngularJS is a client-side JavaScript framework.A video tutorial to help you get started with AngularJS.&lt;br /&gt;
&lt;a href=&quot;http://youtube.com/watch?v=WuiHuZq_cg4&quot;&gt;&lt;img src=&quot;http://img.youtube.com/vi/WuiHuZq_cg4/0.jpg&quot; alt=&quot;Angular JS&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;inline-html&quot;&gt;Inline HTML&lt;/h2&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&amp;lt;div class=&#39;well&#39;&amp;gt;
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;  All is well. Way to go !!
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;&amp;lt;/div&amp;gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

</description>
        <pubDate>Fri, 07 Feb 2014 19:20:07 +0530</pubDate>
        <link>http://funonrails.com/2014/02/writing-octopress-blog-posts-using-markdown</link>
        <guid isPermaLink="true">http://funonrails.com/2014/02/writing-octopress-blog-posts-using-markdown</guid>
        
        
        <category>octopress</category>
        
      </item>
    
      <item>
        <title>Getting started: Octopress powered and Github hosted blog</title>
        <description>&lt;p&gt;There are various blogging sites like &lt;code&gt;blogspot&lt;/code&gt;, &lt;code&gt;wordpress&lt;/code&gt;, &lt;code&gt;posterous&lt;/code&gt;, and many more.&lt;br /&gt;
Each has their own pros and cons and none of the engine gives us full power of ehhance our blogging experience.&lt;br /&gt;
Anyone who has atleast some technical knowledge can start with octopress powered blog. This gives us the power of freedom of doing anything.&lt;br /&gt;
So, Lets first understand about octopress framework and its features.&lt;/p&gt;

&lt;h2 id=&quot;octopress&quot;&gt;Octopress&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;Octopress is a framework designed by Brandon Mathis for Jekyll, the blog aware static site generator. If you wanted to know more then it can be found &lt;a href=&quot;http://octopress.org/2011/07/23/octopress-20-surfaces/&quot;&gt;here&lt;/a&gt;&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;octopress-setup&quot;&gt;Octopress Setup&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1.  Before starting make sure you have &lt;code&gt;git&lt;/code&gt; installed.&lt;/strong&gt;&lt;br /&gt;
If not then do install using,&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;sudo apt-get install git&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.  Install &lt;code&gt;ruby 1.9.3&lt;/code&gt; or higher&lt;/strong&gt;&lt;br /&gt;
Make git clone of octopress source code&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;git clone git://github.com/imathis/octopress.git octopress
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;cd octopress
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;bundle install&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;h2 id=&quot;installing-octopress-theme&quot;&gt;Installing Octopress theme&lt;/h2&gt;
&lt;p&gt;Install the default Octopress theme using&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;rake install&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;In order to install custom themes,&lt;br /&gt;
Please &lt;a href=&quot;http://opthemes.com/&quot;&gt;browse through themes&lt;/a&gt; and make a selection for your blog.&lt;br /&gt;
Once choice is made, you need to do &lt;code&gt;git clone&lt;/code&gt; of theme source and install it.&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;cd octopress
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;git clone https://github.com/kAworu/octostrap3 .themes/octostrap3
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;rake &#39;install[octostrap3]&#39;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;To genrate infrastrcture for your blog i.e. &lt;code&gt;layouts&lt;/code&gt;, &lt;code&gt;js&lt;/code&gt; and &lt;code&gt;css&lt;/code&gt; &lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;rake generate&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;Edit &lt;code&gt;_config.yml&lt;/code&gt; to configure blog &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;header&lt;/code&gt;, &lt;code&gt;metadata&lt;/code&gt;, &lt;code&gt;plugins&lt;/code&gt; and &lt;code&gt;3rd Party Settings&lt;/code&gt;.&lt;br /&gt;
more on configurations can be found &lt;a href=&quot;http://octopress.org/docs/configuring/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We are almost done, and now ready to create our first blog post -&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;rake new_post[“My first octopress post”]
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;mkdir -p source/_posts
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;Creating new post: source/_posts/2014-02-09-my-first-octopress-post.markdown&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;Now you can open a post inside editor and change as you wanted. More on writing markdown can be found &lt;a href=&quot;https://github.com/adam-p/markdown-here/wiki/Markdown-Here-Cheatsheet#wiki-html&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;http://daringfireball.net/projects/markdown/syntax&quot;&gt;here too&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well ! Now its time to see, how our blog looks like -&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;cd octopress
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;rake watch
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;rake preview&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;Visit browser &lt;code&gt;http://localhost:4000&lt;/code&gt; and you should be seeing blog running there :-)&lt;/p&gt;

&lt;h2 id=&quot;deployment&quot;&gt;Deployment&lt;/h2&gt;

&lt;p&gt;There are ways to deploy octopress blog. You can either deploy this blog on &lt;code&gt;heroku&lt;/code&gt; or on &lt;code&gt;github&lt;/code&gt; using github pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Heroku&lt;/strong&gt;:&lt;br /&gt;
As heroku provides free hosting (1 node) you may want to deploy at heroku.&lt;br /&gt;
More on this deployment can be found &lt;a href=&quot;http://def.reyssi.net/blog/2012/01/14/get-blogging-with-octopress-on-heroku/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. With Github Project Pages&lt;/strong&gt;:&lt;br /&gt;
Github provides free static website hosting using github pages and deployment is also very simple.&lt;/p&gt;

&lt;p&gt;Note: You should have a github repository called &lt;code&gt;username.github.io&lt;/code&gt;&lt;/p&gt;

&lt;figure class=&quot;code-highlight-figure&quot;&gt;&lt;div class=&quot;code-highlight&quot;&gt;&lt;pre class=&quot;code-highlight-pre&quot;&gt;&lt;div data-line=&quot;1&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;cd octopress
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;2&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;rake setup_github_pages
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;3&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;git remote add origin git@github.com:username/username.github.io.git
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;4&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;rake generate
&lt;/div&gt;&lt;/div&gt;&lt;div data-line=&quot;5&quot; class=&quot;code-highlight-row numbered&quot;&gt;&lt;div class=&quot;code-highlight-line&quot;&gt;rake deploy&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;p&gt;Visit &lt;code&gt;http://username.github.io&lt;/code&gt; to see your blog hosted on github.&lt;br /&gt;
Also, do not forget to commit and push your source code on github.&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;git add .
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;git commit -m ‘octopress first blog’
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;git push origin source&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;h2 id=&quot;adding-custom-domain&quot;&gt;Adding custom domain&lt;/h2&gt;
&lt;p&gt;First create a &lt;code&gt;CNAME&lt;/code&gt; file and add naked domain name to it.&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;echo ‘funonrails.com’ » source/CNAME&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;Then goto your domain provider website and point your &lt;code&gt;domain&lt;/code&gt; i.e A record to github ip address &lt;code&gt;204.232.175.78&lt;/code&gt;.&lt;br /&gt;
## Adding subdomain&lt;br /&gt;
If you want to point subdomain to github blog then -&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;echo ‘blog.sandipransing.in’ » source/CNAME&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;then go to your domain provider website and create a CNAME record for your &lt;code&gt;subdomain&lt;/code&gt; i.e. &lt;code&gt;blog&lt;/code&gt; pointing to &lt;code&gt;yourname.github.io.&lt;/code&gt;&lt;br /&gt;
If you want more help on this then please see &lt;a href=&quot;http://octopress.org/docs/deploying/github/&quot;&gt;here&lt;/a&gt; and also &lt;a href=&quot;http://help.github.com/pages/#custom_domains&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Fri, 07 Feb 2014 19:20:07 +0530</pubDate>
        <link>http://funonrails.com/2014/02/octopress-powered-and-github-hosted-blog</link>
        <guid isPermaLink="true">http://funonrails.com/2014/02/octopress-powered-and-github-hosted-blog</guid>
        
        
        <category>octopress</category>
        
        <category>github-pages</category>
        
      </item>
    
      <item>
        <title>what is new in rails 3</title>
        <description>&lt;p&gt;Rails 3 has released with various notable features and said that it’s one of the best version rails community has released so far.&lt;/p&gt;

&lt;p&gt;Highlights of major features:&lt;/p&gt;

&lt;h4 id=&quot;brand-new-router-with-an-emphasis-on-restful-declarations&quot;&gt;1. Brand new router with an emphasis on RESTful declarations&lt;/h4&gt;

&lt;h4 id=&quot;new-action-mailer-api-modeled-after-action-controller&quot;&gt;2. New Action Mailer API modeled after Action Controller&lt;/h4&gt;

&lt;h4 id=&quot;new-active-record-chainable-query-language-built-on-top-of-relational-algebra&quot;&gt;3. New Active Record chainable query language built on top of relational algebra&lt;/h4&gt;

&lt;h4 id=&quot;unobtrusive-javascript-helpers-with-drivers-for-prototype-jquery-and-more-coming-end-of-inline-js&quot;&gt;4. Unobtrusive JavaScript helpers with drivers for Prototype, jQuery, and more coming (end of inline JS)&lt;/h4&gt;

&lt;h4 id=&quot;explicit-dependency-management-with-bundler&quot;&gt;5. Explicit dependency management with Bundler&lt;/h4&gt;
</description>
        <pubDate>Sun, 17 Mar 2013 14:33:56 +0530</pubDate>
        <link>http://funonrails.com/2013/03/what-is-new-in-rails-3</link>
        <guid isPermaLink="true">http://funonrails.com/2013/03/what-is-new-in-rails-3</guid>
        
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>Dynamic conditions to rails associations</title>
        <description>&lt;p&gt;We all know that rails models associations gets defined while class definitions are loaded and once defined can’t be changed.&lt;/p&gt;

&lt;p&gt;But still you can make use of &lt;code&gt;block parameter&lt;/code&gt; to conditions to have &lt;code&gt;dynamic query conditions&lt;/code&gt; inside associations.&lt;/p&gt;

&lt;p&gt;Below line explains &lt;strong&gt;how to&lt;/strong&gt; define dynamic associations :&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;has_one&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:code_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:class_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Sequence&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:conditions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;kind = &quot;#{self.kind}&quot;&#39;&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;Please make a note that below code will not work :&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;has_one&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:code_sequence&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:class_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;Sequence&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:conditions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;proc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;kind = ?&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

</description>
        <pubDate>Thu, 25 Oct 2012 16:12:00 +0530</pubDate>
        <link>http://funonrails.com/2012/10/dynamic-conditions-to-rails-associations</link>
        <guid isPermaLink="true">http://funonrails.com/2012/10/dynamic-conditions-to-rails-associations</guid>
        
        
      </item>
    
      <item>
        <title>Upgrading from Rails 2.1.x to Rails 2.3.11</title>
        <description>&lt;p&gt;If your application is currently on any version of Rails 2.1.x,&lt;br /&gt;
Then following changes needs to be done for upgrading your application to Rails &lt;code&gt;2.3.11&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. First install Rails version &lt;code&gt;2.3.11&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;gem install rails -v2.3.11&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Freeze app ruby gems&lt;/strong&gt;&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;rake rails:freeze:gems&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;Hopefully it should work for you but it gave me following error&lt;br /&gt;
&amp;gt; undefined method `manage_gems’ for Gem:Module&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Create sample rails 2.3.11 app&lt;/strong&gt;&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;rails &lt;em&gt;2.3.11&lt;/em&gt; testsapp&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;!--more--&gt;
&lt;p&gt;Now, Copy all missing “config/initializers/*” files from newly created “testapp” to the application that is to be upgraded.&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;cp testapp/config/initializers/* config/initializers&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Change Rails version inside &lt;code&gt;environment.rb&lt;/code&gt; to &lt;code&gt;Rails 2.3.11&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;c1&quot;&gt;# Specifies gem version of Rails to use when vendor/rails is not present&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;no&quot;&gt;RAILS_GEM_VERSION&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;2.3.11&#39;&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Rename &lt;i&gt;app/controllers/application.rb&lt;/i&gt; file to &lt;i&gt;app/controllers/application_controller.rb&lt;/i&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OR&lt;/strong&gt;&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;rails:update:application_controller&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Start rails server and fix the issues one by one&lt;/strong&gt;&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;ruby script/server&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

</description>
        <pubDate>Wed, 04 Apr 2012 22:13:00 +0530</pubDate>
        <link>http://funonrails.com/2012/04/upgrading-from-rails-2-1-x-to-rails-2-3-11</link>
        <guid isPermaLink="true">http://funonrails.com/2012/04/upgrading-from-rails-2-1-x-to-rails-2-3-11</guid>
        
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>ruby enumerable &amp; to_proc (ampersond &amp; symbol shortcut)</title>
        <description>&lt;p&gt;Basically Enumerable mixin gives collection classes a variety of traverse, search, sort methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding ruby blocks&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Blocks are statements of code written in ruby. one can take them as similar to c language macro’s&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Different ways to define blocks&lt;/strong&gt;&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;proc&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;hello&quot;&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; hello&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;5&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;6&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;7&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;hello &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;8&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;9&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;sandip&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; hello sandip&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;10&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;11&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;proc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;12&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;sandip&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; sandip&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;!--more--&gt;
&lt;p&gt;&lt;strong&gt;Passing block to enumerator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets assume we have collection array of strings and we want to print it&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;hi&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;sandip&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;how&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;you&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;doing&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;?&#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;hi&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;sandip&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;how&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;you&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;doing&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;?&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;5&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;6&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;proc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;7&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#&amp;lt;Proc:0x00007f9d2be13140@(irb):89&amp;gt;&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;8&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;9&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt;&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;10&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;hi&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;11&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;sandip&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;12&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;how&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;13&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;you&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;14&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;doing&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;15&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;16&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;17&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt;&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;18&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;hi&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;19&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;sandip&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;20&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;how&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;21&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;you&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;22&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;doing&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;23&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding symbol#to_proc&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Symbol has method &lt;code&gt;to_proc&lt;/code&gt; which converts symbol to block where symbol is taken as method to be executed on first argument of proc&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;** How to_proc got implemented inside Symbol class**&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Symbol&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;to_proc&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;    &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shift&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;&lt;strong&gt;send&lt;/strong&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;5&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lets have some examples:&lt;/strong&gt;&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:even?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_proc&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# equivalent to proc {|a| a.even?} &lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; #&amp;lt;Proc:0x00007f9d2bddcb90@(irb):97&amp;gt; &lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;  
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is there any shortcut?&lt;/strong&gt;&lt;br /&gt;
Yes, there is shortcut to have block passed to enumerators on the fly using ampersand followed by colon (i.e. symbol)&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;  
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:even?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:even?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:even?&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; [false, true, false, false, false]  &lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;5&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;n&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:even?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;6&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; [false, true, false, false, false]&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some handy examples&lt;/strong&gt;&lt;br /&gt;
&lt;figure class=&quot;code-highlight-figure&quot;&gt;&amp;lt;div class=&#39;code-highlight&#39;&amp;gt;&amp;lt;pre class=&#39;code-highlight-pre&#39;&amp;gt;&amp;lt;div data-line=&#39;1&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:+&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; 78 &lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;2&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:+&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; 78 &lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;3&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;any?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:even?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; true &lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;4&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;67&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;detect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:even?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; 2 &lt;/span&gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div data-line=&#39;5&#39; class=&#39;code-highlight-row numbered&#39;&amp;gt;&amp;lt;div class=&#39;code-highlight-line&#39;&amp;gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&#39;ruby&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;on&#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#39;rails&#39;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:upcase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; [&quot;RUBY&quot;, &quot;ON&quot;, &quot;RAILS&quot;]&lt;/span&gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/div&amp;gt;&lt;/figure&gt;&lt;/p&gt;

</description>
        <pubDate>Thu, 23 Feb 2012 02:09:00 +0530</pubDate>
        <link>http://funonrails.com/2012/02/ruby-enumerable-toproc-ampersond-colon</link>
        <guid isPermaLink="true">http://funonrails.com/2012/02/ruby-enumerable-toproc-ampersond-colon</guid>
        
        
        <category>ruby</category>
        
        <category>blocks</category>
        
        <category>enumerable</category>
        
      </item>
    
  </channel>
</rss>
