Ruby UAParser is a bridge to UAParser.js
gem install ua_parser
This library vendors in a local copy of UAParser.js. You can use this library with any future versions of UAParser.js by setting the UA_PARSER_SOURCE_PATH
environment variable:
export UA_PARSER_SOURCE_PATH=/path/to/ua-parser-js/dist/ua-parser.js
The ExecJS library is used to automatically choose the best JavaScript engine for your platform. Check out its README for a complete list of supported engines.
See https://github.com/faisalman/ua-parser-js for full usage information.
Given the following user agent...
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36"
You can produce the following...
UAParser.parse(user_agent)
Which produces the following object and hash
@user_agent_results=
{"ua"=>
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36",
"browser"=>{"name"=>"Chrome", "version"=>"55.0.2883.75", "major"=>"55"},
"engine"=>{"version"=>"537.36", "name"=>"WebKit"},
"os"=>{"name"=>"Mac OS", "version"=>"10.12.1"},
"device"=>{},
"cpu"=>{}}>
For the sake of convenience you can call the following methods
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36"
result = UAParser.parse(user_agent)
result.os.name
# => "Mac OS"
result.os.version
# => "10.12.1"
result.browser.name
# => Chrome
result.browser.version
# => 55.0.2883.75
result.browser.major
# => 55
result.engine.name
# => Webkit
result.engine.version
# => 537.36