Skip to content

Added is_bot? convenience method #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/simple-useragent.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions spec/simple-useragent_spec.rb
Original file line number Diff line number Diff line change
@@ -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