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 =
{ edge: "Microsoft Edge", # Must come before everything ie: "Internet Explorer", # Must come before android chrome: "Chrome", # Must come before android firefox: "Firefox", # Must come before android android: "Android", blackberry: "BlackBerry", core_media: "Apple CoreMedia", 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 =
{ edge: %r[Edge/([\d.]+)], chrome: %r[(?:Chrome|CriOS)/([\d.]+)], default: %r[(?:Version|MSIE|Firefox|QuickTime|BlackBerry[^/]+|CoreMedia v|PhantomJS|AdobeAIR)[/ ]?([a-z0-9.]+)]i, opera: %r[(?:Opera/.*? Version/([\d.]+)|Chrome/.*?OPR/([\d.]+))], ie: %r[(?:MSIE |Trident/.*?; rv:)([\d.]+)] }
Constants included from Bots
Bots::BOTS, Bots::SEARCH_ENGINES
Constants included from Language
Constants included from IE
IE::EDGE, IE::MODERN_IE, IE::MSIE, IE::TRIDENT_MAPPING, IE::TRIDENT_VERSION_REGEX
Instance Attribute Summary collapse
-
#user_agent ⇒ Object
(also: #ua)
Set browser’s UA string.
Attributes included from Language
Class Method Summary collapse
-
.modern_rules ⇒ Object
Define the rules which define a modern browser.
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.
- #known? ⇒ Boolean
-
#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.
- #safari_webapp_mode? ⇒ Boolean
-
#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
#bot?, #bot_name, detect_empty_ua!, detect_empty_ua?, #search_engine?
Methods included from Consoles
#console?, #nintendo?, #playstation4?, #playstation?, #psp?, #psp_vita?, #xbox?, #xbox_one?
Methods included from Devices
#ipad?, #iphone?, #ipod?, #kindle?, #playbook?, #surface?, #tablet?, #windows_touchscreen_desktop?
Methods included from Mobile
#adobe_air?, #blackberry?, #mobile?, #opera_mini?
Methods included from Platform
#android?, #blackberry10?, #blackberry4?, #blackberry5?, #blackberry6?, #blackberry7?, #chrome_os?, #ios4?, #ios5?, #ios6?, #ios7?, #ios8?, #ios?, #linux?, #mac?, #platform, #windows10?, #windows7?, #windows8?, #windows8_1?, #windows?, #windows_mobile?, #windows_phone?, #windows_rt?, #windows_vista?, #windows_wow64?, #windows_x64?, #windows_x64_inclusive?, #windows_xp?
Methods included from IE
#compatibility_view?, #edge?, #ie10?, #ie11?, #ie6?, #ie7?, #ie8?, #ie9?, #ie?, #ie_full_version, #ie_version, #msie_full_version, #msie_version, #trident_version
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"
})
107 108 109 110 111 112 |
# File 'lib/browser.rb', line 107 def initialize( = {}, &block) self.user_agent = ([:user_agent] || [:ua]).to_s self.accept_language = [:accept_language].to_s yield self if block end |
Instance Attribute Details
#user_agent ⇒ Object Also known as: ua
Set browser’s UA string.
40 41 42 |
# File 'lib/browser.rb', line 40 def user_agent @user_agent end |
Class Method Details
.modern_rules ⇒ Object
Define the rules which define a modern browser. A rule must be a proc/lambda or any object that implements the method
and accepts the browser object.
To redefine all rules, clear the existing rules before adding your own.
# Only Chrome Canary is considered modern.
Browser.modern_rules.clear
Browser.modern_rules << -> b { b.chrome? && b.version >= '37' }
86 87 88 |
# File 'lib/browser.rb', line 86 def self.modern_rules @modern_rules ||= [] end |
Instance Method Details
#chrome? ⇒ Boolean
Detect if browser is Chrome.
184 185 186 |
# File 'lib/browser.rb', line 184 def chrome? ua =~ /Chrome|CriOS/ && !opera? && !edge? end |
#core_media? ⇒ Boolean
Detect if browser is Apple CoreMedia.
160 161 162 |
# File 'lib/browser.rb', line 160 def core_media? !!(ua =~ /CoreMedia/) end |
#firefox? ⇒ Boolean
Detect if browser is Firefox.
179 180 181 |
# File 'lib/browser.rb', line 179 def firefox? !!(ua =~ /Firefox/) end |
#full_version ⇒ Object
Return the full version.
135 136 137 138 139 140 141 142 |
# File 'lib/browser.rb', line 135 def full_version if ie? ie_full_version else _, *v = *ua.match(VERSIONS.fetch(id, VERSIONS[:default])) v.compact.first || "0.0" end end |
#id ⇒ Object
Get the browser identifier.
120 121 122 123 |
# File 'lib/browser.rb', line 120 def id NAMES.keys .find {|id| respond_to?("#{id}?") ? public_send("#{id}?") : id } end |
#known? ⇒ Boolean
198 199 200 |
# File 'lib/browser.rb', line 198 def known? id != :other end |
#meta ⇒ Object Also known as: to_a
Return a meta info about this browser.
203 204 205 206 207 208 |
# File 'lib/browser.rb', line 203 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+).
145 146 147 |
# File 'lib/browser.rb', line 145 def modern? self.class.modern_rules.any? {|rule| rule === self } end |
#name ⇒ Object
Get readable browser name.
115 116 117 |
# File 'lib/browser.rb', line 115 def name NAMES[id] end |
#opera? ⇒ Boolean
Detect if browser is Opera.
189 190 191 |
# File 'lib/browser.rb', line 189 def opera? !!(ua =~ /(Opera|OPR)/) end |
#phantom_js? ⇒ Boolean
Detect if browser is PhantomJS
165 166 167 |
# File 'lib/browser.rb', line 165 def phantom_js? !!(ua =~ /PhantomJS/) end |
#quicktime? ⇒ Boolean
Detect if browser is QuickTime
155 156 157 |
# File 'lib/browser.rb', line 155 def quicktime? !!(ua =~ /QuickTime/i) end |
#safari? ⇒ Boolean
Detect if browser is Safari.
170 171 172 |
# File 'lib/browser.rb', line 170 def safari? (ua =~ /Safari/ || safari_webapp_mode?) && ua !~ /Android|Chrome|CriOS|PhantomJS/ end |
#safari_webapp_mode? ⇒ Boolean
174 175 176 |
# File 'lib/browser.rb', line 174 def safari_webapp_mode? (ipad? || iphone?) && ua =~ /AppleWebKit/ end |
#silk? ⇒ Boolean
Detect if browser is Silk.
194 195 196 |
# File 'lib/browser.rb', line 194 def silk? !!(ua =~ /Silk/) end |
#to_s ⇒ Object
Return meta representation as string.
213 214 215 |
# File 'lib/browser.rb', line 213 def to_s .to_a.join(" ") end |
#version ⇒ Object
Return major version.
126 127 128 129 130 131 132 |
# File 'lib/browser.rb', line 126 def version if ie? ie_version else full_version.to_s.split(".").first end end |
#webkit? ⇒ Boolean
Detect if browser is WebKit-based.
150 151 152 |
# File 'lib/browser.rb', line 150 def webkit? ua =~ /AppleWebKit/i && !edge? end |