Class: Browser
- Inherits:
-
Object
- Object
- Browser
- Defined in:
- lib/browser.rb,
lib/browser/rails.rb,
lib/browser/meta/id.rb,
lib/browser/meta/ie.rb,
lib/browser/version.rb,
lib/browser/meta/ios.rb,
lib/browser/meta/base.rb,
lib/browser/methods/ie.rb,
lib/browser/methods/tv.rb,
lib/browser/middleware.rb,
lib/browser/meta/mobile.rb,
lib/browser/meta/modern.rb,
lib/browser/meta/safari.rb,
lib/browser/meta/webkit.rb,
lib/browser/methods/bots.rb,
lib/browser/meta/platform.rb,
lib/browser/methods/mobile.rb,
lib/browser/methods/devices.rb,
lib/browser/methods/consoles.rb,
lib/browser/methods/language.rb,
lib/browser/methods/platform.rb,
lib/browser/action_controller.rb,
lib/browser/middleware/context.rb,
lib/browser/meta/generic_browser.rb,
lib/browser/middleware/context/additions.rb,
lib/browser/middleware/context/url_methods.rb
Defined Under Namespace
Modules: ActionController, Bots, Consoles, Devices, IE, Language, Meta, Mobile, Platform, Tv, Version Classes: Middleware, Railtie
Constant Summary collapse
- NAMES =
{ :android => "Android", :blackberry => "BlackBerry", :chrome => "Chrome", :core_media => "Apple CoreMedia", :firefox => "Firefox", :ie => "Internet Explorer", :ipad => "iPad", :iphone => "iPhone", :ipod => "iPod Touch", :nintendo => "Nintendo", :opera => "Opera", :phantom_js => "PhantomJS", :psp => "PlayStation Portable", :playstation => "PlayStation", :quicktime => "QuickTime", :safari => "Safari", :xbox => "Xbox", # This must be last item, since Ruby 1.9+ has ordered keys. :other => "Other", }
- VERSIONS =
{ :default => %r[(?:Version|MSIE|Firefox|Chrome|CriOS|QuickTime|BlackBerry[^/]+|CoreMedia v|PhantomJS)[/ ]?([a-z0-9.]+)]i, :opera => %r[(?:Opera/.*? Version/([\d.]+)|Chrome/([\d.]+).*?OPR)], :ie => %r[(?:MSIE |Trident/.*?; rv:)([\d.]+)] }
Constants included from Bots
Constants included from Language
Constants included from IE
IE::MODERN_IE, IE::TRIDENT_VERSION_REGEX
Instance Attribute Summary collapse
-
#user_agent ⇒ Object
(also: #ua)
Set browser’s UA string.
Attributes included from Language
Instance Method Summary collapse
-
#chrome? ⇒ Boolean
Detect if browser is Chrome.
-
#core_media? ⇒ Boolean
Detect if browser is Apple CoreMedia.
-
#firefox? ⇒ Boolean
Detect if browser is Firefox.
-
#full_version ⇒ Object
Return the full version.
-
#id ⇒ Object
Get the browser identifier.
-
#initialize(options = {}) {|_self| ... } ⇒ Browser
constructor
Create a new browser instance and set the UA and Accept-Language headers.
-
#meta ⇒ Object
(also: #to_a)
Return a meta info about this browser.
-
#modern? ⇒ Boolean
Return true if browser is modern (Webkit, Firefox 17+, IE9+, Opera 12+).
-
#name ⇒ Object
Get readable browser name.
-
#opera? ⇒ Boolean
Detect if browser is Opera.
-
#phantom_js? ⇒ Boolean
Detect if browser is PhantomJS.
-
#quicktime? ⇒ Boolean
Detect if browser is QuickTime.
-
#safari? ⇒ Boolean
Detect if browser is Safari.
-
#silk? ⇒ Boolean
Detect if browser is Silk.
-
#to_s ⇒ Object
Return meta representation as string.
-
#version ⇒ Object
Return major version.
-
#webkit? ⇒ Boolean
Detect if browser is WebKit-based.
Methods included from Tv
Methods included from Bots
Methods included from Consoles
#console?, #nintendo?, #playstation?, #psp?, #xbox?
Methods included from Devices
#ipad?, #iphone?, #ipod?, #kindle?, #playbook?, #surface?, #tablet?, #windows_touchscreen_desktop?
Methods included from Mobile
#blackberry?, #mobile?, #opera_mini?
Methods included from Platform
#android?, #blackberry10?, #blackberry4?, #blackberry5?, #blackberry6?, #blackberry7?, #chrome_os?, #ios4?, #ios5?, #ios6?, #ios7?, #ios?, #linux?, #mac?, #platform, #windows8?, #windows?, #windows_mobile?, #windows_phone?, #windows_rt?, #windows_x64?
Methods included from IE
#compatibility_view?, #ie10?, #ie11?, #ie6?, #ie7?, #ie8?, #ie9?, #ie?
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Browser
Create a new browser instance and set the UA and Accept-Language headers.
browser = Browser.new({
:ua => "Safari",
:accept_language => "pt-br"
})
80 81 82 83 84 85 |
# File 'lib/browser.rb', line 80 def initialize( = {}, &block) self.user_agent = ([:user_agent] || [:ua]).to_s self.accept_language = [:accept_language].to_s yield self if block_given? end |
Instance Attribute Details
#user_agent ⇒ Object Also known as: ua
Set browser’s UA string.
39 40 41 |
# File 'lib/browser.rb', line 39 def user_agent @user_agent end |
Instance Method Details
#chrome? ⇒ Boolean
Detect if browser is Chrome.
149 150 151 |
# File 'lib/browser.rb', line 149 def chrome? !!(ua =~ /Chrome|CriOS/) && !opera? end |
#core_media? ⇒ Boolean
Detect if browser is Apple CoreMedia.
129 130 131 |
# File 'lib/browser.rb', line 129 def core_media? !!(ua =~ /CoreMedia/) end |
#firefox? ⇒ Boolean
Detect if browser is Firefox.
144 145 146 |
# File 'lib/browser.rb', line 144 def firefox? !!(ua =~ /Firefox/) end |
#full_version ⇒ Object
Return the full version.
104 105 106 107 |
# File 'lib/browser.rb', line 104 def full_version _, *v = *ua.match(VERSIONS.fetch(id, VERSIONS[:default])) v.compact.first || "0.0" end |
#id ⇒ Object
Get the browser identifier.
93 94 95 96 |
# File 'lib/browser.rb', line 93 def id NAMES.keys .find {|id| respond_to?("#{id}?") ? public_send("#{id}?") : id } end |
#meta ⇒ Object Also known as: to_a
Return a meta info about this browser.
164 165 166 167 168 169 |
# File 'lib/browser.rb', line 164 def Meta.constants.each_with_object(Set.new) do |, | = Meta.const_get() .merge(.new(self).to_a) end.to_a end |
#modern? ⇒ Boolean
Return true if browser is modern (Webkit, Firefox 17+, IE9+, Opera 12+).
110 111 112 113 114 115 116 |
# File 'lib/browser.rb', line 110 def modern? webkit? || newer_firefox? || newer_ie? || newer_opera? || newer_firefox_tablet? end |
#name ⇒ Object
Get readable browser name.
88 89 90 |
# File 'lib/browser.rb', line 88 def name NAMES[id] end |
#opera? ⇒ Boolean
Detect if browser is Opera.
154 155 156 |
# File 'lib/browser.rb', line 154 def opera? !!(ua =~ /(Opera|OPR)/) end |
#phantom_js? ⇒ Boolean
Detect if browser is PhantomJS
134 135 136 |
# File 'lib/browser.rb', line 134 def phantom_js? !!(ua =~ /PhantomJS/) end |
#quicktime? ⇒ Boolean
Detect if browser is QuickTime
124 125 126 |
# File 'lib/browser.rb', line 124 def quicktime? !!(ua =~ /QuickTime/i) end |
#safari? ⇒ Boolean
Detect if browser is Safari.
139 140 141 |
# File 'lib/browser.rb', line 139 def safari? ua =~ /Safari/ && ua !~ /Chrome|CriOS|PhantomJS/ end |
#silk? ⇒ Boolean
Detect if browser is Silk.
159 160 161 |
# File 'lib/browser.rb', line 159 def silk? !!(ua =~ /Silk/) end |
#to_s ⇒ Object
Return meta representation as string.
174 175 176 |
# File 'lib/browser.rb', line 174 def to_s .to_a.join(" ") end |
#version ⇒ Object
Return major version.
99 100 101 |
# File 'lib/browser.rb', line 99 def version full_version.to_s.split(".").first end |
#webkit? ⇒ Boolean
Detect if browser is WebKit-based.
119 120 121 |
# File 'lib/browser.rb', line 119 def webkit? !!(ua =~ /AppleWebKit/i) end |