Register now and start sharing your code snippets.

Allowing URL's in a route.

Ruby posted 28 days ago by marko

I wanted to create a route that could accept an URL as a parameter. The problem was that dots and slashes were interpreted as separators for the route. Luckily I managed to find this post that explained how it could be bypassed.

   1  map.connect ':scale/:text.:format', :controller => 'barcode', :requirements => { :text => /.*/ }

With this route set up I now could catch parameters like this: http://localhost:3000/200×200/http://aktagon.com.png, where 200×200 is :scale, http://aktagon.com is :text and .png is :format.

Tagged rails routes, url in route

Time ago in words (minutes, hours, days, weeks, months ago in words)

Ruby posted about 1 month ago by christian

   1  def minutes_in_words(timestamp)
   2      minutes = (((Time.now - timestamp).abs)/60).round
   3      
   4      return nil if minutes < 0
   5      
   6      case minutes
   7        when 0..4            then '&lt; 5 minutes'
   8        when 5..14           then '&lt; 15 minutes'
   9        when 15..29          then '&lt; 30 minutes'
  10        when 30..59          then '&gt; 30 minutes'
  11        when 60..119         then '&gt; 1 hour'
  12        when 120..239        then '&gt; 2 hours'
  13        when 240..479        then '&gt; 4 hours'
  14        when 480..719        then '&gt; 8 hours'
  15        when 720..1439       then '&gt; 12 hours'
  16        when 1440..11519     then '&gt; ' << pluralize((minutes/1440).floor, 'day')
  17        when 11520..43199    then '&gt; ' << pluralize((minutes/11520).floor, 'week')
  18        when 43200..525599   then '&gt; ' << pluralize((minutes/43200).floor, 'month')  
  19        else                      '&gt; ' << pluralize((minutes/525600).floor, 'år')
  20      end
  21    end

There are also similar implementations:
  1. http://www.actsasflinn.com/articles/2007/04/10/time-ago-method-for-ruby-on-rails
  2. http://timeago.yarp.com/
Tagged month, week, hour, minutes, words, rails, ruby

How to detect traffic from the most common search spiders with Ruby

Ruby posted about 1 month ago by christian
This snippet detects traffic from the following bots, which is enough for me:
  • Google – Googlebot/2.1 ( http://www.googlebot.com/bot.html)
  • Google Image – Googlebot-Image/1.0 ( http://www.googlebot.com/bot.html)
  • MSN Live – msnbot-Products/1.0 (+http://search.msn.com/msnbot.htm)
  • Yahoo – Mozilla/5.0 (compatible; Yahoo! Slurp;)

The code (via):

   1  user_agent = request.user_agent.downcase
   2  @bot = [ 'msnbot', 'yahoo! slurp','googlebot' ].detect { |bot| user_agent.include? bot }

When the Google bot visists your site the @bot string will contain ‘googlebot’.

If you need to detect more bots than these, then the user-agents.org site contains a list of various user agents for both bots and browsers.

Tagged spider, web crawler, bot, search, user agent, detect

How to exclude your own traffic from Google Analytics reports and other JavaScript based analytics software

Ruby posted about 1 month ago by christian

Option 1: Changing your browser’s user agent

Open the about:config page in Firefox by typing about:config in the address bar and pressing enter. Now change the general.useragent.extra.firefox setting to an easily identifiable string, for example the following:

   1  Firefox/3.0 disable-tracking

Then in your code check that the user-agent string doesn’t contain disable-tracking>

   1  <% if !request.user_agent.include?('disable-tracking') %>
   2  TRACKING CODE GOES HERE
   3  <% end %>

Option 2:

Use one of Google Analytics native ways of excluding traffic from certain domains, IPs, user-agents or users having a specific browser cookie.

Tagged google, analytics, tracking, exclude, traffic

Restricting CPU usage for Autotest

Ruby posted 2 months ago by marko

Autotest sucks by default up all the cpu cycles. I found a solution for it here.

Add this to $HOME/.autotest

   1  Autotest.add_hook :initialize do |at|
   2    at.sleep = 5
   3  end

Tagged autotest 100% cpu problem