From cfbc141e90f0459667cc0dc9c711a0204b691f8f Mon Sep 17 00:00:00 2001 From: Tucker Sylvestro Date: Mon, 23 Apr 2012 12:57:09 -0400 Subject: [PATCH] Added is_bot? convenience method --- lib/simple-useragent.rb | 7 ++++++- spec/simple-useragent_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/simple-useragent.rb b/lib/simple-useragent.rb index bfa9119..77744cb 100755 --- a/lib/simple-useragent.rb +++ b/lib/simple-useragent.rb @@ -37,12 +37,17 @@ def self.is_apple?(request_or_user_agent) user_agent = get_user_agent(request_or_user_agent) !!(user_agent =~ /(Mobile\/.+Safari)/) || !!(user_agent =~ /OS\s+[X9]/) end - + + def self.is_bot?(request_or_user_agent) + user_agent = get_user_agent(request_or_user_agent) + !!(user_agent =~ BOT) + end def self.is_ie?(request_or_user_agent) user_agent = get_user_agent(request_or_user_agent) !!(user_agent =~ /MSIE/) end + # Some mobile browsers put the User-Agent in a HTTP-X header def self.get_user_agent(request_or_user_agent) return request_or_user_agent if request_or_user_agent.kind_of? String diff --git a/spec/simple-useragent_spec.rb b/spec/simple-useragent_spec.rb index 473cb9e..5717c6c 100644 --- a/spec/simple-useragent_spec.rb +++ b/spec/simple-useragent_spec.rb @@ -72,4 +72,11 @@ SimpleUserAgent::browser(ie8).should == 'ie8' end + it "correctly detects a bot user agent" do + bot = "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" + SimpleUserAgent::is_desktop?(bot).should == true + SimpleUserAgent::is_mobile?(bot).should == false + SimpleUserAgent::is_bot?(bot).should == true + end + end