Register now and start sharing your code snippets.
-->

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

Ruby posted 4 months 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 4 months 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 4 months 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 4 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

Vlad deployment recipe for Phusion Passenger

Ruby posted 5 months ago by christian

   1  #
   2  # General configuration
   3  #
   4  #set :ssh_flags,             '-p 110000'
   5  set :application,           'app.xxx'
   6  set :domain,                'x.x.x.x'
   7  set :deploy_to,             '/var/www/app.xxx'
   8  set :revision,              'master'
   9  set :repository,            '/var/lib/git/repositories/app.xxx/'
  10  
  11  
  12  namespace :vlad do
  13    set :app_command, "/etc/init.d/apache2"
  14   
  15    desc 'Restart Passenger'
  16    remote_task :start_app, :roles => :app do
  17      run "touch #{current_release}/tmp/restart.txt"
  18    end
  19    
  20    desc 'Restarts the apache servers'
  21    remote_task :start_web, :roles => :app do
  22      run "sudo #{app_command} restart"
  23    end
  24  end

Tagged vlad, phusion, passenger, apache, deployment