Rack::Request extension for handling User-Agent.
Thanks to woothee, Rack::UserAgent supports various User-Agents.
Add this line to your application's Gemfile:
gem 'rack-user_agent'
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
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
- request.device_type
- request.os
- request.os_version
- request.browser
- request.browser_version
- request.browser_vendor
- request.from_pc?
- request.from_smartphone?
- request.from_mobilephone?
- request.from_appliance?
- request.from_crawler?
APIs compatible with rack-smartphone_detector
- request.smartphone_version
- request.from_iphone?
- request.from_ipad?
- request.from_android?
- request.from_android_tablet?
- request.from_windows_phone?
MIT License