HTTP User Agent parser
Ruby
Pull request Compare This branch is 1 commit ahead, 31 commits behind jilion:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.travis.yml
CHANGELOG.md
Gemfile
Guardfile
LICENSE
README.md
Rakefile
useragent.gemspec

README.md

UserAgent Build Status

UserAgent is a Ruby library that parses and compares HTTP User Agents.

Detected platforms

UserAgent can detect the following platforms in any given User Agent string: Nintendo Wii, WebTV, Windows, iPad, iPod, iPhone Simulator, iPhone, SunOS, webOS, Macintosh, Android, FreeBSD, OpenBSD, NetBSD, Linux, Nokia, BlackBerry, OpenSolaris, OS/2, BeOS, AIX and X11.

Detected operating systems

UserAgent can detect the following operating systems (and versions, except for the Windows) in any given User Agent string: Windows 7, Windows Vista, Windows Server 2003, Windows XP, Windows 2000, Service Pack 1 (SP1), Windows NT, Windows NT 4.0, Windows NT 4.0, Windows NT, Windows 2000, Windows Me, FreeBSD, OpenBSD, NetBSD, SunOS, BeOS, OS/2, WebTV, Linux, AmigaOS and BlackBerryOS.

Detected linux distributions

UserAgent can detect the following linux distributions (and versions) in any given User Agent string: Debian, Kubuntu, Ubuntu, Fedora, SUSE, Gentoo and Mandriva.

Detected languages

UserAgent can detect languages/countries based on the ISO-639-2 list (for language codes) and the ISO-3166-1 list (for country codes) in any given User Agent string. The detected formats are "en", "en-US" and "en_US".

Examples

Retrieve User Agent information from a User Agent string

# Safari 4.0.4 on iPad 3.2
user_agent = UserAgent.parse("Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10")

user_agent.gecko?         # => false
user_agent.webkit?        # => true
user_agent.browser        # => "Safari"
user_agent.version        # => "4.0.4"
user_agent.webkit_version # => "531.21.10"
user_agent.build          # => "531.21.10"
user_agent.platform       # => "iPad"
user_agent.os             # => "iOS 3.2"
user_agent.language       # => "en-US"
user_agent.security       # => :strong
user_agent.mobile?        # => true

# Firefox 3.1 on Ubuntu 8.04
user_agent = UserAgent.parse("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.2) Gecko/2008092313 Ubuntu/8.04 (hardy) Firefox/3.1")

user_agent.gecko?             # => true
user_agent.webkit?            # => false
user_agent.browser            # => "Firefox"
user_agent.version            # => "3.1"
user_agent.gecko_version      # => "2008092313"
user_agent.platform           # => "Linux"
user_agent.os                 # => "Linux i686"
user_agent.linux_distribution # => "Ubuntu 8.04"
user_agent.language           # => "en-US"
user_agent.security           # => :strong
user_agent.mobile?            # => false

# Opera Mini 5.1.22784
user_agent = UserAgent.parse("Opera/9.80 (Series 60; Opera Mini/5.1.22784/22.394; U; en) Presto/2.5.25 Version/10.54")

user_agent.gecko?             # => false
user_agent.webkit?            # => false
user_agent.browser            # => "Opera Mini"
user_agent.version            # => "5.1.22784"
user_agent.platform           # => nil
user_agent.os                 # => nil
user_agent.linux_distribution # => nil
user_agent.language           # => "en"
user_agent.security           # => :strong
user_agent.mobile?            # => true

Comparisons of browsers

Browser = Struct.new(:browser, :version)
SupportedBrowsers = [
  Browser.new("Safari", "3.1.1"),
  Browser.new("Firefox", "2.0.0.14"),
  Browser.new("Internet Explorer", "7.0")
]

user_agent = UserAgent.parse("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13")

SupportedBrowsers.detect { |browser| user_agent >= browser } # => #<struct Browser browser="Firefox", version="2.0.0.14">

user_agent > SupportedBrowsers[0] # => false
user_agent > SupportedBrowsers[1] # => true
user_agent > SupportedBrowsers[2] # => false

Copyright (c) 2011 Joshua Peek, released under the MIT license