<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
 
 <title>martinciu's dev blog</title>
 
 <link href="http://martinciu.com/" />
 <updated>2012-01-07T03:44:02-08:00</updated>
 <id>martinciu.com</id>

 
 <author>
   <name>Marcin Ciunelis</name>
   <email>marcin.ciunelis@gmail.com</email>
 </author>
 
 
 
 <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/martinciu" /><feedburner:info uri="martinciu" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
   <title>Deploying Hubot to Heroku like a boss</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/qpCT5mUJs-g/deploying-hubot-to-heroku-like-a-boss.html" />
   <updated>2011-11-01T00:00:00-07:00</updated>
   <id>martinciu.com/2011/11/deploying-hubot-to-heroku-like-a-boss</id>
   <summary>How to get running Hubot on heroku in the right way</summary>
   <content type="html">&lt;h4 id='what_is_hubot'&gt;What is Hubot?&lt;/h4&gt;

&lt;p&gt;&lt;a href='http://hubot.github.com/'&gt;Hubot&lt;/a&gt; was an almost mythical GitHub &lt;a href='http://campfirenow.com/'&gt;campfire&lt;/a&gt; bot. They use it for deploying, automate a lot of tasks, and to be a source of fun in the company. Was, because they &lt;a href='https://github.com/blog/968-say-hello-to-hubot'&gt;open sourced&lt;/a&gt; it some time age.&lt;/p&gt;

&lt;h4 id='hubot__heroku'&gt;Hubot &amp;amp; Heroku&lt;/h4&gt;

&lt;p&gt;When I decided to give hubot a try on &lt;a href='http://www.heroku.com/'&gt;Heroku&lt;/a&gt; I googled for it and found &lt;a href='https://github.com/github/hubot/wiki/Hubot-on-Heroku'&gt;a few&lt;/a&gt; &lt;a href='http://apocryph.org/2011/10/29/how-i-got-hubot-deployed-to-heroku/'&gt;tutorials&lt;/a&gt; and &lt;a href='http://jonmagic.com/blog/archives/2011/10/28/hipchat-hubot-and-me'&gt;blog posts&lt;/a&gt;. All of them advise to download (or clone) main &lt;a href='https://github.com/github/hubot'&gt;hubot repository&lt;/a&gt; and deploy it to heroku. In may opinion this is not the best way to do it. This post describes how to create separated deployable hubot application.&lt;/p&gt;

&lt;h4 id='tools_needed'&gt;Tools needed&lt;/h4&gt;

&lt;p&gt;You will need a &lt;a href='http://www.ruby-lang.org'&gt;ruby&lt;/a&gt;, &lt;a href='http://git-scm.com/'&gt;git&lt;/a&gt;, &lt;a href='http://nodejs.org/'&gt;node.js&lt;/a&gt;, &lt;a href='http://npmjs.org/'&gt;npm&lt;/a&gt; and a heroku gem installed. Ruby and git is pretty common. You will install heroku gem by:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;gem install heroku
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;then node.js with Homebrew&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;brew install node
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and npm&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;curl http://npmjs.org/install.sh | sh
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 id='things_done_locally'&gt;Things done locally&lt;/h4&gt;

&lt;p&gt;Clone &lt;a href='https://github.com/github/hubot'&gt;hubot repository&lt;/a&gt; and create a new directory that will deployed to heroku.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;git clone git://github.com/github/hubot.git
&lt;span class='nv'&gt;$ &lt;/span&gt;&lt;span class='nb'&gt;cd &lt;/span&gt;hubot
&lt;span class='nv'&gt;$ &lt;/span&gt;npm install    &lt;span class='c'&gt;# install all required dependencies&lt;/span&gt;
&lt;span class='nv'&gt;$ &lt;/span&gt;bin/hubot --create ../acmebot
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If you go to the created directory you should see file structure similar to this:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;&lt;span class='nb'&gt;cd&lt;/span&gt; ../acmebot
&lt;span class='nv'&gt;$ &lt;/span&gt;ls -l
~/www/blog/hubot/acmebot  
total 32
-rw-r--r--   1 martinciu  staff    36 31 paź 21:28 Procfile
-rw-r--r--   1 martinciu  staff  3411 31 paź 21:28 README.md
drwxr-xr-x   3 martinciu  staff   102 31 paź 21:28 bin
-rw-r--r--   1 martinciu  staff    56 31 paź 21:28 hubot-scripts.json
-rw-r--r--   1 martinciu  staff   518 31 paź 21:28 package.json
drwxr-xr-x  12 martinciu  staff   408 31 paź 21:28 scripts
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This will be your hubot application that you will deploy to heroku. First create a new git repository.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;git init .
&lt;span class='nv'&gt;$ &lt;/span&gt;git add .
&lt;span class='nv'&gt;$ &lt;/span&gt;git commit -m &lt;span class='s2'&gt;&amp;quot;initial commit&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now you can create a new heroku app.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;heroku create acmebot --stack cedar
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And now you can deploy your own hubot to heroku.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;git push heroku
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 id='heroku_configuration'&gt;Heroku configuration&lt;/h4&gt;

&lt;p&gt;It is deployed, but hubot won&amp;#8217;t join any &lt;a href='http://campfirenow.com/'&gt;campfire&lt;/a&gt; room because it doesn&amp;#8217;t know anything about it. You have to tell him what room(s) should he go to. You can do it by heroku configuration variables.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='c'&gt;# Campfire user&amp;#39;s token. You can find it on user&amp;#39;s profile pages.&lt;/span&gt;
&lt;span class='c'&gt;# You should probably have additional campfire user for a hubot &lt;/span&gt;
&lt;span class='nv'&gt;$ &lt;/span&gt;heroku config:add &lt;span class='nv'&gt;HUBOT_CAMPFIRE_TOKEN&lt;/span&gt;&lt;span class='o'&gt;=&lt;/span&gt;secret
&lt;span class='c'&gt;# room ids coma-separated (you can find room id in room URL)&lt;/span&gt;
&lt;span class='nv'&gt;$ &lt;/span&gt;heroku config:add &lt;span class='nv'&gt;HUBOT_CAMPFIRE_ROOMS&lt;/span&gt;&lt;span class='o'&gt;=&lt;/span&gt;123 
&lt;span class='c'&gt;# your campfire account subdoamin&lt;/span&gt;
&lt;span class='nv'&gt;$ &lt;/span&gt;heroku config:add &lt;span class='nv'&gt;HUBOT_CAMPFIRE_ACCOUNT&lt;/span&gt;&lt;span class='o'&gt;=&lt;/span&gt;&lt;span class='s2'&gt;&amp;quot;acme&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;For some scripts &lt;a href='http://redis.io/'&gt;Redis&lt;/a&gt; is required. You can add a free &lt;a href='https://redistogo.com/'&gt;RedisToGo&lt;/a&gt; service by typing:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;heroku addons:add redistogo:nano
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;All is set up. Now you can start hubot:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;heroku ps:scale &lt;span class='nv'&gt;app&lt;/span&gt;&lt;span class='o'&gt;=&lt;/span&gt;1 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 id='its_alive'&gt;It&amp;#8217;s alive!&lt;/h4&gt;

&lt;p&gt;Now if you go to you campfire room and type&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;Hubot &lt;span class='nb'&gt;help&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;you should get a list of commands that your Hubot is familiar with.&lt;/p&gt;

&lt;h4 id='when_it_is_not_alive_'&gt;When it is not alive :/&lt;/h4&gt;

&lt;p&gt;If hubot doesn&amp;#8217;t speak to you, it means that something went wrong. In that case you can check heroku logs by typing in console:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;heroku logs 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also check application status by typing:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;heroku ps 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 id='more_scripts'&gt;More scripts&lt;/h4&gt;

&lt;p&gt;You have just deployed a basic hubot set up. If you want to add more commands you can find them on the &lt;a href='https://github.com/github/hubot-scripts'&gt;hubot-scripts repository&lt;/a&gt;. It is already added to you your copy of hubot. To turn it on you should edit &lt;code&gt;hubot-scripts.json&lt;/code&gt; file. It is simple JSON file with list of custom scripts that should be loaded. At the time of writing some of the scripts that are available in the hubot-scripts repository are not yet available on the npm. So if your hubot doesn&amp;#8217;t start after adding some custom scripts, check it&amp;#8217;s log files to see what scripts can&amp;#8217;t be found.&lt;/p&gt;

&lt;h4 id='robots_name'&gt;Robot&amp;#8217;s name&lt;/h4&gt;

&lt;p&gt;Hubot only talks to you if you call him by name. And it is a new of the user who&amp;#8217;s token hubot uses. You can specify that name in the &lt;code&gt;Procfile&lt;/code&gt;&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='n'&gt;app&lt;/span&gt;&lt;span class='p'&gt;:&lt;/span&gt; &lt;span class='n'&gt;bin&lt;/span&gt;&lt;span class='o'&gt;/&lt;/span&gt;&lt;span class='n'&gt;hubot&lt;/span&gt; &lt;span class='o'&gt;--&lt;/span&gt;&lt;span class='n'&gt;adapter&lt;/span&gt; &lt;span class='n'&gt;campfire&lt;/span&gt; &lt;span class='o'&gt;--&lt;/span&gt;&lt;span class='nb'&gt;name&lt;/span&gt; &lt;span class='n'&gt;acmebot&lt;/span&gt; &lt;span class='o'&gt;--&lt;/span&gt;&lt;span class='n'&gt;enable&lt;/span&gt;&lt;span class='o'&gt;-&lt;/span&gt;&lt;span class='n'&gt;slash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And if you don&amp;#8217;t want to talk to hubot by name you can add &lt;code&gt;--enable-slash&lt;/code&gt; option. It will allow to replace robot&amp;#8217;s name with &lt;code&gt;/&lt;/code&gt;. Example:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;/mustache me lady gaga 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/qpCT5mUJs-g" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2011/11/deploying-hubot-to-heroku-like-a-boss.html</feedburner:origLink></entry>
 
 <entry>
   <title>How to add API throttle to your rails app</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/LZVoQhvUnY8/how-to-add-api-throttle-to-your-rails-app.html" />
   <updated>2011-08-08T00:00:00-07:00</updated>
   <id>martinciu.com/2011/08/how-to-add-api-throttle-to-your-rails-app</id>
   <summary>Simple API throttling with Rack::Throttle and Redis</summary>
   <content type="html">&lt;h4 id='does_your_app_have_api'&gt;Does your app have API?&lt;/h4&gt;

&lt;p&gt;Yes? That&amp;#8217;s awesome! But are you sure that it is safe? And I don&amp;#8217;t mean if you secured access with api_key, Basic HTTP or anything like this. I mean if it is safe for your severs? At &lt;a href='http://zubibu.com/'&gt;zubibu&lt;/a&gt; we are going to add API for updating items in customer shops. It is pretty simple and obvious method. But if some API client use it in bad way it can harm us. For example, if an online store with 400 thousands items decided to decrease all prices by 2% for each item, and if they implemented &lt;a href='http://zubibu.com/'&gt;zubibu&lt;/a&gt; API call after each item update - it could harm us. 400k requests in a very short period of time could be dangerous for many apps.&lt;/p&gt;

&lt;h4 id='how_to_protect_your_api'&gt;How to protect your API?&lt;/h4&gt;

&lt;p&gt;You can educate your clients :), you can implement queue for handling API calls on your side. But in most cases (or at least in case described above) queue should be done on the client side. To force implementing queue on the client side you could use API throttling. It should as light as possible and invoked as soon as possible in request processing. So the &lt;a href='http://rack.rubyforge.org/'&gt;Rack&lt;/a&gt; app sound perfect for this task. You can write your own api throttling app or you can use &lt;a href='https://github.com/datagraph/rack-throttle'&gt;rack-throttle&lt;/a&gt; gem. It is simple but has almost all required features and is pretty easy to extend. We at &lt;a href='http://zubibu.com/'&gt;zubibu&lt;/a&gt; needed some custom features, so we decided to extend &lt;code&gt;Rack::Throttle::Limiter&lt;/code&gt;. This is our implementation of API throttle. I home comments in code are enough to understand it.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='c1'&gt;# lib/api_defender.rb&lt;/span&gt;
&lt;span class='nb'&gt;require&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;rack/throttle&amp;#39;&lt;/span&gt;
&lt;span class='c1'&gt;# we limit daily API usage&lt;/span&gt;
&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;ApiDefender&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&lt;/span&gt; &lt;span class='no'&gt;Rack&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Throttle&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Daily&lt;/span&gt;

  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nf'&gt;initialize&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;app&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;host&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;port&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;db&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='no'&gt;YAML&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;load_file&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='no'&gt;File&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;dirname&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='bp'&gt;__FILE__&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='o'&gt;+&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;/../config/redis.yml&amp;#39;&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='no'&gt;Rails&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;env&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;split&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;:&amp;#39;&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;options&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='p'&gt;{&lt;/span&gt;
      &lt;span class='c1'&gt;# we already use Redis in our app, so we reuse it&amp;#39;s &lt;/span&gt;
      &lt;span class='c1'&gt;# config file here&lt;/span&gt;
      &lt;span class='ss'&gt;:cache&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='no'&gt;Redis&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;new&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='ss'&gt;:host&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='n'&gt;host&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='ss'&gt;:port&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='n'&gt;port&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='ss'&gt;:thread_safe&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='kp'&gt;true&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='ss'&gt;:db&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='n'&gt;db&lt;/span&gt;&lt;span class='p'&gt;),&lt;/span&gt;
      &lt;span class='c1'&gt;# if you use staging environment on the same redis server&lt;/span&gt;
      &lt;span class='c1'&gt;# it is good to have separete key prefix for this&lt;/span&gt;
      &lt;span class='ss'&gt;:key_prefix&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;zubibu:&lt;/span&gt;&lt;span class='si'&gt;#{&lt;/span&gt;&lt;span class='no'&gt;Rails&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;env&lt;/span&gt;&lt;span class='si'&gt;}&lt;/span&gt;&lt;span class='s2'&gt;:api_defender&amp;quot;&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt;
      &lt;span class='c1'&gt;# only 5000 request per day&lt;/span&gt;
      &lt;span class='ss'&gt;:max&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='mi'&gt;5000&lt;/span&gt;
    &lt;span class='p'&gt;}&lt;/span&gt;
    &lt;span class='vi'&gt;@app&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='vi'&gt;@options&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;app&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;options&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;

  &lt;span class='c1'&gt;# this method checks if request needs throttling. &lt;/span&gt;
  &lt;span class='c1'&gt;# If so, it increases usage counter and compare it with maximum &lt;/span&gt;
  &lt;span class='c1'&gt;# allowed API calls. Returns true if a request can be handled.&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nf'&gt;allowed?&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;request&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;need_defense?&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;request&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='p'&gt;?&lt;/span&gt; &lt;span class='n'&gt;cache_incr&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;request&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;=&lt;/span&gt; &lt;span class='n'&gt;max_per_window&lt;/span&gt; &lt;span class='p'&gt;:&lt;/span&gt; &lt;span class='kp'&gt;true&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;

  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nf'&gt;call&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;env&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;status&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;heders&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;body&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='k'&gt;super&lt;/span&gt;
    &lt;span class='n'&gt;request&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='no'&gt;Rack&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Request&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;new&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;env&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='c1'&gt;# just to be nice for our clients we inform them how many&lt;/span&gt;
    &lt;span class='c1'&gt;# requests remaining does they have&lt;/span&gt;
    &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;need_defense?&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;request&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
      &lt;span class='n'&gt;heders&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;X-RateLimit-Limit&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt;     &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;max_per_window&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;to_s&lt;/span&gt;
      &lt;span class='n'&gt;heders&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;X-RateLimit-Remaining&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='mi'&gt;0&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;max_per_window&lt;/span&gt; &lt;span class='o'&gt;-&lt;/span&gt; &lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;cache_get&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;cache_key&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;request&lt;/span&gt;&lt;span class='p'&gt;))&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;to_i&lt;/span&gt; &lt;span class='k'&gt;rescue&lt;/span&gt; &lt;span class='mi'&gt;1&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;max&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;to_s&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;
    &lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='n'&gt;status&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;heders&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='n'&gt;body&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;

  &lt;span class='c1'&gt;# rack-throttle can use many backends for storing request counter.&lt;/span&gt;
  &lt;span class='c1'&gt;# We use Redis only so we can use it&amp;#39;s features. In this case &lt;/span&gt;
  &lt;span class='c1'&gt;# key increase and key expiration&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nf'&gt;cache_incr&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;request&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;key&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;cache_key&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;request&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;count&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;cache&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;incr&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;key&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;cache&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;expire&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;key&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='mi'&gt;1&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;day&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;count&lt;/span&gt; &lt;span class='o'&gt;==&lt;/span&gt; &lt;span class='mi'&gt;1&lt;/span&gt;
    &lt;span class='n'&gt;count&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;

  &lt;span class='kp'&gt;protected&lt;/span&gt;
    &lt;span class='c1'&gt;# only API calls should be throttled&lt;/span&gt;
    &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nf'&gt;need_defense?&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='n'&gt;request&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
      &lt;span class='n'&gt;request&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;host&lt;/span&gt; &lt;span class='o'&gt;==&lt;/span&gt; &lt;span class='no'&gt;API_HOST&lt;/span&gt;
    &lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To add this to your application you to add &lt;a href='https://github.com/datagraph/rack-throttle'&gt;rack-throttle&lt;/a&gt; to your &lt;code&gt;Gemfile&lt;/code&gt;&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='c1'&gt;# Gemfile&lt;/span&gt;
&lt;span class='c1'&gt;#...&lt;/span&gt;
&lt;span class='n'&gt;gem&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;rack-throttle&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and run &lt;code&gt;bundle install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;after that add, &lt;code&gt;ApiDefender&lt;/code&gt; middleware to your app&amp;#8217;s rack stack. It should go as high in the stock as possible:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='c1'&gt;# config/applicaiton.rb&lt;/span&gt;
&lt;span class='nb'&gt;require&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;lib/api_defender&amp;#39;&lt;/span&gt;
&lt;span class='k'&gt;module&lt;/span&gt; &lt;span class='nn'&gt;SomeAwesomeApp&lt;/span&gt;
  &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Application&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&lt;/span&gt; &lt;span class='no'&gt;Rails&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Application&lt;/span&gt;
    &lt;span class='c1'&gt;# ...&lt;/span&gt;
    &lt;span class='n'&gt;config&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;middleware&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;insert_after&lt;/span&gt; &lt;span class='no'&gt;Rack&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Lock&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='no'&gt;ApiDefender&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And thats it! Now, when you access your API you should get response like this:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;curl -I http://api.someawesomeapp.com/whatever 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;HTTP/1.1 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;but if you exceed 5000 API calls you will get this response:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;HTTP/1.1 403 Forbidden
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 0
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And voilà, your API is more safe now!&lt;/p&gt;

&lt;h4 id='more_modifications'&gt;More modifications&lt;/h4&gt;

&lt;p&gt;I suggest you reading rack-throttle&amp;#8217;s &lt;a href='https://github.com/datagraph/rack-throttle/blob/master/README.md'&gt;README&lt;/a&gt; file as well as it&amp;#8217;s &lt;a href='https://github.com/datagraph/rack-throttle'&gt;source code&lt;/a&gt; to find out what else you could easily modify for your needs. For example your &lt;code&gt;ApiDefence&lt;/code&gt; middleware could extend &lt;code&gt;Rack::Throttle::Hourly&lt;/code&gt; instead of &lt;code&gt;Rack::Throttle::Daily&lt;/code&gt;. Your could use different counter store, or identify your clients differently by overriding &lt;code&gt;client_identifier&lt;/code&gt; method.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/LZVoQhvUnY8" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2011/08/how-to-add-api-throttle-to-your-rails-app.html</feedburner:origLink></entry>
 
 <entry>
   <title>Upgrading to Rails 3.1.0.rc5 (from rc4)</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/-yxChMMGyYw/upgrading-to-rails-3-1-rc5.html" />
   <updated>2011-08-02T00:00:00-07:00</updated>
   <id>martinciu.com/2011/08/upgrading-to-rails-3-1-rc5</id>
   <summary>Upgrading from Rails 3.1.0.rc4 to 3.1.0.rc5 don't have to be simple</summary>
   <content type="html">&lt;h4 id='rails_310rc5'&gt;Rails 3.1.0.rc5&lt;/h4&gt;

&lt;p&gt;Rails 3.1.0.rc5 is &lt;a href='http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/ae3139e531b970a2'&gt;out&lt;/a&gt;. If your app doesn&amp;#8217;t use rails 3.1 yet you may want to read &lt;a href='http://davidjrice.co.uk/2011/05/25/how-to-upgrade-a-rails-application-to-version-3-1-0.html'&gt;this&lt;/a&gt; and probably &lt;a href='http://martinciu.com/2011/06/rails-3-1-and-slow-asset-pipeline.html'&gt;this&lt;/a&gt;. If you have balls and you already use version 3.1, you probably would like to use rc5 ASAP.&lt;/p&gt;

&lt;h4 id='surprise'&gt;Surprise&lt;/h4&gt;

&lt;p&gt;Alright RC releases could not be much different from each other. You may think that simple change in &lt;code&gt;Gemfile&lt;/code&gt; make you even more cool.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='n'&gt;gem&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;rails&amp;#39;&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;3.1.0.rc5&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Not this time :). After changing &lt;code&gt;Gemfile&lt;/code&gt; and running&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;bundle update rails
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;this line of my template:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='c1'&gt;# app/views/layouts/application.html.haml&lt;/span&gt;
&lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;stylesheet_link_tag&lt;/span&gt; &lt;span class='ss'&gt;:contents&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='ss'&gt;:media&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;screen,print&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;throws this error:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;No expansion found &lt;span class='k'&gt;for&lt;/span&gt; :contents
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 id='what_has_been_changed'&gt;What has been changed?&lt;/h4&gt;

&lt;p&gt;After each major rails upgrade it is always good to generate dummy app and look around what has been changed. I did it and found two little things.&lt;/p&gt;

&lt;p&gt;There is a new group in &lt;code&gt;Gemfile&lt;/code&gt; called &lt;code&gt;assets&lt;/code&gt;.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='c1'&gt;# Gems used only for assets and not required&lt;/span&gt;
&lt;span class='c1'&gt;# in production environments by default.&lt;/span&gt;
&lt;span class='n'&gt;group&lt;/span&gt; &lt;span class='ss'&gt;:assets&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt;
  &lt;span class='n'&gt;gem&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;sass-rails&amp;#39;&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;~&amp;gt; 3.1.0.rc&amp;quot;&lt;/span&gt;
  &lt;span class='n'&gt;gem&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;coffee-rails&amp;#39;&lt;/span&gt;&lt;span class='p'&gt;,&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;~&amp;gt; 3.1.0.rc&amp;quot;&lt;/span&gt;
  &lt;span class='n'&gt;gem&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;uglifier&amp;#39;&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And &lt;code&gt;config/application.rb&lt;/code&gt; file has been changed in the way how &lt;a href='http://gembundler.com/'&gt;bundler&lt;/a&gt; require gems:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='c1'&gt;# config/application.rb&lt;/span&gt;
&lt;span class='no'&gt;Bundler&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;require&lt;/span&gt; &lt;span class='o'&gt;*&lt;/span&gt;&lt;span class='no'&gt;Rails&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;groups&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='ss'&gt;:assets&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt; &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;defined?&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt;&lt;span class='no'&gt;Bundler&lt;/span&gt;&lt;span class='p'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;After implementing these changes my app is working under rails 3.1.0.rc5. Hooray!&lt;/p&gt;

&lt;p&gt;There may be other differences but none of them breaks my app.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/-yxChMMGyYw" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2011/08/upgrading-to-rails-3-1-rc5.html</feedburner:origLink></entry>
 
 <entry>
   <title>Difference between class_inheritable_attribute and class_attribute</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/dlZycsILjaE/difference-between-class_inheritable_attribute-and-class_attribute.html" />
   <updated>2011-07-07T00:00:00-07:00</updated>
   <id>martinciu.com/2011/07/difference-between-class_inheritable_attribute-and-class_attribute</id>
   <summary>What is the difference beetween between class_inheritable_attribute and class_attribute methods and how to get rid of rails 3.1 depracation warning</summary>
   <content type="html">&lt;h4 id='deprecation_warning'&gt;Deprecation warning&lt;/h4&gt;

&lt;p&gt;After you upgrade to Rails 3.1 you may be attacked by deprecation warnings similar to this one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;DEPRECATION WARNING: class_inheritable_attribute is deprecated, please use class_attribute method instead. Notice their behavior are slightly different, so refer to class_attribute documentation first. (called from included at base.rb:2)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In most case it will probably be all right to replace your all &lt;code&gt;class_inheritable_hash&lt;/code&gt;, &lt;code&gt;class_inheritable_array&lt;/code&gt;, etc method with &lt;code&gt;class_attribute&lt;/code&gt;. Unfortunately it won&amp;#8217;t always work. There is a &amp;#8220;slight&amp;#8221; difference mentioned by warning above.&lt;/p&gt;

&lt;h4 id='what_is_the_difference'&gt;What is the difference?&lt;/h4&gt;

&lt;p&gt;With &lt;code&gt;class_inheritable_hash&lt;/code&gt; (the old way):&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Base&lt;/span&gt;
  &lt;span class='n'&gt;class_inheritable_hash&lt;/span&gt; &lt;span class='ss'&gt;:mappings&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='p'&gt;{}&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;FirstChild&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&lt;/span&gt; &lt;span class='no'&gt;Base&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='ss'&gt;:foo&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='ss'&gt;:bar&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;SecondChild&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&lt;/span&gt; &lt;span class='no'&gt;Base&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='ss'&gt;:bar&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='ss'&gt;:baz&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='no'&gt;Base&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;           &lt;span class='c1'&gt;# {}&lt;/span&gt;
&lt;span class='no'&gt;FirstChild&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;     &lt;span class='c1'&gt;# {:foo=&amp;gt;:bar}&lt;/span&gt;
&lt;span class='no'&gt;SecondChild&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;    &lt;span class='c1'&gt;# {:bar=&amp;gt;:baz}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Alright, this is the expected behavior. How does this code works with &lt;code&gt;class_attribute&lt;/code&gt;? Let&amp;#8217;s see!&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Base&lt;/span&gt;
  &lt;span class='n'&gt;class_attribute&lt;/span&gt; &lt;span class='ss'&gt;:mappings&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='p'&gt;{}&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;FirstChild&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&lt;/span&gt; &lt;span class='no'&gt;Base&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='ss'&gt;:foo&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='ss'&gt;:bar&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;SecondChild&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&lt;/span&gt; &lt;span class='no'&gt;Base&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='ss'&gt;:bar&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='ss'&gt;:baz&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='no'&gt;Base&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;           &lt;span class='c1'&gt;# {:foo=&amp;gt;:bar, :bar=&amp;gt;:baz}&lt;/span&gt;
&lt;span class='no'&gt;FirstChild&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;     &lt;span class='c1'&gt;# {:foo=&amp;gt;:bar, :bar=&amp;gt;:baz}&lt;/span&gt;
&lt;span class='no'&gt;SecondChild&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;    &lt;span class='c1'&gt;# {:foo=&amp;gt;:bar, :bar=&amp;gt;:baz}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;It is far from the how it supposed to work. I would&amp;#8217;t say that &lt;code&gt;class_attribute&lt;/code&gt; behavior is &amp;#8220;slightly&amp;#8221; different than &amp;#8220;class_inheritable_hash&amp;#8221;. It is completely different method! But there is no place for complaining. We have to deal with it. How can we do it? Actually quite simple. Just call &lt;code&gt;dup&lt;/code&gt; method on an inheritable attribute:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Base&lt;/span&gt;
  &lt;span class='n'&gt;class_attribute&lt;/span&gt; &lt;span class='ss'&gt;:mappings&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='p'&gt;{}&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;FirstChild&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&lt;/span&gt; &lt;span class='no'&gt;Base&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;dup&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='ss'&gt;:foo&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='ss'&gt;:bar&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;SecondChild&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&lt;/span&gt; &lt;span class='no'&gt;Base&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;dup&lt;/span&gt;
  &lt;span class='nb'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='ss'&gt;:bar&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='ss'&gt;:baz&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;

&lt;span class='no'&gt;Base&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;           &lt;span class='c1'&gt;# {}&lt;/span&gt;
&lt;span class='no'&gt;FirstChild&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;     &lt;span class='c1'&gt;# {:foo=&amp;gt;:bar}&lt;/span&gt;
&lt;span class='no'&gt;SecondChild&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;mappings&lt;/span&gt;    &lt;span class='c1'&gt;# {:bar=&amp;gt;:baz}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Exactly as expected! Hooray!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/dlZycsILjaE" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2011/07/difference-between-class_inheritable_attribute-and-class_attribute.html</feedburner:origLink></entry>
 
 <entry>
   <title>Building your own Hoptoad app clone with Errbit, VMware Cloud Foundry and MongoDB</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/nkJ1fxDsQRM/building-your-own-hoptoad-app-clone-with-errbit-vmware-cloud-foundry-and-mongodb.html" />
   <updated>2011-07-01T00:00:00-07:00</updated>
   <id>martinciu.com/2011/07/building-your-own-hoptoad-app-clone-with-errbit-vmware-cloud-foundry-and-mongodb</id>
   <summary>A simple Getting Started Tutorial VMware Cloud Foundry.</summary>
   <content type="html">&lt;h4 id='disclaimer'&gt;Disclaimer&lt;/h4&gt;

&lt;p&gt;Please note that &lt;a href='http://cloudfoundry.com/' title='Cloud Foundry website'&gt;VMware Cloud Foundry&lt;/a&gt; is under heavy &lt;a href='https://github.com/cloudfoundry'&gt;development&lt;/a&gt; right now. It means that some of the things written below might not be valid at time you are reading it. Please leave a comment if you find any outdated stuff.&lt;/p&gt;

&lt;h4 id='errbit'&gt;Errbit&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href='https://github.com/jdpace/errbit'&gt;Errbit&lt;/a&gt; is an open source, self-hosted error catcher. It is &lt;a href='http://hoptoadapp.com/pages/home'&gt;Hoptoad&lt;/a&gt; API compliant so you can just point the &lt;a href='http://hoptoadapp.com/pages/home'&gt;Hoptoad&lt;/a&gt; notifier at your Errbit server if you are already using &lt;a href='http://hoptoadapp.com/pages/home'&gt;Hoptoad&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because Errbit is self-hosted solution you need to have a decent server to run it. You can run it on you own server, you can run it on &lt;a href='http://www.heroku.com/'&gt;heroku&lt;/a&gt; - details in Errbit&amp;#8217;s &lt;a href='https://github.com/relevance/errbit/blob/master/README.md'&gt;Readme&lt;/a&gt; or you can use Cloud Foundry. This post is a simple tutorial how to do it.&lt;/p&gt;

&lt;h4 id='what_is_vmware_cloud_foundry'&gt;What is VMware Cloud Foundry&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Cloud Foundry is the open platform as a service project initiated by VMware. It can support multiple frameworks, multiple cloud providers, and multiple application services all on a cloud scale platform.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can download from Cloud Foundry from GitHub &lt;a href='https://github.com/cloudfoundry'&gt;repository&lt;/a&gt; and set up your own cloud infrastructure or you can use hosted by VMware one. In this tutorial we use hosted one.&lt;/p&gt;

&lt;h4 id='cloud_foundry_account_and_vmc_tools'&gt;Cloud Foundry account and VMC tools&lt;/h4&gt;

&lt;p&gt;At first we need a &lt;a href='http://cloudfoundry.com/signup'&gt;Cloud Foundry account&lt;/a&gt;. They was at closed beta at the time I sign up. I had to wait a few days to get an account. I don&amp;#8217;t know if it still apply. After you get an account install Cloud Foundry &lt;a href='http://support.cloudfoundry.com/entries/20012337-getting-started-guide-command-line-vmc-users'&gt;VMC tools&lt;/a&gt;. VMC is a command line client for Cloud Foundry.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;gem install vmc
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Then log in to Cloud Foundry using credentials.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;vmc login  
Email: marcin.ciunelis@gmail.com
Password: ******
Successfully logged into &lt;span class='o'&gt;[&lt;/span&gt;http://api.cloudfoundry.com&lt;span class='o'&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You may want to change automatically generated password. You can do that using VMC tools:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;vmc passwd  
Changing password &lt;span class='k'&gt;for&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;marcin.ciunelis@gmail.com&amp;#39;&lt;/span&gt;
New Password: ******
Verify Password: ******

Successfully changed password
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4 id='errbit'&gt;Errbit&lt;/h4&gt;

&lt;p&gt;&lt;a href='https://github.com/jdpace/errbit'&gt;Errbit&lt;/a&gt; will not work out of the box on Cloud Foundry. We need to modify it a little. I won&amp;#8217;t write here how to set up Errbit on your local box. You can find it on project &lt;a href='https://github.com/relevance/errbit/blob/master/README.md'&gt;README&lt;/a&gt; file. I&amp;#8217;m going to focus on Cloud Foundry specific issues mainly. First, clone it from GitHub repository.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;git clone git://github.com/jdpace/errbit.git
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;When you open &lt;code&gt;Gemfile&lt;/code&gt; you will notice that &lt;code&gt;redmine_client&lt;/code&gt; gem is taken from git repository, not from official gem release because official one does not work with Rails3. If you are not going to use &lt;a href='http://www.redmine.org/'&gt;Redmine&lt;/a&gt; integration you can safely comment that line out. If want to use &lt;a href='http://www.redmine.org/'&gt;Redmine&lt;/a&gt; you we will need to figure out something smarter :)&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='c1'&gt;# Gemfile&lt;/span&gt;
&lt;span class='n'&gt;gem&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;lighthouse-api&amp;#39;&lt;/span&gt;
&lt;span class='c1'&gt;# gem &amp;#39;redmine_client&amp;#39;, :git =&amp;gt; &amp;quot;git://github.com/oruen/redmine_client.git&amp;quot;&lt;/span&gt;
&lt;span class='n'&gt;gem&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;mongoid_rails_migrations&amp;#39;&lt;/span&gt;
&lt;span class='c1'&gt;#...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The other thing that Cloud Foundry currently does not support is running &lt;code&gt;rake&lt;/code&gt; task. Errbit requires running &lt;code&gt;rake errbit:bootstrap&lt;/code&gt; which copy &lt;code&gt;config/config.yml&lt;/code&gt;, &lt;code&gt;config/mongoid.yml&lt;/code&gt; files and seed the database. You can run this task on your local box to create config files.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;rake errbit:bootstrap
Copying example config files...
-- Copying config/config.example.yml to config/config.yml
-- Copying config/deploy.example.rb to config/deploy.rb
-- Copying config/mongoid.example.yml to config/mongoid.yml

Seeding database
-------------------------------
Creating an initial admin user:
-- email:    errbit@errbit.example.com
-- password: password

Be sure to change these credentials ASAP!

Generating indexes &lt;span class='k'&gt;for &lt;/span&gt;App
Generating indexes &lt;span class='k'&gt;for &lt;/span&gt;Err
Generating indexes &lt;span class='k'&gt;for &lt;/span&gt;Notice
Generating indexes &lt;span class='k'&gt;for &lt;/span&gt;User
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Edit &lt;code&gt;config/config.yml&lt;/code&gt; file amending &lt;code&gt;host&lt;/code&gt; (it must be uniqe) and &lt;code&gt;email_from&lt;/code&gt; lines. Also production section in your &lt;code&gt;config/mongoid.yml&lt;/code&gt; file should looks like this:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='erb'&gt;&lt;span class='x'&gt;production:&lt;/span&gt;
&lt;span class='x'&gt;  host: &lt;/span&gt;&lt;span class='cp'&gt;&amp;lt;%=&lt;/span&gt; &lt;span class='no'&gt;JSON&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;parse&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt; &lt;span class='no'&gt;ENV&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;VCAP_SERVICES&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;mongodb-1.8&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;first&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;credentials&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;][&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;hostname&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='k'&gt;rescue&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;localhost&amp;#39;&lt;/span&gt; &lt;span class='cp'&gt;%&amp;gt;&lt;/span&gt;&lt;span class='x' /&gt;
&lt;span class='x'&gt;  port: &lt;/span&gt;&lt;span class='cp'&gt;&amp;lt;%=&lt;/span&gt; &lt;span class='no'&gt;JSON&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;parse&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt; &lt;span class='no'&gt;ENV&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;VCAP_SERVICES&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;mongodb-1.8&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;first&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;credentials&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;][&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;port&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='k'&gt;rescue&lt;/span&gt; &lt;span class='mi'&gt;27017&lt;/span&gt; &lt;span class='cp'&gt;%&amp;gt;&lt;/span&gt;&lt;span class='x' /&gt;
&lt;span class='x'&gt;  database:  &lt;/span&gt;&lt;span class='cp'&gt;&amp;lt;%=&lt;/span&gt; &lt;span class='no'&gt;JSON&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;parse&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt; &lt;span class='no'&gt;ENV&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;VCAP_SERVICES&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;mongodb-1.8&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;first&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;credentials&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;][&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;db&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='k'&gt;rescue&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;errbit_development&amp;#39;&lt;/span&gt; &lt;span class='cp'&gt;%&amp;gt;&lt;/span&gt;&lt;span class='x' /&gt;
&lt;span class='x'&gt;  username: &lt;/span&gt;&lt;span class='cp'&gt;&amp;lt;%=&lt;/span&gt; &lt;span class='no'&gt;JSON&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;parse&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt; &lt;span class='no'&gt;ENV&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;VCAP_SERVICES&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;mongodb-1.8&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;first&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;credentials&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;][&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;username&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='k'&gt;rescue&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;&amp;#39;&lt;/span&gt; &lt;span class='cp'&gt;%&amp;gt;&lt;/span&gt;&lt;span class='x' /&gt;
&lt;span class='x'&gt;  password: &lt;/span&gt;&lt;span class='cp'&gt;&amp;lt;%=&lt;/span&gt; &lt;span class='no'&gt;JSON&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;parse&lt;/span&gt;&lt;span class='p'&gt;(&lt;/span&gt; &lt;span class='no'&gt;ENV&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;VCAP_SERVICES&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='p'&gt;)&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;mongodb-1.8&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;first&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;credentials&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;][&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;password&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='k'&gt;rescue&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;&amp;#39;&lt;/span&gt; &lt;span class='cp'&gt;%&amp;gt;&lt;/span&gt;&lt;span class='x' /&gt;
&lt;span class='x'&gt;  &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Because we use JSON here to decode mongodb configuration, you will add the following line to your &lt;code&gt;Gemfile&lt;/code&gt;&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='n'&gt;gem&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;json&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and run &lt;code&gt;bundle install&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;To seed database we need to use a little trick. Errbit already use &lt;code&gt;mongoid_rails_migrations&lt;/code&gt; so we can use rails migration to do this. Generate migration:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;rails generate migration seed_database
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and modify to look like this:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;SeedDatabase&lt;/span&gt; &lt;span class='o'&gt;&amp;lt;&lt;/span&gt; &lt;span class='no'&gt;Mongoid&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Migration&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nc'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='nf'&gt;up&lt;/span&gt;
    &lt;span class='no'&gt;Rake&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Task&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;db:seed&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;invoke&lt;/span&gt;
    &lt;span class='no'&gt;Rake&lt;/span&gt;&lt;span class='o'&gt;::&lt;/span&gt;&lt;span class='no'&gt;Task&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='s1'&gt;&amp;#39;db:mongoid:create_indexes&amp;#39;&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;invoke&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;

  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='nc'&gt;self&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='nf'&gt;down&lt;/span&gt;
  &lt;span class='k'&gt;end&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;One more thing before deploying. Errbit will send an email when error occur in your app. So you should have an valid delivery_method configured. It may be your own SMTP server, &lt;a href='http://aws.amazon.com/ses/'&gt;Amazon SES&lt;/a&gt; or email app like &lt;a href='http://sendgrid.com/'&gt;Sendgrid&lt;/a&gt; or &lt;a href='http://postmarkapp.com/'&gt;PostmarkApp&lt;/a&gt;&lt;/p&gt;

&lt;h4 id='deploy'&gt;Deploy&lt;/h4&gt;

&lt;p&gt;To run Errbit we need two servers. One for web-server and one for MongoDB database. Setting this up is really simple. To setup the app and database server type:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;vmc push errbit --path&lt;span class='o'&gt;=&lt;/span&gt;. --url&lt;span class='o'&gt;=&lt;/span&gt;martinciu-errbit.cloudfoundry.com --mem&lt;span class='o'&gt;=&lt;/span&gt;128M --runtime&lt;span class='o'&gt;=&lt;/span&gt;ruby19
Detected a Rails Application, is this correct? &lt;span class='o'&gt;[&lt;/span&gt;Yn&lt;span class='o'&gt;]&lt;/span&gt;: 
Creating Application: OK
Would you like to &lt;span class='nb'&gt;bind &lt;/span&gt;any services to &lt;span class='s1'&gt;&amp;#39;errbit&amp;#39;&lt;/span&gt;? &lt;span class='o'&gt;[&lt;/span&gt;yN&lt;span class='o'&gt;]&lt;/span&gt;: y
The following system services are available::
1. mongodb
2. mysql
3. redis
Please &lt;span class='k'&gt;select &lt;/span&gt;one you wish to provision: 1
Specify the name of the service &lt;span class='o'&gt;[&lt;/span&gt;mongodb-e776e&lt;span class='o'&gt;]&lt;/span&gt;: 
Creating Service: OK
Binding Service: OK
Uploading Application:
  Checking &lt;span class='k'&gt;for &lt;/span&gt;available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading &lt;span class='o'&gt;(&lt;/span&gt;9K&lt;span class='o'&gt;)&lt;/span&gt;: OK   
Push Status: OK
Staging Application: OK                                                         
Starting Application: OK
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;--url&lt;/code&gt; should be same as the one you entered in &lt;code&gt;config/config.yml&lt;/code&gt; file and it should be unique. That&amp;#8217;s it you should have now a working application. You can check working apps by&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;vmc apps

+-------------+----+---------+-----------------------------------+---------------+
| Application | &lt;span class='c'&gt;#  | Health  | URLS                              | Services      |&lt;/span&gt;
+-------------+----+---------+-----------------------------------+---------------+
| errbit      | 1  | RUNNING | martinciu-errbit.cloudfoundry.com | mongodb-e776e |
+-------------+----+---------+-----------------------------------+---------------+
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;It also shows connected services - mongodb here. You can now visit your errbit app in your browser. You should see a log in screen, but you can not log in because migrations was not run. Cloud Foundry runs migration only if &lt;code&gt;config/database.yml&lt;/code&gt; exists. We don&amp;#8217;t have such file because mongoid uses &lt;code&gt;config/mongid.yml&lt;/code&gt; file by default. To force running migration just create an empty &lt;code&gt;database.yml&lt;/code&gt; file and update app:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;touch config/database.yml
&lt;span class='nv'&gt;$ &lt;/span&gt;vmc update errbit
Uploading Application:
  Checking &lt;span class='k'&gt;for &lt;/span&gt;available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading &lt;span class='o'&gt;(&lt;/span&gt;9K&lt;span class='o'&gt;)&lt;/span&gt;: OK   
Push Status: OK
Stopping Application: OK
Staging Application: OK                                                         
Starting Application: OK
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Sometimes update doesn&amp;#8217;t success. In that case stopping and starting service may help.&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;vmc stop errbit  
Stopping Application: OK

&lt;span class='nv'&gt;$ &lt;/span&gt;vmc start errbit  
Staging Application: OK                                                         
Starting Application: OK
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Alright you should be able to log in to your own error catcher app. Just visit chosen URL (http://martinciu-errbit.cloudfoundry.com/ in my case) and log in with default credentials which are email: &lt;code&gt;errbit@your-app-url.cloudfoundry.com&lt;/code&gt; and password: &lt;code&gt;password&lt;/code&gt;. You may want to change these default. You can do this by clicking &lt;code&gt;Edit profile&lt;/code&gt; button.&lt;/p&gt;

&lt;h4 id='configuration'&gt;Configuration&lt;/h4&gt;

&lt;p&gt;You can now configure your production app that you want be monitored by Errbit. Errbit is compatible with &lt;a href='https://github.com/thoughtbot/hoptoad_notifier'&gt;hoptoad_notifier&lt;/a&gt; gem, so if you are familiar with this it should be pretty obvious to you. If you don&amp;#8217;t use hoptoad you should add it to your &lt;code&gt;Gemgile&lt;/code&gt;&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='n'&gt;gem&lt;/span&gt; &lt;span class='s2'&gt;&amp;quot;hoptoad_notifier&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and run &lt;code&gt;bundle install&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;You can create new app on the homepage, define who should receive notifications and create a &lt;code&gt;config/errbit.rb&lt;/code&gt; file from the code provided by Errbit. In my case this file looks like this:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;&lt;span class='no'&gt;HoptoadNotifier&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;configure&lt;/span&gt; &lt;span class='k'&gt;do&lt;/span&gt; &lt;span class='o'&gt;|&lt;/span&gt;&lt;span class='n'&gt;config&lt;/span&gt;&lt;span class='o'&gt;|&lt;/span&gt;
  &lt;span class='n'&gt;config&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;api_key&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;3aa6c74ccabaf61295c1d10813575705&amp;#39;&lt;/span&gt;
  &lt;span class='n'&gt;config&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;host&lt;/span&gt;    &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='s1'&gt;&amp;#39;martinciu-errbit.cloudfoundry.com&amp;#39;&lt;/span&gt;
  &lt;span class='n'&gt;config&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;port&lt;/span&gt;    &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='mi'&gt;80&lt;/span&gt;
  &lt;span class='n'&gt;config&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;secure&lt;/span&gt;  &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;config&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;port&lt;/span&gt; &lt;span class='o'&gt;==&lt;/span&gt; &lt;span class='mi'&gt;443&lt;/span&gt;
&lt;span class='k'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Unfortunately Errbit overwrites default &lt;a href='https://github.com/thoughtbot/hoptoad_notifier'&gt;hoptoad_notifier&lt;/a&gt; settings, so you can not use both error catchers at same time. Alright you should now be able to send a test error notification. Just go to you app folder and run:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='bash'&gt;&lt;span class='nv'&gt;$ &lt;/span&gt;rake hoptoad:test
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You should a lot of dumped XML data and a while later you should receive email notification from Errbit. You can also review the test error on your errbit app. And that it! You have up and running your own Hoptoad clone!&lt;/p&gt;

&lt;h4 id='something_went_wrong'&gt;Something went wrong?&lt;/h4&gt;

&lt;p&gt;If something went wrong (it probably will :P) you can find these tools and resources useful:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;vmc logs errbit&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;&lt;code&gt;vmc files errbit logs&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;&lt;code&gt;vmc help&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;Cloud Foundry &lt;a href='http://support.cloudfoundry.com/home'&gt;Forum&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Cloud Foundry &lt;a href='https://github.com/cloudfoundry'&gt;source code&lt;/a&gt; on GitHub&lt;/li&gt;

&lt;li&gt;Errbit &lt;a href='https://github.com/jdpace/errbit'&gt;source code&lt;/a&gt; on GitHub&lt;/li&gt;
&lt;/ol&gt;&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/nkJ1fxDsQRM" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2011/07/building-your-own-hoptoad-app-clone-with-errbit-vmware-cloud-foundry-and-mongodb.html</feedburner:origLink></entry>
 
 <entry>
   <title>Rails 3.1 and slow Asset Pipeline</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/xZ7O_93YkFc/rails-3-1-and-slow-asset-pipeline.html" />
   <updated>2011-06-25T00:00:00-07:00</updated>
   <id>martinciu.com/2011/06/rails-3-1-and-slow-asset-pipeline</id>
   <summary>Is your application extremely slow after upgrading to Rails 3.1? It's (probably) your fault!</summary>
   <content type="html">&lt;h4&gt;tl;dr;&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Rails 3.1 Asset Pipeline is not slow! It is your app!&lt;/strong&gt; If your app became slow after upgrading to Rails 3.1 and you use &lt;a href="http://mongoid.org/"&gt;Mongoid&lt;/a&gt; &amp;#8211; turn preloading models off. If this won&amp;#8217;t help profile your app with &lt;a href="https://github.com/bhb/rack-perftools_profiler"&gt;rack-perftools_profiler&lt;/a&gt;.&lt;/p&gt;
&lt;h4&gt;Rails 3.1&lt;/h4&gt;
&lt;p&gt;Rails 3.1 is out for a while. It has a bunch of &lt;a href="http://weblog.rubyonrails.org/2011/5/22/rails-3-1-release-candidate"&gt;new features and enhancements&lt;/a&gt; and a bit of &lt;a href="http://www.rubyinside.com/rails-3-1-adopts-coffeescript-jquery-sass-and-controversy-4669.html"&gt;controversy&lt;/a&gt;. One of the new features that I wanted to play with was the asset pipeline. So after Release Candidate 4 was out I decided to upgrade &lt;a href="http://zubibu.com"&gt;zubibu.com&lt;/a&gt; to the newest version.&lt;/p&gt;
&lt;h4&gt;Upgrade&lt;/h4&gt;
&lt;p&gt;It is pretty easy to upgrade. You can find detailed instructions on &lt;a href="http://davidjrice.co.uk/2011/05/25/how-to-upgrade-a-rails-application-to-version-3-1-0.html"&gt;David Rice&amp;#8217;s blog post&lt;/a&gt;. Some additional fixes are in comments below his post so read them as well. If you want to fully use asset pipeline you also have to modify all urls in your stylesheets from:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="erb"&gt;&lt;span class="x"&gt;background: #ffffff url(images/backend/bg.png) repeat-x 0 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="erb"&gt;&lt;span class="x"&gt;background: #ffffff url(&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;asset_path&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;backend/bg.png&amp;quot;&lt;/span&gt;&lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="x"&gt;) repeat-x 0 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and add &lt;code&gt;.erb&lt;/code&gt; extension to all modified &lt;span class="caps"&gt;CSS&lt;/span&gt; files. One good regular expression should be enough for updating all your files.&lt;/p&gt;
&lt;h4&gt;Horror&lt;/h4&gt;
&lt;p&gt;I restarted application and then the horror begun. It was impossible to load whole page with all the assts. Some assets returned 500 status code, some load in 30+ seconds. I tried some tricks with linking &lt;code&gt;app/assets/images/&lt;/code&gt; folder to &lt;code&gt;public/images/&lt;/code&gt;. It helped a lot, as images was not served by rails, but still &lt;span class="caps"&gt;CSS&lt;/span&gt; and JavaScript files was very slow. If I don&amp;#8217;t reload pages by &lt;code&gt;CMD+R&lt;/code&gt; I was able to work, but styling pages was almost impossible.&lt;br /&gt;
&lt;img src="/images/posts/2011-06-25-rails-3-1-and-slow-asset-pipeline/assets.jpg" title="slow assets pipeline" alt="slow assets pipeline" /&gt;&lt;/p&gt;
&lt;h4&gt;Why is it so slow?&lt;/h4&gt;
&lt;p&gt;I couldn&amp;#8217;t believe that asset pipeline is so slow. Yes, it should be slower that serving static files from disk but it should not be &lt;span class="caps"&gt;THAT&lt;/span&gt; slow. I decided to check what is wrong. I created new rails 3.1 application, added one model with scaffold, one stylesheet and one javascript. I started the application and everything was alright. Assets was loading under 50ms. Then I&amp;#8217;ve added more images, stylesheets, and javascripts. About twenty files together. And still everything was running smoothly. We use &lt;a href="http://mongoid.org/"&gt;mongoid&lt;/a&gt; as an &lt;span class="caps"&gt;ORM&lt;/span&gt; in &lt;a href="http://zubibu.com"&gt;zubibu&lt;/a&gt;, so I migrated the only model to &lt;a href="http://mongoid.org/"&gt;mongoid&lt;/a&gt;. But it still was damn fast! It was fast until I added more models to my test application. After adding 30 models (with relations between them) assets started to load in about 3 seconds each. The models was very simple without any logic &amp;#8211; just a few fields and references each. It means that it could be models them self but the fact that there are many models.&lt;/p&gt;
&lt;h4&gt;perftools.rb&lt;/h4&gt;
&lt;p&gt;I didn&amp;#8217;t want to dig in &lt;a href="http://mongoid.org/"&gt;mongoid&lt;/a&gt; source code for ever, so decided to use great profiling tool: &lt;a href="https://github.com/tmm1/perftools.rb"&gt;perftools.rb&lt;/a&gt; and because I need to profile web app I used &lt;a href="https://github.com/bhb/rack-perftools_profiler"&gt;rack-perftools_profiler&lt;/a&gt; middleware. I ran profiler two times: first with 1 model and than with 30 models loaded. Reading profile data is quite hard and, I am not the best in this, but I noticed that when app is running with many models loaded it spends much more time in &lt;code&gt;Mongoid::load_models&lt;/code&gt; method which looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="lineno"&gt;14&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;load_models&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="lineno"&gt;15&lt;/span&gt;   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Mongoid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preload_models&lt;/span&gt;
&lt;span class="lineno"&gt;16&lt;/span&gt;   &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;paths&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;app/models&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="lineno"&gt;17&lt;/span&gt;     &lt;span class="no"&gt;Dir&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/**/*.rb&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="lineno"&gt;18&lt;/span&gt;       &lt;span class="n"&gt;load_model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gsub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gsub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;.rb&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="lineno"&gt;19&lt;/span&gt;     &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="lineno"&gt;20&lt;/span&gt;   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="lineno"&gt;21&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And what is &lt;code&gt;::Mongoid.preload_models&lt;/code&gt;? It is config option turn on by default that reloads all models on each request. According to mongoid &lt;a href="http://mongoid.org/docs/rails/railties.html"&gt;documentation&lt;/a&gt; you can turn it off if don&amp;#8217;t use single collection inheritance in your app. To turn it off simply add following line to your &lt;code&gt;congif.applicaton&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mongoid&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preload_models&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;I did it in my application and it works! It is usable again!&lt;/p&gt;






&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/xZ7O_93YkFc" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2011/06/rails-3-1-and-slow-asset-pipeline.html</feedburner:origLink></entry>
 
 <entry>
   <title>Blogging like a hacker using Jekyll, Compass and foreman</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/yje8e9DQDXU/blogging-like-a-hacker-using-jekyll-compass-and-foreman.html" />
   <updated>2011-06-17T00:00:00-07:00</updated>
   <id>martinciu.com/2011/06/blogging-like-a-hacker-using-jekyll-compass-and-foreman</id>
   <summary>How to add some magic to static Jekyll blog development.</summary>
   <content type="html">&lt;p&gt;You probably already know &lt;a href="http://tom.preston-werner.com/2008/11/17/blogging-like-a-hacker.html"&gt;how to blog like a hacker&lt;/a&gt; using &lt;a href="http://git-scm.com/"&gt;Git&lt;/a&gt; and &lt;a href="http://jekyllrb.com/"&gt;Jekyll&lt;/a&gt; It is realy simple but powerfull way of maintaing personal blog. If you put it&amp;#8217;s code on the &lt;a href="https://github.com/"&gt;github&lt;/a&gt; like &lt;a href="https://github.com/jeffkreeftmeijer/jeffkreeftmeijer.com"&gt;Jeff Kreeftmeijer&lt;/a&gt; did you can even count on comunity &lt;a href="pool-request"&gt;contribution&lt;/a&gt; to your own personal blog. How could is that!&lt;/p&gt;
&lt;p&gt;If don&amp;#8217;t use &lt;a href="http://jekyllrb.com/"&gt;jekyll&lt;/a&gt; for you blog you should definetly reconsider that. If you already do use jekyll you probably know that for developemnt you need to run&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;jekyll --server
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;to set up simple local server that automatically regenarates static files echa time they are changend. If you also use &lt;a href="http://sass-lang.com/"&gt;&lt;span class="caps"&gt;SASS&lt;/span&gt;&lt;/a&gt; or &lt;a href="http://compass-style.org/"&gt;Compass&lt;/a&gt; for your stylesheets you need to run in second terminal&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;sass --watch input-dir:output-dir
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;or for &lt;a href="comapss"&gt;Comapss&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;compass watch
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That is preaty annoying to run two processes in two separate terminals.&lt;/p&gt;
&lt;h4&gt;Foreman &amp;amp; Procfile&lt;/h4&gt;
&lt;p&gt;And this is where &lt;a href="http://blog.daviddollar.org/2011/05/06/introducing-foreman.html"&gt;Foreman&lt;/a&gt; comes with help. &lt;a href="http://blog.daviddollar.org/2011/05/06/introducing-foreman.html"&gt;Foreman&lt;/a&gt; is a process manager for applications with multiple components. It manages Procfile-based applications. We have application with two components so this perfetly fits for our needs. All we need is to add &lt;code&gt;Procfile&lt;/code&gt; To make it work install foreman by&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gem install foreman
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and create &lt;code&gt;Procfile&lt;/code&gt; under root directory&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;jekyll:  jekyll --server
comapss: compass watch
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To start both processes just type&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;foreman start
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and you should see smething similar to:&lt;/p&gt;
&lt;p&gt;&lt;img src="/images/posts/2011-06-17-blogging-like-a-hacker-using-jekyll-compass-and-foreman/foreman.png" title="forman start output" alt="forman start output" /&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s it! Your are a better now!&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;







&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/yje8e9DQDXU" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2011/06/blogging-like-a-hacker-using-jekyll-compass-and-foreman.html</feedburner:origLink></entry>
 
 <entry>
   <title>Rails 2.2, bundler, rack and pow</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/LK8vbAiXn7E/rails2.2-bunlder-rack-and-pow.html" />
   <updated>2011-06-16T00:00:00-07:00</updated>
   <id>martinciu.com/2011/06/rails2.2-bunlder-rack-and-pow</id>
   <summary>How to set up Ruby on Rails 2.2 with bundler, rack and Pow</summary>
   <content type="html">&lt;p&gt;Yesterday I have upgraded our production server. I updated &lt;a href="https://rubygems.org"&gt;rubygems&lt;/a&gt; to version 1.8.5 and &lt;a href="http://gembundler.com"&gt;bundler&lt;/a&gt; to 1.1.pre.5. I also updated &lt;a href="http://zuibu.com"&gt;zubibu.com&lt;/a&gt; &amp;#8216;s staging server by uploading quite a few new gems. One of these actions (or all of them together) broke a few old, low-trafic &lt;a href="http://rubyonrails.org/"&gt;rails&lt;/a&gt; websites. I didn&amp;#8217;t investigate what exactly did break them. All broken websites had one thing in common &amp;#8211; they were so old that they didn&amp;#8217;t use bunlder to manage gems.&lt;/p&gt;
&lt;h4&gt;Bundler&lt;/h4&gt;
&lt;p&gt;Most of them was written in rails 2.3, so managing them to user bundler was preaty easy. You can &lt;a href="bunlder-rails23"&gt;manual&lt;/a&gt; on bundler website how to do it and that simply wokrs. But one of the broken webisetes was running Rails 2.2.2. I googled if such old app can use bundler, but didn&amp;#8217;t find any answers. So I decided to try it. I started with the &lt;a href="http://gembundler.com/rails23.html"&gt;inststructions&lt;/a&gt; for rails 2.3 app.&lt;/p&gt;
&lt;p&gt;I have added this code to the &lt;code&gt;config/boot.rb&lt;/code&gt; file right above the line &lt;code&gt;Rails.boot!&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Boot&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;
    &lt;span class="n"&gt;load_initializer&lt;/span&gt;

    &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Initializer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;class_eval&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;load_gems&lt;/span&gt;
        &lt;span class="vi"&gt;@bundler_loaded&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="no"&gt;Bundler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;require&lt;/span&gt; &lt;span class="ss"&gt;:default&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Initializer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:set_load_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Then I created &lt;code&gt;config/preinitializer.rb&lt;/code&gt; file&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;begin&lt;/span&gt;
  &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;rubygems&amp;quot;&lt;/span&gt;
  &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;bundler&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="no"&gt;LoadError&lt;/span&gt;
  &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Could not load the bundler gem. Install it with `gem install bundler`.&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;Gem&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Version&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Bundler&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;VERSION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="no"&gt;Gem&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Version&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;0.9.24&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Your bundler version is too old for Rails 2.3.&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
   &lt;span class="s2"&gt;&amp;quot;Run `gem install bundler` to upgrade.&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;begin&lt;/span&gt;
  &lt;span class="c1"&gt;# Set up load paths for all bundled gems&lt;/span&gt;
  &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;BUNDLE_GEMFILE&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expand_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;../../Gemfile&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="no"&gt;Bundler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setup&lt;/span&gt;
&lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="no"&gt;Bundler&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GemNotFound&lt;/span&gt;
  &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Bundler couldn&amp;#39;t find some gems.&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
    &lt;span class="s2"&gt;&amp;quot;Did you run `bundle install`?&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And created the &lt;code&gt;Gemfile&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;bundle init
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;I copied there all &lt;code&gt;config.gem&lt;/code&gt; directives from &lt;code&gt;confiv/environmet*&lt;/code&gt; files and lock the gems at the certain versions as the probably wouldn&amp;#8217;t run with old code.&lt;/p&gt;
&lt;p&gt;That would be all from gembundler instructions, so I created a gemset for this faling app and run &lt;code&gt;bundle install&lt;/code&gt;. When I try to run tests I got this depracation warning:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;rake/rdoctask is deprecated.  Use rdoc/task instead &lt;span class="o"&gt;(&lt;/span&gt;in RDoc 2.4.2+&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To get rid of it just replace replace one line in your &lt;code&gt;Rakefile&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# require &amp;#39;rake/rdoctask&amp;#39;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rdoc/task&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and add &lt;code&gt;rdoc&lt;/code&gt; to your &lt;code&gt;Gemfile&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;#...&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rdoc&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;After &lt;code&gt;bundle install&lt;/code&gt; all the tests passed. Yay!&lt;/p&gt;
&lt;h4&gt;Pow &amp;amp; Rack&lt;/h4&gt;
&lt;p&gt;I run all my rails application using &lt;a href="http://pow.cx/"&gt;Pow&lt;/a&gt;. This one I wanted to run same as others. I just add a symbolic link in my &lt;code&gt;~./pow&lt;/code&gt; folder and visited an .dev url in the browser and I got this message:&lt;/p&gt;
&lt;p&gt;&lt;img src="/images/posts/2011-06-16-rails2.2-bunlder-rack-and-pow.textile/pow.png" title="If you’re using a version of Rails older than 2.3, you’ll need to upgrade first." alt="If you’re using a version of Rails older than 2.3, you’ll need to upgrade first." /&gt;&lt;/p&gt;
&lt;p&gt;Not good :(. They advised me to upgrade my application but I realy don&amp;#8217;t want to do. Before I googled how to upgrade from rails 2.2 to 2.3 I decided to give it a try and just add the &lt;code&gt;cofnig.ru&lt;/code&gt; file.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/config/environment&amp;#39;&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Dispatcher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Then I vited app&amp;#8217;s dev url in the browser just to get this message:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;MissingSourceFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="n"&gt;such&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nb"&gt;load&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="n"&gt;rack&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To fix it just add &lt;code&gt;gem 'rack'&lt;/code&gt; to your &lt;code&gt;Gemfile&lt;/code&gt;. Than all works perfectly! Even my old rails 2.2 app.&lt;/p&gt;
&lt;h4&gt;tl;dr&lt;/h4&gt;
&lt;p&gt;1. &lt;a href="http://gembundler.com"&gt;Bundler&lt;/a&gt; works with rails 2.2 app.&lt;br /&gt;
2. &lt;a href="http://pow.cx/"&gt;Pow&lt;/a&gt; works with rails 2.2 app even if docs says something else&lt;br /&gt;
3. If you want to know how &amp;#8211; read the whole thing!&lt;/p&gt;




&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/LK8vbAiXn7E" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2011/06/rails2.2-bunlder-rack-and-pow.html</feedburner:origLink></entry>
 
 <entry>
   <title>Mounting Grape API inside rails application</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/AiM91c9Leeo/mounting-grape-api-inside-rails-application.html" />
   <updated>2011-01-07T00:00:00-08:00</updated>
   <id>martinciu.com/2011/01/mounting-grape-api-inside-rails-application</id>
   <summary>Grape is a REST-like API micro-framework for Ruby. It is built to complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily provide APIs.</summary>
   <content type="html">&lt;p&gt;&lt;a href="https://github.com/intridea/grape"&gt;Grape&lt;/a&gt; is a &lt;span class="caps"&gt;REST&lt;/span&gt;-like &lt;span class="caps"&gt;API&lt;/span&gt; micro-framework for Ruby. It is built to complement existing web application frameworks such as &lt;a href="http://rubyonrails.org/"&gt;Rails&lt;/a&gt; and &lt;a href="http://www.sinatrarb.com/"&gt;Sinatra&lt;/a&gt; by providing a simple &lt;span class="caps"&gt;DSL&lt;/span&gt; to easily provide APIs. It has built-in support for common conventions such as multiple formats, subdomain/prefix restriction, and versioning.  Because &lt;a href="https://github.com/intridea/grape"&gt;grape&lt;/a&gt; APIs are &lt;a href="http://rack.rubyforge.org/"&gt;Rack&lt;/a&gt; applications, it is very easy (in rails 3) and quite easy (in rails 2.3) to mount it in your existing rails application.&lt;/p&gt;
&lt;h4&gt;&lt;span class="caps"&gt;API&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;Let’s start with a simple &lt;span class="caps"&gt;API&lt;/span&gt;. Assuming you have a simple blog application with Post resource and you want to list all posts and show a specific post by ID using &lt;span class="caps"&gt;API&lt;/span&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;#lib/api.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;MyApp&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;API&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Grape&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;API&lt;/span&gt;
    &lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;api&amp;quot;&lt;/span&gt;
 
    &lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;posts&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;all&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
 
      &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;:id&amp;#39;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
 
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;Rails 3&lt;/h4&gt;
&lt;p&gt;Because of Rails3 modularity it is very easy to mount any &lt;a href="http://rack.rubyforge.org/"&gt;Rack&lt;/a&gt; application inside existing application. You only need to add following line to your &lt;code&gt;config/routes.rb&lt;/code&gt; file&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;#config/routes.rb&lt;/span&gt;
&lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;MyApp&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;API&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span class="c1"&gt;#API will be available under &amp;quot;/api&amp;quot; url because of setting from MyApp::API line 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;After restartig your application, &lt;code&gt;GET /api/posts&lt;/code&gt; should returns all posts, and &lt;code&gt;GET /api/posts/2&lt;/code&gt; should returns post with id=2. Simple as that.&lt;/p&gt;
&lt;h4&gt;Rails 2.3&lt;/h4&gt;
&lt;p&gt;If you want to use grape with rails 2.3, things are a little more complicated. We can use the same &lt;code&gt;lib/api.rb&lt;/code&gt; file, but we can not mount our &lt;span class="caps"&gt;API&lt;/span&gt; in &lt;code&gt;config/routes.rb&lt;/code&gt;. Fortunately, rails 2.3 is actually a rack application, so we can use &lt;a href="https://github.com/rack/rack/blob/master/lib/rack/cascade.rb"&gt;Rack::Cascade&lt;/a&gt; to run api request without touching rails app. But, first things first.&lt;/p&gt;
&lt;p&gt;When you are starting rails application by &lt;code&gt;ruby script/server&lt;/code&gt;, an new &lt;a href="http://rack.rubyforge.org/doc/classes/Rack/Builder.html"&gt;Rack::Builder&lt;/a&gt; is being initialized with some middleware and new rails dispatcher:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="lineno"&gt; 83&lt;/span&gt;   &lt;span class="c1"&gt;# rails/lib/commands/server.rb&lt;/span&gt;
&lt;span class="lineno"&gt; 84&lt;/span&gt;   &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="no"&gt;RAILS_ROOT&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/config/environment&amp;quot;&lt;/span&gt;
&lt;span class="lineno"&gt; 85&lt;/span&gt;   &lt;span class="n"&gt;inner_app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Dispatcher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;span class="lineno"&gt; 86&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="lineno"&gt; 87&lt;/span&gt;  
&lt;span class="lineno"&gt; 88&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:path&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;nil?&lt;/span&gt;
&lt;span class="lineno"&gt; 89&lt;/span&gt;   &lt;span class="n"&gt;map_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/&amp;quot;&lt;/span&gt;
&lt;span class="lineno"&gt; 90&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="lineno"&gt; 91&lt;/span&gt;   &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relative_url_root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:path&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="lineno"&gt; 92&lt;/span&gt;   &lt;span class="n"&gt;map_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:path&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="lineno"&gt; 93&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="lineno"&gt; 94&lt;/span&gt;  
&lt;span class="lineno"&gt; 95&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="lineno"&gt; 96&lt;/span&gt;   &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;LogTailer&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:detach&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="lineno"&gt; 97&lt;/span&gt;   &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Debugger&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:debugger&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="lineno"&gt; 98&lt;/span&gt;   &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;map_path&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
&lt;span class="lineno"&gt; 99&lt;/span&gt;     &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Static&lt;/span&gt; 
&lt;span class="lineno"&gt;100&lt;/span&gt;     &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;inner_app&lt;/span&gt;
&lt;span class="lineno"&gt;101&lt;/span&gt;   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="lineno"&gt;102&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Then, a server runs app. Any server knows how to run it because &lt;code&gt;app&lt;/code&gt; is a valid Rack application. So, why can we run rack app our own? Sure we can! The best way to run is to define it in &lt;code&gt;config.ru&lt;/code&gt; file. Rails 2.3 is not shipped with it, so you have to create it by your self. Unless you changed default options your &lt;code&gt;config.ru&lt;/code&gt; file should looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;config/environment&amp;quot;&lt;/span&gt;
 
&lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;LogTailer&lt;/span&gt;
&lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Static&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Dispatcher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now, you can start your appliction by:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;bundle &lt;span class="nb"&gt;exec&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; rackup
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Your application should run like before, except logs my looks a little bit different. Now we will add our &lt;span class="caps"&gt;API&lt;/span&gt; to the application. To do that, we will use &lt;a href="https://github.com/rack/rack/blob/master/lib/rack/cascade.rb"&gt;Rack::Cascade&lt;/a&gt;. &lt;a href="https://github.com/rack/rack/blob/master/lib/rack/cascade.rb"&gt;Rack::Cascade&lt;/a&gt; tries an request on several apps, and returns the first response that is not 404 (or in a list of configurable status codes). We want our &lt;span class="caps"&gt;API&lt;/span&gt; to be as fast and lightweight as possible. That’s why we will add before trying rails application, to avoid all it’s magic when it is not necessary.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;config/environment&amp;quot;&lt;/span&gt;
 
&lt;span class="n"&gt;rails_app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;LogTailer&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Static&lt;/span&gt;
  &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Dispatcher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
 
&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Cascade&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;
  &lt;span class="no"&gt;MyApp&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;API&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;rails_app&lt;/span&gt;
&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now, routes &lt;code&gt;GET /api/posts&lt;/code&gt; should disply all posts, and &lt;code&gt;GET /api/posts/2&lt;/code&gt; should work as expected.&lt;/p&gt;




&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/AiM91c9Leeo" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2011/01/mounting-grape-api-inside-rails-application.html</feedburner:origLink></entry>
 
 <entry>
   <title>Differences between Proc.new and lambda in ruby</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/HTV6Brbj3GY/differences-between-proc-new-and-lambda-in-ruby.html" />
   <updated>2010-12-27T00:00:00-08:00</updated>
   <id>martinciu.com/2010/12/differences-between-proc-new-and-lambda-in-ruby</id>
   <summary>Proc.new and lambda does almost the same job. They provide a dynamically created inline method. But they are slightly different.</summary>
   <content type="html">&lt;p&gt;It will be another not very innovative post about a something that probably most Ruby developers already know. Unfortunately I didn’t know about this  before, and I think it will be a quite good form of this blog, that I will be posting things that I’ve recently learned. So here is what I’ve learned today.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Proc.new&lt;/code&gt; and &lt;code&gt;lambda&lt;/code&gt; does almost the same job. They provide a dynamically created inline method. But they are slightly different. What are the differences? There are two (as far as I know at the moment)&lt;/p&gt;
&lt;h4&gt;1. Acceptance of parameters&lt;/h4&gt;
&lt;p&gt;&lt;code&gt;Proc.new&lt;/code&gt; doesn’t care how many parameter do you provide, when &lt;code&gt;lambda&lt;/code&gt; needs exact number of parameters. Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;pr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;pr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# 1&lt;/span&gt;
&lt;span class="c1"&gt;# 2&lt;/span&gt;
&lt;span class="c1"&gt;# 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;#8230;and&amp;#8230;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;pr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;pr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ArgumentError: wrong number of arguments (4 for 3)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h4&gt;2. Returning from a proc&lt;br /&gt;
&lt;code&gt;return&lt;/code&gt; called in &lt;code&gt;Proc.new&lt;/code&gt; block returns from enclosing method when return called in &lt;code&gt;lambda&lt;/code&gt; block returns from just a block.&lt;br /&gt;
Examples:&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_method&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Starting my_method&amp;quot;&lt;/span&gt;
  &lt;span class="n"&gt;pr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Proc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;pr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Finishing my_method&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
 
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Before my_method&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;my_method&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;After my_method&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;# Before my_method&lt;/span&gt;
&lt;span class="c1"&gt;# Starting my_method&lt;/span&gt;
&lt;span class="c1"&gt;# After my_method&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;but&amp;#8230;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_method&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Starting my_method&amp;quot;&lt;/span&gt;
  &lt;span class="n"&gt;pr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;pr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Finishing my_method&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
 
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Before my_method&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;my_method&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;After my_method&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;# Before my_method&lt;/span&gt;
&lt;span class="c1"&gt;# Starting my_method&lt;/span&gt;
&lt;span class="c1"&gt;# Finishing my_method&lt;/span&gt;
&lt;span class="c1"&gt;# After my_method&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The other thing that is worth mentioning is that there exist an instruction called &lt;code&gt;proc&lt;/code&gt;. What is wired, it works different in ruby 1.8 and in version 1.9. In Ruby 1.8 proc behaviors like &lt;code&gt;lambda&lt;/code&gt; when in Ruby 1.9 it works like &lt;code&gt;Proc.ne&lt;/code&gt;w. Because of this, and because of ruumors that proc instruction will be removed in ruby 2.0, I don’t recommend using it in your code.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/HTV6Brbj3GY" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2010/12/differences-between-proc-new-and-lambda-in-ruby.html</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby doesn’t have class methods</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/DWQWWXKOEGk/ruby-doesnt-have-class-methods.html" />
   <updated>2010-12-23T00:00:00-08:00</updated>
   <id>martinciu.com/2010/12/ruby-doesnt-have-class-methods</id>
   <summary>What does "def self.foo" realy do...</summary>
   <content type="html">&lt;p&gt;Maybe it is not very innovative and every ruby developer already knows about it, but when I first heard it, it was quite a hit. I mean I always feel that there is something wired on when I wrote some thing like &lt;code&gt;“def self.foo”&lt;/code&gt; in my class, but I didn’t fully understand actually does this line do.&lt;/p&gt;
&lt;p&gt;Alright. How can ruby doesn’t have class methods that I use them all time, you could ask. You mean something like this?&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bar&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;bar&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
 
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;baz&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;baz&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;bar&lt;/code&gt; and &lt;code&gt;baz&lt;/code&gt; are &lt;code&gt;Foo&lt;/code&gt; ‘s class methods, aren’t they? Actually they are not! So what they are? To answer that you should know two things. You should know what does this piece of code do:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;cat&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;animal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;speak&lt;/span&gt;
  &lt;span class="s2"&gt;&amp;quot;miau&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
 
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;speak&lt;/span&gt;
&lt;span class="c1"&gt;# miau&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;It adds a &lt;code&gt;speak&lt;/code&gt; method to the &lt;code&gt;animal&lt;/code&gt; object. It doesn’t add it to the &lt;code&gt;animal&lt;/code&gt; class (which is a &lt;code&gt;String&lt;/code&gt;), it also doesn’t add it to the other “cat” string. Only to the animal. How ruby does it? It creates a something called singleton class, and put there a speak method. When animal object receives any method call, it looks first in self, then in it’s singleton class (if exist) and then in each parent. When it reaches top, &lt;code&gt;method_missing&lt;/code&gt; is called and exactly same like any other methods calls.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;cat&amp;quot;&lt;/span&gt;
 
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;animal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;method_missing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; does not &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
 
&lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fly&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;quot;cat does not fly&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The second thing you should know is what does self is in class &lt;code&gt;Foo&lt;/code&gt; body.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
  &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;# Foo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Alright, so class Foo can looks like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
 
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bar&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;bar&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
 
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;baz&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;baz&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;What happend here? Same as adding method &lt;code&gt;speak&lt;/code&gt; for &lt;code&gt;animal&lt;/code&gt; object. Here we added method baz to &lt;code&gt;Foo&lt;/code&gt; object. Yes, &lt;code&gt;Foo&lt;/code&gt; is an object, and it is an instance of a class &lt;code&gt;Class&lt;/code&gt;. Method &lt;code&gt;baz&lt;/code&gt; has been added to his singleton class. Not to all classes, just to the &lt;code&gt;Foo&lt;/code&gt;. So, when you adds so called class methods, you actually adds an instance method to object’s parent class object through his singleton class.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/DWQWWXKOEGk" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2010/12/ruby-doesnt-have-class-methods.html</feedburner:origLink></entry>
 
 <entry>
   <title>Ruby Timeout::Error is not a StandardError</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/aOaFpKczBwQ/ruby-timeout-error-is-not-a-standard-error.html" />
   <updated>2010-12-21T00:00:00-08:00</updated>
   <id>martinciu.com/2010/12/ruby-timeout-error-is-not-a-standard-error</id>
   <summary>Quite strange but it is true (for ruby 1.8)</summary>
   <content type="html">&lt;p&gt;Let&amp;#8217;s assume that you have piece of code that does a very important job but some times this job can not be accomplished because of various reasons. One of them could be a timeout . It should also inform you nicely about that:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="lineno"&gt;1&lt;/span&gt; &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;timeout&amp;quot;&lt;/span&gt;
&lt;span class="lineno"&gt;2&lt;/span&gt;  
&lt;span class="lineno"&gt;3&lt;/span&gt; &lt;span class="k"&gt;begin&lt;/span&gt;
&lt;span class="lineno"&gt;4&lt;/span&gt;   &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
&lt;span class="lineno"&gt;5&lt;/span&gt;     &lt;span class="nb"&gt;sleep&lt;/span&gt;
&lt;span class="lineno"&gt;6&lt;/span&gt;   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="lineno"&gt;7&lt;/span&gt; &lt;span class="k"&gt;rescue&lt;/span&gt;
&lt;span class="lineno"&gt;8&lt;/span&gt;   &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;I&amp;#39;m sorry, Sir. We couldn&amp;#39;t make it, Sir.&amp;quot;&lt;/span&gt;
&lt;span class="lineno"&gt;9&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This code seems to be alright. If script sleeps to long Timeout::Error exception should be thrown and I should be informed about it. Alright, lets run it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ruby test.rb
/Users/martinciu/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/1.8/timeout.rb:60: execution expired &lt;span class="o"&gt;(&lt;/span&gt;Timeout::Error&lt;span class="o"&gt;)&lt;/span&gt;
	from test.rb:4
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span class="caps"&gt;WTF&lt;/span&gt;? That was exactly what I thought when I first saw this issue. The answer is simple and actually is in documentation: “If you write a rescue clause with no parameter list, the parameter defaults to StandardError.”&lt;/p&gt;
&lt;p&gt;Wait a minute! Timeout::Error does not extend StandardError? Well, actually it doesn’t:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;irb
&amp;gt;&amp;gt; Timeout::Error.ancestors
&lt;span class="o"&gt;=&lt;/span&gt;&amp;gt; &lt;span class="o"&gt;[&lt;/span&gt;Timeout::Error, Interrupt, SignalException, Exception, Object, Kernel&lt;span class="o"&gt;]&lt;/span&gt;
&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Strange, isn’t it? It has to be strange to so many people that it has been changed in ruby 1.9:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;rvm 1.9.2 irb                                                                                                                         ~/www/timeout
&amp;gt;&amp;gt; require &lt;span class="s1"&gt;&amp;#39;timeout&amp;#39;&lt;/span&gt;
&lt;span class="o"&gt;=&lt;/span&gt;&amp;gt; &lt;span class="nb"&gt;true&lt;/span&gt;
&amp;gt;&amp;gt; Timeout::Error.ancestors
&lt;span class="o"&gt;=&lt;/span&gt;&amp;gt; &lt;span class="o"&gt;[&lt;/span&gt;Timeout::Error, RuntimeError, StandardError, Exception, Object, Kernel, BasicObject&lt;span class="o"&gt;]&lt;/span&gt;
&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;So, the title of this post should read: Timeout::Error is not an StandardError in ruby 1.8.&lt;/p&gt;
&lt;p&gt;OK, what are the solutions for this case:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;you can replace line 7 by “rescue StandardError,Timeout::Error” – only standard erors and timrouts will he rescued then&lt;/li&gt;
	&lt;li&gt;you can replace line 7 by “rescue Exception” – all exceptions will be caught than by rescue block&lt;/li&gt;
	&lt;li&gt;you can add another rescue block only for Timeout::Error&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It seems to me that solution no.1 is the best, but it could vary on your application.&lt;/p&gt;
&lt;p&gt;Are there any other Exceptions that doesn’t extend StandardError and are not rescued by defult? Yes there are some. Here is the list of a few Exceptions that you could be familiar with:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;StopIteration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SystemStackError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;LocalJumpError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;EOFError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;IOError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;RegexpError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;FloatDomainError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ZeroDivisionError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ThreadError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SystemCallError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NoMemoryError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;SecurityError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;RuntimeError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NotImplementedError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;LoadError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SyntaxError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="no"&gt;ScriptError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NoMethodError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NameError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;RangeError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;IndexError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="no"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;StandardError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Interrupt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SignalException&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fatal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SystemExit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;For most of them it is best to not catch them by rescue directive. But probably there could some cases that rescuing any of these Exceptions is require. Please remember that these are only examples and there are many other exceptions that are not rescued by default.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/aOaFpKczBwQ" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2010/12/ruby-timeout-error-is-not-a-standard-error.html</feedburner:origLink></entry>
 
 <entry>
   <title>RailsAdmin</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/soPMG4C5jaM/rails-admin.html" />
   <updated>2010-12-19T00:00:00-08:00</updated>
   <id>martinciu.com/2010/12/rails-admin</id>
   <summary>RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data.</summary>
   <content type="html">&lt;p&gt;Erlier this week &lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt; came out to the wider world.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt; is a Rails engine that provides an easy-to-use interface for managing your data. &lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt; started as a port of &lt;a href="http://github.com/sferik/merb-admin"&gt;MerbAdmin&lt;/a&gt; to Rails3 and was implemented as a &lt;a href="http://www.rubysoc.org/projects"&gt;Ruby Summer of Code&lt;/a&gt; project by &lt;a href="http://github.com/hurrycane"&gt;Bogdan Gaza&lt;/a&gt; with mentors &lt;a href="http://github.com/sferik"&gt;Erik Michaels-Ober&lt;/a&gt;, &lt;a href="http://github.com/wycats"&gt;Yehuda Katz&lt;/a&gt;, &lt;a href="http://github.com/rosenfeld"&gt;Rodrigo Rosenfeld Rosas&lt;/a&gt;, &lt;a href="http://github.com/plukevdh"&gt;Luke van der Hoeven&lt;/a&gt;, and &lt;a href="rein"&gt;Rein Henrichs&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I first heard it from &lt;a href="http://twitter.com/josevalim/status/15349132516397056"&gt;Jose Valim’s tweet&lt;/a&gt;. If he said it is very well written, it has be so. At first I thought about &lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt; as another &lt;a href="http://www.activescaffold.com/"&gt;ActiveScaffold&lt;/a&gt; or &lt;a href="http://streamlinedframework.org/"&gt;Streamlined&lt;/a&gt; clone. Then I remembered that I gave a try some of them but each had some issues that makes them unusable in my projects. That’s why I decided to try out the newest rails administration engine.&lt;/p&gt;
&lt;p&gt;Installation is easy and well explained in a &lt;a href="https://github.com/sferik/rails_admin/blob/master/README.mkd"&gt;Readme&lt;/a&gt; file. I installed it in the project that uses ActiveRecord as this is the only &lt;span class="caps"&gt;ORM&lt;/span&gt; supported at the moment. This is quite an issue for me as my main project uses &lt;a href="https://github.com/jnunemaker/mongomapper"&gt;MongoMapper&lt;/a&gt;. Fortunately &lt;a href="https://github.com/siong1987"&gt;Teng Siong Ong&lt;/a&gt; should be already working on this &lt;a href="https://github.com/sferik/rails_admin/issues/#issue/105"&gt;issue&lt;/a&gt;. The other issue that I got was that &lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt; is available under “/admin” route and it can not be currently changed. This is another blocking issue for me as I have this route already used in almost every application. Fortunately (again) this &lt;a href="https://github.com/sferik/rails_admin/issues#issue/151"&gt;issue&lt;/a&gt; should be soon (I hope) fixed.&lt;/p&gt;
&lt;p&gt;Alright. Assuming that your application uses Rails3, ActiveRecord and does not use “/admin” rooute. Are there any other issues that prevents using &lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt;? Actually, there was (for me) a few:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt; controller inherits from your ApplicationController. That means that if you have some wired stuff like finding subdomains, or setting locales in your ApplicationController then it break &lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt;. However, maybe ApplicationController is not the best place to keep that kind of stuff.&lt;/li&gt;
	&lt;li&gt;if you have I18n.default_locale set in your environment.rb and &lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt; doesn’t support it, than the only thing that you’ll see are “translation_missing” messages&lt;/li&gt;
	&lt;li&gt;looking at the list of &lt;a href="https://github.com/sferik/rails_admin/issues"&gt;issues&lt;/a&gt; on github there could be even more&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Besides issues that I mentioned, and other that you could have I think that could be a really useful tool for every rails developer. Looking at the same issue list and recent commit history I believe that &lt;a href="https://github.com/sferik/rails_admin"&gt;RailsAdmin&lt;/a&gt; will be one of that tools that you adds to your each new application.&lt;/p&gt;
&lt;p&gt;Thanks to &lt;a href="http://github.com/sferik"&gt;Erik&lt;/a&gt;, and to all contributors.&lt;/p&gt;
















&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/soPMG4C5jaM" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2010/12/rails-admin.html</feedburner:origLink></entry>
 
 <entry>
   <title>My Gemfile</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/x0XTUsIhkJI/my-gemfile.html" />
   <updated>2010-12-19T00:00:00-08:00</updated>
   <id>martinciu.com/2010/12/my-gemfile</id>
   <summary>For each new rails project I almost always put same gems to the Gemfile with initial commit. Here is my gemfile compiled from a few recent projects. Hope some of you find something new for your projects.</summary>
   <content type="html">&lt;p&gt;For each new rails project I almost always put same gems to the Gemfile with initial commit. Here is my gemfile compiled from a few recent projects. Hope some of you find something new for your projects.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;http://rubygems.org&amp;#39;&lt;/span&gt;
 
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rails&amp;#39;&lt;/span&gt;               &lt;span class="c1"&gt;# pretty obvious&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;mysql2&amp;#39;&lt;/span&gt;              &lt;span class="c1"&gt;# or mongoid if I&amp;#39;m not using mysql&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;devise&amp;#39;&lt;/span&gt;              &lt;span class="c1"&gt;# authentication done right&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;newrelic_rpm&amp;#39;&lt;/span&gt;        &lt;span class="c1"&gt;# best monitoring service for me, very useful&lt;/span&gt;
                          &lt;span class="c1"&gt;# in development too&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;inherited_resources&amp;#39;&lt;/span&gt; &lt;span class="c1"&gt;# makes my controller DRY&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;formtastic&amp;#39;&lt;/span&gt;          &lt;span class="c1"&gt;# for less painful forms&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;carrier_wave&amp;#39;&lt;/span&gt;        &lt;span class="c1"&gt;# cool way in managing uploaded files&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;hoptoad_notifier&amp;#39;&lt;/span&gt;    &lt;span class="c1"&gt;# simple and cheap error notification service&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;haml&amp;#39;&lt;/span&gt;                &lt;span class="c1"&gt;# makes views more readable&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;haml-rails&amp;#39;&lt;/span&gt;          &lt;span class="c1"&gt;# adds haml in generators&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rails3-generators&amp;#39;&lt;/span&gt;   &lt;span class="c1"&gt;# add rails3 compatible generators to gems that&lt;/span&gt;
                          &lt;span class="c1"&gt;# don&amp;#39;t have them&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;jquery-rails&amp;#39;&lt;/span&gt;        &lt;span class="c1"&gt;# to replace prototype&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;jammit&amp;#39;&lt;/span&gt;              &lt;span class="c1"&gt;# powerful asset packager&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;resque&amp;#39;&lt;/span&gt;              &lt;span class="c1"&gt;# for background jobs&lt;/span&gt;
 
&lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="ss"&gt;:development&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;mail_safe&amp;#39;&lt;/span&gt;         &lt;span class="c1"&gt;# it makes you sure that you won&amp;#39;t send test&lt;/span&gt;
                          &lt;span class="c1"&gt;# email to all your user base&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
 
&lt;span class="n"&gt;group&lt;/span&gt; &lt;span class="ss"&gt;:development&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:test&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;cucumber-rails&amp;#39;&lt;/span&gt;    &lt;span class="c1"&gt;# it knows how your application should behave&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;database_cleaner&amp;#39;&lt;/span&gt;  &lt;span class="c1"&gt;# easily cleans your database during tests&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;capybara&amp;#39;&lt;/span&gt;          &lt;span class="c1"&gt;# best browser simulator&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rspec-rails&amp;#39;&lt;/span&gt;       &lt;span class="c1"&gt;# good test-unit replacement&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;machinist&amp;#39;&lt;/span&gt;         &lt;span class="c1"&gt;# fixtures are fun again&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;faker&amp;#39;&lt;/span&gt;             &lt;span class="c1"&gt;# to generating tons of fake data&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;pickle&amp;#39;&lt;/span&gt;            &lt;span class="c1"&gt;# easy model creations and more for cucumber&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;email_spec&amp;#39;&lt;/span&gt;        &lt;span class="c1"&gt;# rspec matchers for emails&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;timecop&amp;#39;&lt;/span&gt;           &lt;span class="c1"&gt;# great tool if your application time sensitive&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;shoulda&amp;#39;&lt;/span&gt;           &lt;span class="c1"&gt;# useful macros for rspec&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;launchy&amp;#39;&lt;/span&gt;           &lt;span class="c1"&gt;# simple tool launching applications from ruby&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;capistrano&amp;#39;&lt;/span&gt;        &lt;span class="c1"&gt;# my tool of choice for deployment&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;capistrano-ext&amp;#39;&lt;/span&gt;    &lt;span class="c1"&gt;# with some extensions for multistage&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;bond&amp;#39;&lt;/span&gt;              &lt;span class="c1"&gt;# makes irb more useful&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;rcov&amp;#39;&lt;/span&gt;              &lt;span class="c1"&gt;# code coverage tool&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;fuubar&amp;#39;&lt;/span&gt;            &lt;span class="c1"&gt;# progress bar rspec formatter&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;fuubar-cucumber&amp;#39;&lt;/span&gt;   &lt;span class="c1"&gt;# progress bar cucumber formatter&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;chronic&amp;#39;&lt;/span&gt;           &lt;span class="c1"&gt;# easy time parsing&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;fakeweb&amp;#39;&lt;/span&gt;           &lt;span class="c1"&gt;# for mocking net/http connections&lt;/span&gt;
  &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;vcr&amp;#39;&lt;/span&gt;               &lt;span class="c1"&gt;# records and plays back http calls&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;I hope you find at least one gem that you haven’t heard about.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/x0XTUsIhkJI" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2010/12/my-gemfile.html</feedburner:origLink></entry>
 
 <entry>
   <title>fuubar-cucumber</title>
   <link href="http://feedproxy.google.com/~r/martinciu/~3/IRdFOczeC-g/fuubar-cucumber.html" />
   <updated>2010-10-21T00:00:00-07:00</updated>
   <id>martinciu.com/2010/10/fuubar-cucumber</id>
   <summary>The instafailing Cucumber progress bar formatter</summary>
   <content type="html">&lt;p&gt;A few days ago &lt;a href="http://jeffkreeftmeijer.com/"&gt;Jeff Kreeftmeijer&lt;/a&gt; released &lt;a href="http://jeffkreeftmeijer.com/2010/fuubar-the-instafailing-rspec-progress-bar-formatter/"&gt;Fuubar: the instafailing RSpec progress bar formatter&lt;/a&gt;. It is great, and I use it as default formatter in all my projects. As I started to use it I realised that it would be great to have similar formatter for &lt;a href="https://github.com/aslakhellesoy/cucumber"&gt;Cucumber&lt;/a&gt;. At first I wanted to add cucuber formatter class to &lt;a href="https://github.com/jeffkreeftmeijer/fuubar"&gt;Jeff’s gem&lt;/a&gt;, but later I decided to make it a separet gam because of gem dependencies.&lt;/p&gt;
&lt;p&gt;This is where &lt;a href="https://github.com/martinciu/fuubar-cucumber"&gt;fuubar-cucumber&lt;/a&gt; came from. Installation is simple. Just add it to your gem file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;fuubar-cucumber&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;or install manualy&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;gem install fuubar-cucumber
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and from now on you can run your cucumber features with ‘–format Cucumber::Formatter::Fuubar’ option. Hope you find it useful.&lt;/p&gt;



&lt;img src="http://feeds.feedburner.com/~r/martinciu/~4/IRdFOczeC-g" height="1" width="1"/&gt;</content>



 <feedburner:origLink>http://martinciu.com/2010/10/fuubar-cucumber.html</feedburner:origLink></entry>
 
 
</feed>
