Skip to content

k0kubun/rack-user_agent

Repository files navigation

Rack::UserAgent test

Rack::Request extension for handling User-Agent.
Thanks to woothee, Rack::UserAgent supports various User-Agents.

Installation

Add this line to your application's Gemfile:

gem 'rack-user_agent'

Usage

Rails

If you bundle "rack-user_agent", Rack::UserAgent will be automatically inserted when started.
Additional methods for request will be available with no cost.

class ApplicationController < ActionController::Base
  before_action :set_request_variant

  def index
    request.user_agent       #=> "Mozilla/5.0 (Macintosh; ..."
    request.device_type      #=> :pc
    request.os               #=> "Mac OSX"
    request.browser          #=> "Chrome"
    request.from_pc?         #=> true
    request.from_smartphone? #=> false
  end

  private

  # It is useful for Action Pack variants, which is new feature from Rails 4.1.
  # You can switch view templates by +pc or +smartphone in file name.
  # http://guides.rubyonrails.org/4_1_release_notes.html#action-pack-variants
  def set_request_variant
    request.variant = request.device_variant # :pc, :smartphone
  end
end

Sinatra

You can also manually use Rack::UserAgent for any Rack apps.

require "sinatra"
require "rack/user_agent"

configure do
  use Rack::UserAgent
end

get "/" do
  request.user_agent       #=> "Mozilla/5.0 (Linux; Android 4.3; Nexus 7 ..."
  request.device_type      #=> :smartphone
  request.os               #=> "Android"
  request.os_version       #=> "4.3"
  request.browser          #=> "Chrome"
  request.browser_version  #=> "29.0.1547.72"
  request.from_smartphone? #=> true
end

Available Methods

Attributes

  • request.device_type
  • request.os
  • request.os_version
  • request.browser
  • request.browser_version
  • request.browser_vendor

Helpers

  • request.from_pc?
  • request.from_smartphone?
  • request.from_mobilephone?
  • request.from_appliance?
  • request.from_crawler?

Detectors

APIs compatible with rack-smartphone_detector

  • request.smartphone_version
  • request.from_iphone?
  • request.from_ipad?
  • request.from_ipod?
  • request.from_android?
  • request.from_android_tablet?
  • request.from_windows_phone?
  • request.from_ios?
  • request.from_android_os?

License

MIT License

About

Rack::Request extension for handling User-Agent

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages