GithubHelp home page GithubHelp logo

useragent's Introduction

UserAgent

UserAgent is a Ruby tool that facilitates quick matches to the user agent.

Getting Started

  1. Create an instance of UserAgent passing in the actual HTTP_USER_AGENT
  2. Then you can then call methods such as webkit?, iphone?, windows?, etc
  3. 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 

Usage with Rails

  1. Place the user_agent.rb file in the "lib" directory of your app
  2. 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)
  1. 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
  1. 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

useragent's People

Contributors

egjiri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.