Skip to content

Instantly share code, notes, and snippets.

@robdimarco
Created August 23, 2012 14:18
Show Gist options
  • Save robdimarco/3437062 to your computer and use it in GitHub Desktop.
Save robdimarco/3437062 to your computer and use it in GitHub Desktop.
Simple method to test whether a user agent is a bot or not.
protected
def bot?
# Simple check to match if the user agent is a bot. We look for anything
# that has the pattern bot/ (e.g. Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html))
# or that does not start with Mozilla / Opera (e.g. AdsBot-Google (+http://www.google.com/adsbot.html))
# This could probably be combined in to one regex, but couldn't figure out how to do it quickly.
%r[bot/] =~ request.user_agent || %r{^(?!Mozilla|Opera)} =~ request.user_agent
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment