GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ua-parser/uap-ruby at 7bb5be9b8fb92a9d867515b1c6161111c5645122

BranchesTags
Open more actions menu

Latest commit

 

History

134 Commits
134 Commits

Folders and files

NameName
Last commit message
Last commit date
bin
bin
 
 
lib
lib
 
 
spec
spec
 
 
vendor
vendor
 
 
.gitignore
.gitignore
 
 
.gitmodules
.gitmodules
 
 
.travis.yml
.travis.yml
 
 
Gemfile
Gemfile
 
 
MIT-LICENSE
MIT-LICENSE
 
 
Rakefile
Rakefile
 
 
Readme.md
Readme.md
 
 
user_agent_parser.gemspec
user_agent_parser.gemspec
 
 

Repository files navigation

UserAgentParser Build Status

UserAgentParser is a simple, comprehensive Ruby gem for parsing user agent strings. It uses BrowserScope's parsing patterns.

Supported Rubies

  • Ruby 2.1
  • Ruby 2.0
  • Ruby 1.9.3
  • Ruby 1.9.2
  • JRuby

Installation

$ gem install user_agent_parser

Example usage

require 'user_agent_parser'
=> true
user_agent = UserAgentParser.parse 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0;)'
=> #<UserAgentParser::UserAgent IE 9.0 (Windows Vista)>
user_agent.to_s
=> "IE 9.0"
user_agent.family
=> "IE"
user_agent.version.to_s
=> "9.0"
user_agent.version.major
=> "9"
user_agent.version.minor
=> "0"
operating_system = user_agent.os
=> #<UserAgentParser::OperatingSystem Windows Vista>
operating_system.to_s
=> "Windows Vista"

# The parser database will be loaded and parsed on every call to
# UserAgentParser.parse. To avoid this, instantiate your own Parser instance.
parser = UserAgentParser::Parser.new
parser.parse 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0;)'
=> #<UserAgentParser::UserAgent IE 9.0 (Windows Vista)>
parser.parse 'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.5.24 Version/10.53'
=> #<UserAgentParser::UserAgent Opera 10.53 (Windows XP)>

In a larger application, you could store a parser in a global to avoid repeat pattern loading:

module MyApplication

  # Instantiate the parser on load as it's quite expensive
  USER_AGENT_PARSER = UserAgentParser::Parser.new

  def self.user_agent_parser
    USER_AGENT_PARSER
  end

end

The pattern database

The ua-parser database is included via a git submodule. To update the database the submodule needs to be updated and the gem re-released (pull requests for this are very welcome!).

You can also specify the path to your own, updated and/or customised regexes.yaml file as a second argument to UserAgentParser.parse:

UserAgentParser.parse(ua_string, patterns_path: '/some/path/to/regexes.yaml')

or when instantiating a UserAgentParser::Parser:

UserAgentParser::Parser.new(patterns_path: '/some/path/to/regexes.yaml').parse(ua_string)

Command line tool

The gem incldes a user_agent_parser bin command which will read from standard input, parse each line and print the result, for example:

$ cat > SOME-FILE-WITH-USER-AGENTS.txt
USER_AGENT_1
USER_AGENT_2
...
$ cat SOME-FILE-WITH-USER-AGENTS.txt | user_agent_parser --format '%f %M' | distribution

See user_agent_parser -h for more information.

Contributing

  1. Fork
  2. Hack
  3. rake test
  4. Send a pull request

All accepted pull requests will earn you commit and release rights.

Releasing a new version

  1. Update the version in user_agent_parser.gemspec

  2. git commit user_agent_parser.gemspec with the following message format:

     Version x.x.x
    
     Changelog:
     * Some new feature
     * Some new bug fix
    
  3. rake release

  4. Create a new Github release

License

MIT

About

A simple, comprehensive Ruby gem for parsing user agent strings with the help of BrowserScope's UA database

Topics

browserscopeparserubyuseragentuseragentparser

Resources

Readme
License
Activity
Custom properties

Stars

260 stars

Watchers

5 watching

Forks

42 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.