Tree @fresh-releases/main (Download .tar.gz)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | # UserAgentParser [![Build Status](https://github.com/ua-parser/uap-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/ua-parser/uap-ruby/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/ua-parser/uap-ruby/badge.svg)](https://coveralls.io/github/ua-parser/uap-ruby) UserAgentParser is a simple, comprehensive Ruby gem for parsing user agent strings. It uses [BrowserScope](http://www.browserscope.org/)'s [parsing patterns](https://github.com/ua-parser/uap-core). ## Supported Rubies * Ruby 3.2 * Ruby 3.1 * Ruby 3.0 * JRuby ## Installation ```bash $ gem install user_agent_parser ``` ## Example usage ```ruby 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" user_agent.family == "IE" && user_agent.version >= "9" => true operating_system = user_agent.os => #<UserAgentParser::OperatingSystem Windows Vista> operating_system.to_s => "Windows Vista" # Device information can also be determined from some devices user_agent = UserAgentParser.parse "Mozilla/5.0 (Linux; Android 7.0; SAMSUNG SM-G930T Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/5.0 Chrome/51.0.2704.106 Mobile Safari/537.36" => #<UserAgentParser::UserAgent Samsung Internet 5.0 (Android 7.0) (Samsung SM-G930T)> user_agent.device.family => "Samsung SM-G930T" user_agent.device.brand => "Samsung" user_agent.device.model => "SM-G930T" user_agent = UserAgentParser.parse "Mozilla/5.0 (iPad; CPU OS 10_2_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) GSA/23.1.148956103 Mobile/14D27 Safari/600.1.4" => #<UserAgentParser::UserAgent Mobile Safari 10.2.1 (iOS 10.2.1) (iPad)> irb(main):026:0> user_agent.device.family => "iPad" irb(main):027:0> user_agent.device.brand => "Apple" irb(main):028:0> user_agent.device.model => "iPad" # 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: ```ruby 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](https://github.com/ua-parser/uap-core/blob/master/regexes.yaml) is included via a [git submodule](http://help.github.com/submodules/). 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`: ```ruby UserAgentParser.parse(ua_string, patterns_path: '/some/path/to/regexes.yaml') ``` or when instantiating a `UserAgentParser::Parser`: ```ruby UserAgentParser::Parser.new(patterns_path: '/some/path/to/regexes.yaml').parse(ua_string) ``` Extending the standard database is possible by providing multiple files in `patterns_paths` (plural) array argument: ```ruby UserAgentParser::Parser.new(patterns_paths: [UserAgentParser::DefaultPatternsPath, '/some/path/to/regexes.yaml']) ``` ## 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: ```bash $ 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](https://github.com/ua-parser/uap-ruby/releases/new) ## License MIT |
Commit History @fresh-releases/main
0
»»
- Update watch file Daniel Leidert 4 years ago
- Whitespace fix Daniel Leidert 4 years ago
- Update packaging files Daniel Leidert 4 years ago
- Add d/.gitattributes Daniel Leidert 4 years ago
- Update d/ch Utkarsh Gupta 4 years ago
- Add salsa-ci.yml Utkarsh Gupta 4 years ago
- Update changelog file Samyak Jain 4 years ago
- Update rules file Samyak Jain 4 years ago
- Added metadata in upstream file Samyak Jain 4 years ago
- Update Copyright file Samyak Jain 4 years ago
- Update Control file Samyak Jain 4 years ago
- Import Debian changes 2.5.1-1 Samyak Jain 4 years ago
- Import Upstream version 2.5.1 Samyak Jain 4 years ago
0
»»