Created
August 23, 2012 14:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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