Class: Browser

Inherits:
Object
  • Object
show all
Includes:
BlackBerry, Bots, Consoles, Devices, IE, Language, Mobile, Platform, Tv
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/data/bots.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/data/languages.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/methods/blackberry.rb,
lib/browser/middleware/context.rb,
lib/browser/data/search_engines.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, BlackBerry, Bots, Consoles, Data, 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_running_safari: "Safari",
  blackberry: "BlackBerry",
  core_media: "Apple CoreMedia",
  ipad: "iPad",             # Must come before safari
  iphone: "iPhone",         # Must come before safari
  ipod: "iPod Touch",       # Must come before safari
  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

Language::LANGUAGES

Constants included from IE

IE::EDGE, IE::MODERN_IE, IE::MSIE, IE::TRIDENT_MAPPING, IE::TRIDENT_VERSION_REGEX

Instance Attribute Summary collapse

Attributes included from Language

#accept_language

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tv

#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?, #mobile?, #opera_mini?

Methods included from Platform

#android?, #android_version, #chrome_os?, #ios4?, #ios5?, #ios6?, #ios7?, #ios8?, #ios9?, #ios?, #ios_version, #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 BlackBerry

#blackberry10?, #blackberry4?, #blackberry5?, #blackberry6?, #blackberry7?, #blackberry?, #blackberry_version

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"
})

Yields:

  • (_self)

Yield Parameters:

  • _self (Browser)

    the object that the method was called on



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/browser.rb', line 116

def initialize(options = {})
  user_agent = options[:user_agent] || options[:ua]
  unless user_agent
    raise ArgumentError, 'Must receive a :user_agent or :ua option'
  end

  @user_agent = user_agent.to_s
  self.accept_language = options[:accept_language].to_s

  yield self if block_given?
end

Instance Attribute Details

#user_agentObject (readonly) Also known as: ua

Get the browser’s UA string; this is immutable after initialization.



49
50
51
# File 'lib/browser.rb', line 49

def user_agent
  @user_agent
end

Class Method Details

.modern_rulesObject

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' }


95
96
97
# File 'lib/browser.rb', line 95

def self.modern_rules
  @modern_rules ||= []
end

Instance Method Details

#chrome?Boolean

Detect if browser is Chrome.

Returns:

  • (Boolean)


200
201
202
# File 'lib/browser.rb', line 200

def chrome?
  (in_ua?('Chrome') || in_ua?('CriOS')) && !opera? && !edge?
end

#core_media?Boolean

Detect if browser is Apple CoreMedia.

Returns:

  • (Boolean)


176
177
178
# File 'lib/browser.rb', line 176

def core_media?
  !!(ua =~ /CoreMedia/)
end

#firefox?Boolean

Detect if browser is Firefox.

Returns:

  • (Boolean)


195
196
197
# File 'lib/browser.rb', line 195

def firefox?
  in_ua? 'Firefox'
end

#full_versionObject

Return the full version.



150
151
152
153
154
155
156
157
158
# File 'lib/browser.rb', line 150

def full_version
  @full_version ||=
    if ie?
      ie_full_version
    else
      _, *v = *ua.match(VERSIONS.fetch(id, VERSIONS[:default]))
      v.compact.first || "0.0"
    end
end

#idObject

Get the browser identifier.



134
135
136
137
138
# File 'lib/browser.rb', line 134

def id
  @id ||=
    NAMES.keys
      .find {|id| respond_to?("#{id}?", true) ? send("#{id}?") : id }
end

#known?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/browser.rb', line 219

def known?
  id != :other
end

#metaObject Also known as: to_a

Return a meta info about this browser.



224
225
226
227
228
229
230
# File 'lib/browser.rb', line 224

def meta
  @meta ||=
    Meta.constants.each_with_object(Set.new) do |meta_name, meta|
      meta_class = Meta.const_get(meta_name)
      meta.merge(meta_class.new(self).to_a)
    end.to_a
end

#modern?Boolean

Return true if browser is modern (Webkit, Firefox 17+, IE9+, Opera 12+).

Returns:

  • (Boolean)


161
162
163
# File 'lib/browser.rb', line 161

def modern?
  self.class.modern_rules.any? { |rule| rule.call self }
end

#nameObject

Get readable browser name.



129
130
131
# File 'lib/browser.rb', line 129

def name
  NAMES[id]
end

#opera?Boolean

Detect if browser is Opera.

Returns:

  • (Boolean)


205
206
207
# File 'lib/browser.rb', line 205

def opera?
  in_ua?('Opera') || in_ua?('OPR')
end

#phantom_js?Boolean

Detect if browser is PhantomJS

Returns:

  • (Boolean)


181
182
183
# File 'lib/browser.rb', line 181

def phantom_js?
  !!(ua =~ /PhantomJS/)
end

#quicktime?Boolean

Detect if browser is QuickTime

Returns:

  • (Boolean)


171
172
173
# File 'lib/browser.rb', line 171

def quicktime?
  !!(ua =~ /QuickTime/i)
end

#safari?Boolean

Detect if browser is Safari.

Returns:

  • (Boolean)


186
187
188
# File 'lib/browser.rb', line 186

def safari?
  (in_ua?('Safari') || safari_webapp_mode?) && ua !~ /Android|Chrome|CriOS|PhantomJS/
end

#safari_webapp_mode?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/browser.rb', line 190

def safari_webapp_mode?
  (ipad? || iphone?) && in_ua?('AppleWebKit')
end

#silk?Boolean

Detect if browser is Silk.

Returns:

  • (Boolean)


210
211
212
# File 'lib/browser.rb', line 210

def silk?
  in_ua? 'Silk'
end

#to_sObject

Return meta representation as string.



235
236
237
# File 'lib/browser.rb', line 235

def to_s
  meta.to_a.join(" ")
end

#versionObject

Return major version.



141
142
143
144
145
146
147
# File 'lib/browser.rb', line 141

def version
  if ie?
    ie_version
  else
    full_version.to_s.split(".").first
  end
end

#webkit?Boolean

Detect if browser is WebKit-based.

Returns:

  • (Boolean)


166
167
168
# File 'lib/browser.rb', line 166

def webkit?
  ua =~ /AppleWebKit/i && !edge?
end

#yandex?Boolean

Detect if browser is Yandex.

Returns:

  • (Boolean)


215
216
217
# File 'lib/browser.rb', line 215

def yandex?
  in_ua? 'YaBrowser'
end