Time ago in words (minutes, hours, days, weeks, months ago in words)
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 '< 5 minutes' 8 when 5..14 then '< 15 minutes' 9 when 15..29 then '< 30 minutes' 10 when 30..59 then '> 30 minutes' 11 when 60..119 then '> 1 hour' 12 when 120..239 then '> 2 hours' 13 when 240..479 then '> 4 hours' 14 when 480..719 then '> 8 hours' 15 when 720..1439 then '> 12 hours' 16 when 1440..11519 then '> ' << pluralize((minutes/1440).floor, 'day') 17 when 11520..43199 then '> ' << pluralize((minutes/11520).floor, 'week') 18 when 43200..525599 then '> ' << pluralize((minutes/43200).floor, 'month') 19 else '> ' << pluralize((minutes/525600).floor, 'år') 20 end 21 endThere are also similar implementations:
- http://www.actsasflinn.com/articles/2007/04/10/time-ago-method-for-ruby-on-rails
- http://timeago.yarp.com/
How to detect traffic from the most common search spiders with Ruby
- 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.
How to exclude your own traffic from Google Analytics reports and other JavaScript based analytics software
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.
Restricting CPU usage for Autotest
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
Vlad deployment recipe for Phusion Passenger
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