UserAgent is a Ruby tool that facilitates quick matches to the user agent.
- Create an instance of UserAgent passing in the actual HTTP_USER_AGENT
- Then you can then call methods such as webkit?, iphone?, windows?, etc
- This will try to match any string that ends with '?'
# Example code
user_agent = UserAgent.new("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31")
user_agent.webkit? # => true
user_agent.iphone? # => false
user_agent.windows? # => false
user_agent.mac? # => true
- Place the user_agent.rb file in the "lib" directory of your app
- Configure the autoload_paths to include the lib directory if not already so.
# Add this line of code to config/application.rb
config.autoload_paths += %W(#{Rails.root}/lib)
- Create an instance of UserAgent using a before_filter in the controllers that will need it or application_controller to be accessible to all. Get the HTTP_USER_AGENT directly from Rails.
class PublicController < ApplicationController
before_filter :initialize_user_agent
def initialize_user_agent
@user_agent = UserAgent.new(request.env['HTTP_USER_AGENT'])
end
# ...
end
- Use this instance variable in the above specified controller and associated views to easily check for Browser, OS, or Device matches using the same methods as earlier.
@user_agent.webkit? # => true
@user_agent.iphone? # => false
@user_agent.windows? # => false
@user_agent.mac? # => true
Enjoy