Class: Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/browser.rb,
lib/browser/version.rb,
lib/browser/action_controller.rb

Defined Under Namespace

Modules: ActionController, Version

Constant Summary collapse

NAMES =
{
  :android    => "Android",
  :blackberry => "BlackBerry",
  :chrome     => "Chrome",
  :firefox    => "Firefox",
  :ie         => "Internet Explorer",
  :ipad       => "iPad",
  :iphone     => "iPhone",
  :ipod       => "iPod Touch",
  :opera      => "Opera",
  :other      => "Other",
  :safari     => "Safari",
  :psp        => "PlayStation Portable",
  :quicktime  => "QuickTime",
  :core_media => "Apple CoreMedia"
}
VERSION_REGEX =
/(?:Version|MSIE|Opera|Firefox|Chrome|QuickTime|BlackBerry[^\/]+|CoreMedia v)[\/ ]?([a-z0-9.]+)/i
TRIDENT_VERSION_REGEX =
/Trident\/([0-9.]+)/
LANGUAGES =
{
  "af"    => "Afrikaans",
  "sq"    => "Albanian",
  "eu"    => "Basque",
  "bg"    => "Bulgarian",
  "be"    => "Byelorussian",
  "ca"    => "Catalan",
  "zh"    => "Chinese",
  "zh-cn" => "Chinese/China",
  "zh-tw" => "Chinese/Taiwan",
  "zh-hk" => "Chinese/Hong Kong",
  "zh-sg" => "Chinese/singapore",
  "hr"    => "Croatian",
  "cs"    => "Czech",
  "da"    => "Danish",
  "nl"    => "Dutch",
  "nl-nl" => "Dutch/Netherlands",
  "nl-be" => "Dutch/Belgium",
  "en"    => "English",
  "en-gb" => "English/United Kingdom",
  "en-us" => "English/United States",
  "en-au" => "English/Australian",
  "en-ca" => "English/Canada",
  "en-nz" => "English/New Zealand",
  "en-ie" => "English/Ireland",
  "en-za" => "English/South Africa",
  "en-jm" => "English/Jamaica",
  "en-bz" => "English/Belize",
  "en-tt" => "English/Trinidad",
  "et"    => "Estonian",
  "fo"    => "Faeroese",
  "fa"    => "Farsi",
  "fi"    => "Finnish",
  "fr"    => "French",
  "fr-be" => "French/Belgium",
  "fr-fr" => "French/France",
  "fr-ch" => "French/Switzerland",
  "fr-ca" => "French/Canada",
  "fr-lu" => "French/Luxembourg",
  "gd"    => "Gaelic",
  "gl"    => "Galician",
  "de"    => "German",
  "de-at" => "German/Austria",
  "de-de" => "German/Germany",
  "de-ch" => "German/Switzerland",
  "de-lu" => "German/Luxembourg",
  "de-li" => "German/Liechtenstein",
  "el"    => "Greek",
  "he"    => "Hebrew",
  "he-il" => "Hebrew/Israel",
  "hi"    => "Hindi",
  "hu"    => "Hungarian",
  "ie-ee" => "Internet Explorer/Easter Egg",
  "is"    => "Icelandic",
  "id"    => "Indonesian",
  "in"    => "Indonesian",
  "ga"    => "Irish",
  "it"    => "Italian",
  "it-ch" => "Italian/ Switzerland",
  "ja"    => "Japanese",
  "km"    => "Khmer",
  "km-kh" => "Khmer/Cambodia",
  "ko"    => "Korean",
  "lv"    => "Latvian",
  "lt"    => "Lithuanian",
  "mk"    => "Macedonian",
  "ms"    => "Malaysian",
  "mt"    => "Maltese",
  "no"    => "Norwegian",
  "pl"    => "Polish",
  "pt"    => "Portuguese",
  "pt-br" => "Portuguese/Brazil",
  "rm"    => "Rhaeto-Romanic",
  "ro"    => "Romanian",
  "ro-mo" => "Romanian/Moldavia",
  "ru"    => "Russian",
  "ru-mo" => "Russian /Moldavia",
  "gd"    => "Scots Gaelic",
  "sr"    => "Serbian",
  "sk"    => "Slovack",
  "sl"    => "Slovenian",
  "sb"    => "Sorbian",
  "es"    => "Spanish",
  "es-do" => "Spanish",
  "es-ar" => "Spanish/Argentina",
  "es-co" => "Spanish/Colombia",
  "es-mx" => "Spanish/Mexico",
  "es-es" => "Spanish/Spain",
  "es-gt" => "Spanish/Guatemala",
  "es-cr" => "Spanish/Costa Rica",
  "es-pa" => "Spanish/Panama",
  "es-ve" => "Spanish/Venezuela",
  "es-pe" => "Spanish/Peru",
  "es-ec" => "Spanish/Ecuador",
  "es-cl" => "Spanish/Chile",
  "es-uy" => "Spanish/Uruguay",
  "es-py" => "Spanish/Paraguay",
  "es-bo" => "Spanish/Bolivia",
  "es-sv" => "Spanish/El salvador",
  "es-hn" => "Spanish/Honduras",
  "es-ni" => "Spanish/Nicaragua",
  "es-pr" => "Spanish/Puerto Rico",
  "sx"    => "Sutu",
  "sv"    => "Swedish",
  "sv-se" => "Swedish/Sweden",
  "sv-fi" => "Swedish/Finland",
  "ts"    => "Thai",
  "tn"    => "Tswana",
  "tr"    => "Turkish",
  "uk"    => "Ukrainian",
  "ur"    => "Urdu",
  "vi"    => "Vietnamese",
  "xh"    => "Xshosa",
  "ji"    => "Yiddish",
  "zu"    => "Zulu"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

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



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

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

  yield self if block_given?
end

Instance Attribute Details

#accept_languageObject

Return an array with all preferred languages that this browser accepts.



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

def accept_language
  @accept_language.gsub(/;q=[\d.]+/, "").split(",").collect {|l| l.downcase.gsub(/\s/m, "")}
end

#user_agentObject Also known as: ua

Set browser’s UA string.



12
13
14
# File 'lib/browser.rb', line 12

def user_agent
  @user_agent
end

Instance Method Details

#android?Boolean

Detect if browser is Android.

Returns:

  • (Boolean)


258
259
260
# File 'lib/browser.rb', line 258

def android?
  !!(ua =~ /Android/)
end

#blackberry?Boolean

Detect if browser is BlackBerry

Returns:

  • (Boolean)


253
254
255
# File 'lib/browser.rb', line 253

def blackberry?
  !!(ua =~ /BlackBerry/)
end

#capable?Boolean

Return true if browser supports some CSS 3 (Safari, Firefox, Opera & IE8+).

Returns:

  • (Boolean)


221
222
223
224
225
226
# File 'lib/browser.rb', line 221

def capable?
  (chrome? && version_obj  >= '16'.to_version)    || 
  (firefox? && version_obj >= '9'.to_version)     || 
  (safari? && version_obj  >= '5.1.2'.to_version) || 
  (ie? && version_obj      >= '8'.to_version)
end

#chrome?Boolean

Detect if browser is Chrome.

Returns:

  • (Boolean)


293
294
295
# File 'lib/browser.rb', line 293

def chrome?
  !!(ua =~ /Chrome/)
end

#compatibility_view?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/browser.rb', line 228

def compatibility_view?
  ie? && ua.match(TRIDENT_VERSION_REGEX) && version.to_i < ($1.to_i + 4)
end

#core_media?Boolean

Detect if browser is Apple CoreMedia.

Returns:

  • (Boolean)


263
264
265
# File 'lib/browser.rb', line 263

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

#firefox?Boolean

Detect if browser is Firefox.

Returns:

  • (Boolean)


288
289
290
# File 'lib/browser.rb', line 288

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

#full_versionObject

Return the full version.



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

def full_version
  _, v = *ua.match(VERSION_REGEX)
  v || "0.0"
end

#idObject

Return a symbol that identifies the browser.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/browser.rb', line 179

def id
  case
  when chrome?      then :chrome
  when iphone?      then :iphone
  when ipad?        then :ipad
  when ipod?        then :ipod
  when ie?          then :ie
  when opera?       then :opera
  when firefox?     then :firefox
  when android?     then :android
  when blackberry?  then :blackberry
  when safari?      then :safari
  when psp?         then :psp
  when quicktime?   then :quicktime
  when core_media?  then :core_media
  else
    :other
  end
end

#ie6?Boolean

Detect if browser is Internet Explorer 6.

Returns:

  • (Boolean)


303
304
305
# File 'lib/browser.rb', line 303

def ie6?
  ie? && version == "6"
end

#ie7?Boolean

Detect if browser is Internet Explorer 7.

Returns:

  • (Boolean)


308
309
310
# File 'lib/browser.rb', line 308

def ie7?
  ie? && version == "7"
end

#ie8?Boolean

Detect if browser is Internet Explorer 8.

Returns:

  • (Boolean)


313
314
315
# File 'lib/browser.rb', line 313

def ie8?
  ie? && version == "8"
end

#ie9?Boolean

Detect if browser is Internet Explorer 9.

Returns:

  • (Boolean)


318
319
320
# File 'lib/browser.rb', line 318

def ie9?
  ie? && version == "9"
end

#ie?Boolean

Detect if browser is Internet Explorer.

Returns:

  • (Boolean)


298
299
300
# File 'lib/browser.rb', line 298

def ie?
  !!(ua =~ /MSIE/ && ua !~ /Opera/)
end

#ios?Boolean

Detect if browser is ios?.

Returns:

  • (Boolean)


238
239
240
# File 'lib/browser.rb', line 238

def ios?
  ipod? || ipad? || iphone?
end

#ipad?Boolean

Detect if browser is iPad.

Returns:

  • (Boolean)


273
274
275
# File 'lib/browser.rb', line 273

def ipad?
  !!(ua =~ /iPad/)
end

#iphone?Boolean

Detect if browser is iPhone.

Returns:

  • (Boolean)


268
269
270
# File 'lib/browser.rb', line 268

def iphone?
  !!(ua =~ /iPhone/)
end

#ipod?Boolean

Detect if browser is iPod.

Returns:

  • (Boolean)


278
279
280
# File 'lib/browser.rb', line 278

def ipod?
  !!(ua =~ /iPod/)
end

#linux?Boolean

Detect if current platform is Linux flavor.

Returns:

  • (Boolean)


343
344
345
# File 'lib/browser.rb', line 343

def linux?
  !!(ua =~ /Linux/)
end

#mac?Boolean

Detect if current platform is Macintosh.

Returns:

  • (Boolean)


333
334
335
# File 'lib/browser.rb', line 333

def mac?
  !!(ua =~ /Mac OS X/)
end

#metaObject

Return a meta info about this browser.



364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/browser.rb', line 364

def meta
  Array.new.tap do |m|
    m << id
    m << "webkit" if webkit?
    m << "ios" if ios?
    m << "safari safari#{version}" if safari?
    m << "#{id}#{version}" unless safari? || chrome?
    m << platform
    m << "capable" if capable?
    m << "mobile" if mobile?
  end
end

#mobile?Boolean

Detect if browser is mobile.

Returns:

  • (Boolean)


243
244
245
# File 'lib/browser.rb', line 243

def mobile?
  !!(ua =~ /(Mobile|Symbian|MIDP|Windows CE)/) || blackberry? || psp?
end

#nameObject

Get readable browser name.



174
175
176
# File 'lib/browser.rb', line 174

def name
  NAMES[id]
end

#opera?Boolean

Detect if browser is Opera.

Returns:

  • (Boolean)


328
329
330
# File 'lib/browser.rb', line 328

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

#platformObject

Return the platform.



353
354
355
356
357
358
359
360
361
# File 'lib/browser.rb', line 353

def platform
  case
  when linux?   then :linux
  when mac?     then :mac
  when windows? then :windows
  else
    :other
  end
end

#psp?Boolean

Detect if browser is running from PSP.

Returns:

  • (Boolean)


323
324
325
# File 'lib/browser.rb', line 323

def psp?
  !!(ua =~ /PSP/)
end

#quicktime?Boolean

Detect if browser is QuickTime

Returns:

  • (Boolean)


248
249
250
# File 'lib/browser.rb', line 248

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

#safari?Boolean

Detect if browser is Safari.

Returns:

  • (Boolean)


283
284
285
# File 'lib/browser.rb', line 283

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

#tablet?Boolean

Detect if browser is tablet (currently just iPad or Android).

Returns:

  • (Boolean)


348
349
350
# File 'lib/browser.rb', line 348

def tablet?
  ipad? || (android? && !mobile?)
end

#to_sObject

Return meta representation as string.



378
379
380
# File 'lib/browser.rb', line 378

def to_s
  meta.join(" ")
end

#versionObject

Return major version.



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

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

#version_objObject

Return major version object.



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

def version_obj
  Gem::Version.new(full_version)
end

#webkit?Boolean

Detect if browser is WebKit-based.

Returns:

  • (Boolean)


233
234
235
# File 'lib/browser.rb', line 233

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

#windows?Boolean

Detect if current platform is Windows.

Returns:

  • (Boolean)


338
339
340
# File 'lib/browser.rb', line 338

def windows?
  !!(ua =~ /Windows/)
end