simplify user-agent detection in Rails
Ruby
Switch branches/tags
Nothing to show
Pull request Compare This branch is 1 commit ahead, 1 commit behind notch8:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
doc
lib
tests
.gitignore
README.rdoc
Rakefile
VERSION
user_agent_info.gemspec

README.rdoc

UserAgentInfo

Author

Mehmet Celik (mehmet at celik.be)

simplify user-agent detection in Rails

Install

Hosted on Gemcutter

gem install user_agent_info Your Rails app add next line to config/environment.rb config.gem “user_agent_info”

Usage

It extends ActionController::Request and parses the requests HTTP_USER_AGENT header so it can be queried. The supported browsers are IE, Firefox, Chrome, Opera

Example

class UpgradeBrowserController < ApplicationController

		def index
  			@upgrade = request.user_agent_info.isIE_or_worse?('6.0')   
		end

end

More Examples

Depending on your browser you could get output like this request.user_agent_info ==> Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4 request.user_agent_info ==> Macintosh request.user_agent_info ==> Firefox request.user_agent_info ==> Mozilla Firefox request.user_agent_info ==> Intel Mac OS X 10.5 request.user_agent_info ==> en-US request.user_agent_info ==> 3.5 request.user_agent_info ==> Gecko/20091016

Query for specific browsers request.user_agent_info.isIE? ==> true if browser is Internet Explorer request.user_agent_info.isFirefox? ==> true if browser is Mozilla Firefox request.user_agent_info.isChrome? ==> true if browser is Google Chrome request.user_agent_info.isOpera? ==> true if browser is Opera

You can also query if it is better or worse then a certain browser version. The example below uses IE but you can replace it by Firefox, Chrome or Opera request.user_agent_info.isIE?('6.0') ==> true if browser is Internet Explorer version 6.0 '==' request.user_agent_info.isIE_or_worse?('6.0') ==> true if browser is Internet Explorer version worse or equal to 6.0 '<=' request.user_agent_info.isIE_and_worse?('6.0') ==> true if browser is Internet Explorer version worse then 6.0 '<' request.user_agent_info.isIE_or_better?('6.0') ==> true if browser is Internet Explorer version better or equal to 6.0 '>=' request.user_agent_info.isIE_and_better?('6.0') ==> true if browser is Internet Explorer version better then 6.0 '>'