Class: Browser

Inherits:
Object
  • Object
show all
Includes:
Bots, Consoles, Devices, IE, Language, Mobile, Platform
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/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, 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

Bots::BOTS

Constants included from Language

Language::LANGUAGES

Constants included from IE

IE::MODERN_IE, IE::TRIDENT_VERSION_REGEX

Instance Attribute Summary collapse

Attributes included from Language

#accept_language

Instance Method Summary collapse

Methods included from Bots

#bot?

Methods included from Consoles

#console?, #nintendo?, #playstation?, #xbox?

Methods included from Devices

#ipad?, #iphone?, #ipod?, #kindle?, #playbook?, #psp?, #surface?, #tablet?

Methods included from Mobile

#blackberry?, #mobile?, #opera_mini?

Methods included from Platform

#android?, #ios4?, #ios5?, #ios6?, #ios7?, #ios?, #linux?, #mac?, #platform, #windows8?, #windows?, #windows_mobile?, #windows_phone?, #windows_rt?

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Browser)

    the object that the method was called on



77
78
79
80
81
82
# File 'lib/browser.rb', line 77

def initialize(options = {}, &block)
  self.user_agent = (options[:user_agent] || options[:ua]).to_s
  self.accept_language = options[:accept_language].to_s

  yield self if block_given?
end

Instance Attribute Details

#user_agentObject Also known as: ua

Set browser’s UA string.



36
37
38
# File 'lib/browser.rb', line 36

def user_agent
  @user_agent
end

Instance Method Details

#chrome?Boolean

Detect if browser is Chrome.

Returns:

  • (Boolean)


146
147
148
# File 'lib/browser.rb', line 146

def chrome?
  !!(ua =~ /Chrome|CriOS/) && !opera?
end

#core_media?Boolean

Detect if browser is Apple CoreMedia.

Returns:

  • (Boolean)


126
127
128
# File 'lib/browser.rb', line 126

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

#firefox?Boolean

Detect if browser is Firefox.

Returns:

  • (Boolean)


141
142
143
# File 'lib/browser.rb', line 141

def firefox?
  !!(ua =~ /Firefox/)
end

#full_versionObject

Return the full version.



101
102
103
104
# File 'lib/browser.rb', line 101

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

#idObject

Get the browser identifier.



90
91
92
93
# File 'lib/browser.rb', line 90

def id
  NAMES.keys
    .find {|id| respond_to?("#{id}?") ? public_send("#{id}?") : id }
end

#metaObject Also known as: to_a

Return a meta info about this browser.



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

def 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)


107
108
109
110
111
112
113
# File 'lib/browser.rb', line 107

def modern?
  webkit? ||
  newer_firefox? ||
  newer_ie? ||
  newer_opera? ||
  newer_firefox_tablet?
end

#nameObject

Get readable browser name.



85
86
87
# File 'lib/browser.rb', line 85

def name
  NAMES[id]
end

#opera?Boolean

Detect if browser is Opera.

Returns:

  • (Boolean)


151
152
153
# File 'lib/browser.rb', line 151

def opera?
  !!(ua =~ /(Opera|OPR)/)
end

#phantom_js?Boolean

Detect if browser is PhantomJS

Returns:

  • (Boolean)


131
132
133
# File 'lib/browser.rb', line 131

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

#quicktime?Boolean

Detect if browser is QuickTime

Returns:

  • (Boolean)


121
122
123
# File 'lib/browser.rb', line 121

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

#safari?Boolean

Detect if browser is Safari.

Returns:

  • (Boolean)


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

def safari?
  ua =~ /Safari/ && ua !~ /Chrome|CriOS|PhantomJS/
end

#silk?Boolean

Detect if browser is Silk.

Returns:

  • (Boolean)


156
157
158
# File 'lib/browser.rb', line 156

def silk?
  !!(ua =~ /Silk/)
end

#to_sObject

Return meta representation as string.



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

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

#versionObject

Return major version.



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

def version
  full_version.to_s.split(".").first
end

#webkit?Boolean

Detect if browser is WebKit-based.

Returns:

  • (Boolean)


116
117
118
# File 'lib/browser.rb', line 116

def webkit?
  !!(ua =~ /AppleWebKit/i)
end