Allowing URL's in a route.
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.
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