A framework agnostic browser detection and querying helper.
Brauser is a framework agnostic helper that helps you in targeting your applications against most diffused browsers.
Brauser comes with a Ruby on Rails hooks (more frameworks to follow), so for Rails you have just to add this to your Gemfile:
Once done that, every controller in your application will have a
browser method (also extended to views/layout via
If you don't use Rails, you can instantiate a new browser by including the gem in your code and by doing something like this:
browser = Brauser::Browser.new(USER_AGENT_HEADER, ACCEPT_LANGUAGE_HEADER)
where the first argument is the HTTP header
User-Agent, and the second is the HTTP header
For the rest of this document, let's assume you use Chrome 1.2.3 on Mac OS X.
Getting browser information
Once you instantiate the browser, you can query the browser about
You can also get readable name and platforms via
The version is handle via the versionomy gem.
The name and the platform are returned as
Symbol and can be in the range of names and engines registered via
The languages are returned as an hash where values are the priorities.
Also, you can get global information using
browser.classes. This will return an array or a string already formatted to be used in your views to scope your CSS.
For example, if you do this in a ERB view:
<body class="<%= browser.classes %>"> ...
The view will get compiled to this:
<body class="chrome version-1 version-1_2 version-1_2_3 platform-osx"> ...
And thus scoping your CSS will be trivial.
Querying the browser
Brauser supports querying about name, version, platform, language.
Name and platform support querying via the
== operator, which supports a single value or a list of values.
# We'll talk about the ending "?" later. browser.name == :chrome # => true browser.name == [:msie, :firefox] # => false
The version is delegated to the versionomy gem. You can use comparison operator. The right hand part must be either a
String or a
browser.version == "3" # => false browser.version >= "2" # => true
The language support querying via the
accepts? method, which supports a single value or a list of values.
browser.accepts?(:it) # => true browser.accepts?(:it, :en) # => true
All the querying can be combined in the single method
browser.is?(name: :chrome, version: ">= 4", platform: [:osx, :windows], languages: :it) # => false
Name, platform and languages can be either symbols or array of symbols. Version must be a query in the form is
OPERATOR VALUE && ..,
OPERATOR is one of
["<", "<=", "=", "==", ">=", ">"] and value specifies the version.
Prevent old browsers to access the Rails application.
If you want to easily prevent a legacy browser to open your application, create a file
supported-browsers.yml in the
config folder with a similar content:
chrome: 29 firefox: 28 safari: 6.1 msie: 11
then create a filter in the
class ApplicationController < ActionController::Base # ... before_filter do redirect_to(URL) unless browser.supported?(Rails.root + "config/supported-browsers.yml") end # ... end
and you are set.
Adding new browsers, platform and languages.
To add new browsers, simply call
::Brauser::Definitions.register(:browser, :id, ...).
The first argument can be
The second argument is the id of the definition.
The remaining argument will be passed to the definition constructor.
For example, for Google Chrome the call should be:
Brauser::Definitions.register(:browsers, :chrome, "Chrome", /((chrome)|(chromium))/i, /(.+Chrom[a-z]+\/)([a-z0-9.]+)/i)
The API documentation can be found here.
Contributing to brauser
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
- Fork the project.
- Start a feature/bugfix branch.
- Commit and push until you are happy with your contribution.
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright (C) 2013 and above Shogun (email@example.com).
Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.