https://github.com/fnando/browser
Do some browser detection with Ruby. Includes ActionController integration.
https://github.com/fnando/browser
Keywords
browser-detection rails ruby user-agent
Keywords from Contributors
activerecord activejob mvc rubygem crash-reporting ruby-gem rspec rack geocoding geocoding-api
Last synced: about 22 hours ago
JSON representation
Repository metadata
Do some browser detection with Ruby. Includes ActionController integration.
- Host: GitHub
- URL: https://github.com/fnando/browser
- Owner: fnando
- License: mit
- Created: 2010-07-16T17:25:01.000Z (over 15 years ago)
- Default Branch: main
- Last Pushed: 2025-06-10T23:37:39.000Z (9 months ago)
- Last Synced: 2026-02-24T14:18:35.264Z (7 days ago)
- Topics: browser-detection, rails, ruby, user-agent
- Language: Ruby
- Homepage:
- Size: 942 KB
- Stars: 2,493
- Watchers: 42
- Forks: 365
- Open Issues: 18
- Releases: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
README.md
Browser
Do some browser detection with Ruby. Includes ActionController integration.
Installation
gem install browser
Usage
require "browser"
browser = Browser.new("Some User Agent", accept_language: "en-us")
# General info
browser.bot?
browser.chrome?
browser.chromium_based?
browser.core_media?
browser.duck_duck_go?
browser.edge? # Newest MS browser
browser.electron? # Electron Framework
browser.firefox?
browser.full_version
browser.ie?
browser.ie?(6) # detect specific IE version
browser.ie?([">8", "<10"]) # detect specific IE (IE9).
browser.known? # has the browser been successfully detected?
browser.unknown? # the browser wasn't detected.
browser.meta # an array with several attributes
browser.name # readable browser name
browser.nokia?
browser.opera?
browser.opera_mini?
browser.phantom_js?
browser.quicktime?
browser.safari?
browser.safari_webapp_mode?
browser.samsung_browser?
browser.to_s # the meta info joined by space
browser.uc_browser?
browser.version # major version number
browser.webkit?
browser.webkit_full_version
browser.yandex?
browser.wechat?
browser.qq?
browser.weibo?
browser.sputnik?
browser.sougou_browser?
browser.epiphany?
# Get bot info
browser.bot.name
browser.bot.search_engine?
browser.bot?
browser.bot.why? # shows which matcher detected this user agent as a bot.
Browser::Bot.why?(ua)
# Get device info
browser.device
browser.device.id
browser.device.name
browser.device.unknown?
browser.device.blackberry_playbook?
browser.device.console?
browser.device.ipad?
browser.device.iphone?
browser.device.ipod_touch?
browser.device.kindle?
browser.device.kindle_fire?
browser.device.mobile?
browser.device.nintendo?
browser.device.playstation?
browser.device.ps3?
browser.device.ps4?
browser.device.psp?
browser.device.silk?
browser.device.surface?
browser.device.tablet?
browser.device.tv?
browser.device.vita?
browser.device.wii?
browser.device.wiiu?
browser.device.samsung?
browser.device.switch?
browser.device.xbox?
browser.device.xbox_360?
browser.device.xbox_one?
# Get platform info
browser.platform
browser.platform.id
browser.platform.name
browser.platform.version # e.g. 9 (for iOS9)
browser.platform.adobe_air?
browser.platform.android?
browser.platform.android?(4.2) # detect Android Jelly Bean 4.2
browser.platform.android_app? # detect webview in an Android app
browser.platform.android_webview? # alias for android_app?
browser.platform.blackberry?
browser.platform.blackberry?(10) # detect specific BlackBerry version
browser.platform.chrome_os?
browser.platform.firefox_os?
browser.platform.ios? # detect iOS
browser.platform.ios?(9) # detect specific iOS version
browser.platform.ios_app? # detect webview in an iOS app
browser.platform.ios_webview? # alias for ios_app?
browser.platform.linux?
browser.platform.mac?
browser.platform.unknown?
browser.platform.windows10?
browser.platform.windows7?
browser.platform.windows8?
browser.platform.windows8_1?
browser.platform.windows?
browser.platform.windows_mobile?
browser.platform.windows_phone?
browser.platform.windows_rt?
browser.platform.windows_touchscreen_desktop?
browser.platform.windows_vista?
browser.platform.windows_wow64?
browser.platform.windows_x64?
browser.platform.windows_x64_inclusive?
browser.platform.windows_xp?
browser.platform.kai_os?
Aliases
To add aliases like mobile? and tablet? to the base object (e.g
browser.mobile?), require the browser/aliases file and extend the
Browser::Base object like the following:
require "browser/aliases"
Browser::Base.include(Browser::Aliases)
browser = Browser.new("Some user agent")
browser.mobile? #=> false
What's being detected?
- For a list of platform detections, check
lib/browser/platform.rb - For a list of device detections, check
lib/browser/device.rb - For a list of bot detections, check
bots.yml
Detecting modern browsers
To detect whether a browser can be considered as modern or not, create a method
that abstracts your versioning constraints. The following example will consider
any of the following browsers as a modern:
# Expects an Browser instance,
# like in `Browser.new(user_agent, accept_language: language)`.
def modern_browser?(browser)
[
browser.chrome?(">= 65"),
browser.safari?(">= 10"),
browser.firefox?(">= 52"),
browser.ie?(">= 11") && !browser.compatibility_view?,
browser.edge?(">= 15"),
browser.opera?(">= 50"),
browser.facebook?
&& browser.safari_webapp_mode?
&& browser.webkit_full_version.to_i >= 602
].any?
end
Rails integration
Just add it to the Gemfile.
gem "browser"
This adds a helper method called browser, that inspects your current user
agent.
<% if browser.ie?(6) %>
<p class="disclaimer">You're running an older IE version. Please update it!</p>
<% end %>
If you want to use Browser on your Rails app but don't want to taint your
controller, use the following line on your Gemfile:
gem "browser", require: "browser/browser"
Accept Language
Parses the accept-language header from an HTTP request and produces an array of
language objects sorted by quality.
browser = Browser.new("Some User Agent", accept_language: "en-us")
browser.accept_language.class
#=> Array
language = browser.accept_language.first
language.code
#=> "en"
language.region
#=> "US"
language.full
#=> "en-US"
language.quality
#=> 1.0
language.name
#=> "English/United States"
Result is always sorted in quality order from highest to lowest. As per the HTTP
spec:
- omitting the quality value implies 1.0.
- quality value equal to zero means that is not accepted by the client.
Internet Explorer
Internet Explorer has a compatibility view mode that allows newer versions
(IE8+) to run as an older version. Browser will always return the navigator
version, ignoring the compatibility view version, when defined. If you need to
get the engine's version, you have to use Browser#msie_version and
Browser#msie_full_version.
So, let's say an user activates compatibility view in a IE11 browser. This is
what you'll get:
browser.version
#=> 11
browser.full_version
#=> 11.0
browser.msie_version
#=> 7
browser.msie_full_version
#=> 7.0
browser.compatibility_view?
#=> true
This behavior changed in v1.0.0; previously there wasn't a way of getting the
real browser version.
Safari
iOS webviews and web apps aren't detected as Safari anymore, so be aware of that
if that's your case. You can use a combination of platform and webkit detection
to do whatever you want.
# iPad's Safari running as web app mode.
browser = Browser.new("Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405")
browser.safari?
#=> false
browser.webkit?
#=> true
browser.platform.ios?
#=> true
Bots
The bot detection is quite aggressive. Anything that matches at least one of the
following requirements will be considered a bot.
- Empty user agent string
- User agent that matches
/crawl|fetch|search|monitoring|spider|bot/ - Any known bot listed under
bots.yml
To add custom matchers, you can add a callable object to
Browser::Bot.matchers. The following example matches everything that has a
externalhit substring on it. The bot name will always be General Bot.
Browser::Bot.matchers << ->(ua, _browser) { ua.match?(/externalhit/i) }
To clear all matchers, including the ones that are bundled, use
Browser::Bot.matchers.clear. You can re-add built-in matchers by doing the
following:
Browser::Bot.matchers += Browser::Bot.default_matchers
To restore v2's bot detection, remove the following matchers:
Browser::Bot.matchers.delete(Browser::Bot::KeywordMatcher)
Browser::Bot.matchers.delete(Browser::Bot::EmptyUserAgentMatcher)
To extend the bot list, you can manipulate the methods below:
Browser::Bot.bots.merge!(new_bots_hash)
Browser::Bot.bot_exceptions += new_exceptions
Browser::Bot.search_engines.merge!(new_search_engines_hash)
Middleware
You can use the Browser::Middleware to redirect user agents.
use Browser::Middleware do
redirect_to "/upgrade" if browser.ie?
end
If you're using Rails, you can use the route helper methods. Just add something
like the following to a initializer file (config/initializers/browser.rb).
Rails.configuration.middleware.use Browser::Middleware do
redirect_to upgrade_path if browser.ie?
end
If you need access to the Rack::Request object (e.g. to exclude a path), you
can do so with request.
Rails.configuration.middleware.use Browser::Middleware do
redirect_to upgrade_path if browser.ie? && request.env["PATH_INFO"] != "/exclude_me"
end
Restrictions
- User agent has a size limit of 2048 bytes. This can be customized through
Browser.user_agent_size_limit=(size). - Accept-Language has a size limit of 2048 bytes. This can be customized through
Browser.accept_language_size_limit=(size).
If size is not respected, then Browser::Error is raised.
Browser.user_agent_size_limit = 4096
Browser.accept_language_size_limit = 4096
Development
Versioning
This library follows http://semver.org.
Writing code
Once you've made your great commits (include tests, please):
- Fork browser
- Create a topic branch -
git checkout -b my_branch - Push to your branch -
git push origin my_branch - Create a pull request
- That's it!
Please respect the indentation rules and code style. And use 2 spaces, not tabs.
And don't touch the version thing.
Configuring environment
To configure your environment, you must have Ruby and bundler installed. Then
run bundle install to install all dependencies.
To run tests, execute ./bin/rake.
Adding new features
Before using your time to code a new feature, open a ticket asking if it makes
sense and if it's on this project's scope.
Don't forget to add a new entry to CHANGELOG.md.
Adding a new bot
- Add the user agent to
test/ua_bots.yml. - Add the readable name to
bots.yml. The key must be something that matches
the user agent, in lowercased text. - Run tests.
Don't forget to add a new entry to CHANGELOG.md.
Adding a new search engine
- Add the user agent to
test/ua_search_engines.yml. - Add the same user agent to
test/ua_bots.yml. - Add the readable name to
search_engines.yml. The key must be something that
matches the user agent, in lowercased text. - Run tests.
Don't forget to add a new entry to CHANGELOG.md.
Wrong browser/platform/device detection
If you know how to fix it, follow the "Writing code" above. Open an issue
otherwise; make sure you fill in the issue template with all the required
information.
Maintainer
- Nando Vieira - https://nandovieira.com
Contributors
License
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the 'Software'), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Owner metadata
- Name: Nando Vieira
- Login: fnando
- Email:
- Kind: user
- Description: Father, husband, software developer, 🇧🇷🇨🇦, he/him/ele, punk rocker, amateur guitar/bass player. Everything is fine.🔥
- Website: https://nandovieira.com
- Location: Vancouver, Canada
- Twitter:
- Company: @stellar
- Icon url: https://avatars.githubusercontent.com/u/3009?u=951a4f4b949afb51e006f6d80b1a6075f99e48cc&v=4
- Repositories: 315
- Last ynced at: 2025-11-29T23:09:14.220Z
- Profile URL: https://github.com/fnando
GitHub Events
Total
- Delete event: 2
- Pull request event: 13
- Fork event: 10
- Issues event: 9
- Watch event: 41
- Issue comment event: 18
- Push event: 6
- Pull request review event: 1
- Create event: 4
Last Year
- Pull request event: 1
- Fork event: 3
- Watch event: 22
- Issue comment event: 1
Committers metadata
Last synced: 2 days ago
Total Commits: 539
Total Committers: 108
Avg Commits per committer: 4.991
Development Distribution Score (DDS): 0.267
Commits in past year: 3
Committers in past year: 2
Avg Commits per committer in past year: 1.5
Development Distribution Score (DDS) in past year: 0.333
| Name | Commits | |
|---|---|---|
| Nando Vieira | f****a@g****m | 395 |
| itsalongstory | l****2@g****m | 7 |
| Joe Lencioni | j****i@g****m | 5 |
| Claudio Fiorini | c****i@g****m | 5 |
| Michal Hantl | m****l@g****m | 4 |
| Paul Gallagher | g****l@g****m | 3 |
| Stuart Williams | s****t@s****a | 3 |
| villins | l****2@g****m | 3 |
| Alexander Popov | a****r@g****m | 2 |
| Alexandru Stoian | h****k@h****o | 2 |
| Milan Dobrota | m****n@m****m | 2 |
| Omri | o****n | 2 |
| Paolo Dona | p****a@g****m | 2 |
| Peter Marsh | p****h@g****m | 2 |
| Prem Sichanugrist | s@s****u | 2 |
| Renaud Chaput | r****p@g****m | 2 |
| Rodrigo Ra | r****p@g****m | 2 |
| Ross Noble | r****e@g****m | 2 |
| Vincent Pochet | v****t@g****m | 2 |
| coffeebite | me@c****m | 2 |
| Roger Campos | r****r@i****t | 2 |
| giacomomacri | g****i@d****m | 2 |
| Chris Marshall | c****s@c****m | 1 |
| Casey Lang | c****4@g****m | 1 |
| CJ Kihlbom | cj@e****e | 1 |
| Arty Gus | a****s | 1 |
| Anton Rieder | 1****r | 1 |
| Andrew Nesbitt | a****z@g****m | 1 |
| Andrew Hooker | G****e@g****m | 1 |
| Akira Matsuda | r****e@d****p | 1 |
| and 78 more... | ||
Committer domains:
- jammer.fm: 2
- dealbase.com: 2
- lumoslabs.com: 1
- pobox.com: 1
- netguru.pl: 1
- gauravtiwari.co.uk: 1
- stillhope.com: 1
- dio.jp: 1
- elabs.se: 1
- chrismar035.com: 1
- develon.com: 1
- itnig.net: 1
- coffeebite.com: 1
- sikac.hu: 1
- milandobrota.com: 1
- herk.ro: 1
- sidereal.ca: 1
- pfeiffer.dk: 1
- rankia.com: 1
- outerim.com: 1
- wanko.cc: 1
- oboxodo.com: 1
- bayekeji.com: 1
- spernj.org: 1
- roguepod.com: 1
- mdsol.com: 1
- qq.com: 1
- qmee.com: 1
- ignitionindustries.com: 1
- meisterlabs.com: 1
- socialreferral.com: 1
- getightower.com: 1
- homestay.com: 1
- gmx.de: 1
Issue and Pull Request metadata
Last synced: 19 days ago
Total issues: 52
Total pull requests: 73
Average time to close issues: 3 months
Average time to close pull requests: 5 months
Total issue authors: 43
Total pull request authors: 40
Average comments per issue: 1.69
Average comments per pull request: 0.78
Merged pull request: 36
Bot issues: 0
Bot pull requests: 0
Past year issues: 7
Past year pull requests: 12
Past year average time to close issues: 4 days
Past year average time to close pull requests: 23 days
Past year issue authors: 7
Past year pull request authors: 8
Past year average comments per issue: 1.57
Past year average comments per pull request: 0.67
Past year merged pull request: 5
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- itsalongstory (8)
- mjankowski (2)
- luissimo (2)
- deneuxa (1)
- feliperaul (1)
- salmehdhar (1)
- ishields (1)
- Landwookie (1)
- esmyllyaho90 (1)
- danfishgold (1)
- ddlogesh (1)
- joeltaylor (1)
- tomash (1)
- fnando (1)
- aleksander-tatskiy (1)
Top Pull Request Authors
- fnando (15)
- itsalongstory (11)
- gogainda (7)
- victoreduardo (3)
- andrew (2)
- sikachu (2)
- milandobrota (2)
- mjankowski (2)
- a5ob7r (2)
- carl033013 (2)
- williantenfen (2)
- westonganger (2)
- conarro (2)
- igor-drozdov (2)
- amatsuda (1)
Top Issue Labels
- can't fix (2)
- duplicate (1)
Top Pull Request Labels
Package metadata
- Total packages: 13
-
Total downloads:
- rubygems: 264,824,434 total
- Total docker downloads: 1,161,097,992
- Total dependent packages: 56 (may contain duplicates)
- Total dependent repositories: 4,533 (may contain duplicates)
- Total versions: 178
- Total maintainers: 1
gem.coop: browser
Do some browser detection with Ruby.
- Homepage: https://github.com/fnando/browser
- Documentation: http://www.rubydoc.info/gems/browser/
- Licenses: MIT
- Latest release: 6.2.0 (published about 1 year ago)
- Last Synced: 2026-03-01T21:01:28.742Z (1 day ago)
- Versions: 56
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 132,417,017 Total
- Docker Downloads: 580,548,996
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.061%
- Downloads: 0.184%
- Maintainers (1)
-
Funding:
- https://github.com/sponsors/fnando
rubygems.org: browser
Do some browser detection with Ruby.
- Homepage: https://github.com/fnando/browser
- Documentation: http://www.rubydoc.info/gems/browser/
- Licenses: MIT
- Latest release: 6.2.0 (published about 1 year ago)
- Last Synced: 2026-03-01T04:05:42.192Z (2 days ago)
- Versions: 56
- Dependent Packages: 56
- Dependent Repositories: 4,533
- Downloads: 132,407,417 Total
- Docker Downloads: 580,548,996
-
Rankings:
- Docker downloads count: 0.19%
- Downloads: 0.199%
- Dependent repos count: 0.469%
- Dependent packages count: 0.487%
- Average: 0.637%
- Stargazers count: 1.012%
- Forks count: 1.464%
- Maintainers (1)
-
Funding:
- https://github.com/sponsors/fnando
proxy.golang.org: github.com/fnando/browser
- Homepage:
- Documentation: https://pkg.go.dev/github.com/fnando/browser#section-documentation
- Licenses: mit
- Latest release: v6.2.0+incompatible (published about 1 year ago)
- Last Synced: 2026-02-26T11:01:46.445Z (5 days ago)
- Versions: 56
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent packages count: 5.497%
- Average: 5.681%
- Dependent repos count: 5.866%
debian-10: ruby-browser
- Homepage: http://github.com/fnando/browser
- Documentation: https://packages.debian.org/buster/ruby-browser
- Licenses:
- Latest release: 2.5.3-1 (published 20 days ago)
- Last Synced: 2026-02-13T04:19:32.390Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-11: ruby-browser
- Homepage: http://github.com/fnando/browser
- Documentation: https://packages.debian.org/bullseye/ruby-browser
- Licenses:
- Latest release: 4.2.0-3 (published 20 days ago)
- Last Synced: 2026-02-13T08:18:54.344Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-12: ruby-browser
- Homepage: http://github.com/fnando/browser
- Documentation: https://packages.debian.org/bookworm/ruby-browser
- Licenses:
- Latest release: 4.2.0-3 (published 18 days ago)
- Last Synced: 2026-02-12T23:26:11.346Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
debian-13: ruby-browser
- Homepage: https://github.com/fnando/browser
- Documentation: https://packages.debian.org/trixie/ruby-browser
- Licenses:
- Latest release: 5.3.1-3 (published 19 days ago)
- Last Synced: 2026-02-13T13:13:44.875Z (18 days ago)
- Versions: 1
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 100%
Dependencies
- bundler >= 0 development
- minitest >= 0 development
- minitest-autotest >= 0 development
- minitest-utils >= 0 development
- pry-meta >= 0 development
- rack-test >= 0 development
- rails >= 0 development
- rake >= 0 development
- rubocop >= 0 development
- rubocop-fnando >= 0 development
- simplecov >= 0 development
- actions/cache v2 composite
- actions/checkout v2.4.0 composite
- ruby/setup-ruby v1 composite
Score: 33.58865592906244