Module: Browser

Defined in:
lib/browser/qq.rb,
lib/browser/bot.rb,
lib/browser/base.rb,
lib/browser/edge.rb,
lib/browser/nokia.rb,
lib/browser/opera.rb,
lib/browser/rails.rb,
lib/browser/weibo.rb,
lib/browser/alipay.rb,
lib/browser/chrome.rb,
lib/browser/device.rb,
lib/browser/safari.rb,
lib/browser/aliases.rb,
lib/browser/browser.rb,
lib/browser/firefox.rb,
lib/browser/generic.rb,
lib/browser/meta/id.rb,
lib/browser/meta/ie.rb,
lib/browser/testing.rb,
lib/browser/version.rb,
lib/browser/meta/ios.rb,
lib/browser/platform.rb,
lib/browser/device/tv.rb,
lib/browser/meta/base.rb,
lib/browser/blackberry.rb,
lib/browser/device/psp.rb,
lib/browser/device/wii.rb,
lib/browser/meta/proxy.rb,
lib/browser/middleware.rb,
lib/browser/phantom_js.rb,
lib/browser/uc_browser.rb,
lib/browser/device/base.rb,
lib/browser/device/ipad.rb,
lib/browser/device/wiiu.rb,
lib/browser/meta/device.rb,
lib/browser/meta/mobile.rb,
lib/browser/meta/modern.rb,
lib/browser/meta/safari.rb,
lib/browser/meta/tablet.rb,
lib/browser/meta/webkit.rb,
lib/browser/platform/ios.rb,
lib/browser/platform/mac.rb,
lib/browser/device/iphone.rb,
lib/browser/device/kindle.rb,
lib/browser/device/psvita.rb,
lib/browser/meta/platform.rb,
lib/browser/platform/base.rb,
lib/browser/detect_version.rb,
lib/browser/device/surface.rb,
lib/browser/device/unknown.rb,
lib/browser/platform/linux.rb,
lib/browser/platform/other.rb,
lib/browser/accept_language.rb,
lib/browser/device/xbox_360.rb,
lib/browser/device/xbox_one.rb,
lib/browser/micro_messenger.rb,
lib/browser/platform/android.rb,
lib/browser/platform/windows.rb,
lib/browser/action_controller.rb,
lib/browser/device/ipod_touch.rb,
lib/browser/internet_explorer.rb,
lib/browser/device/kindle_fire.rb,
lib/browser/middleware/context.rb,
lib/browser/platform/adobe_air.rb,
lib/browser/platform/chrome_os.rb,
lib/browser/device/playstation3.rb,
lib/browser/device/playstation4.rb,
lib/browser/platform/blackberry.rb,
lib/browser/platform/firefox_os.rb,
lib/browser/meta/generic_browser.rb,
lib/browser/platform/windows_phone.rb,
lib/browser/platform/windows_mobile.rb,
lib/browser/device/blackberry_playbook.rb,
lib/browser/middleware/context/additions.rb,
lib/browser/middleware/context/url_methods.rb

Defined Under Namespace

Modules: ActionController, Aliases, DetectVersion, Meta Classes: AcceptLanguage, Alipay, Base, BlackBerry, Bot, Chrome, Device, Edge, Firefox, Generic, InternetExplorer, MicroMessenger, Middleware, Nokia, Opera, PhantomJS, Platform, QQ, Railtie, Safari, UCBrowser, Weibo

Constant Summary collapse

EMPTY_STRING =
"".freeze
VERSION =
"2.4.0".freeze

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



24
25
26
# File 'lib/browser/testing.rb', line 24

def self.[](key)
  user_agents.fetch(key)
end

.bot_user_agentsObject



14
15
16
# File 'lib/browser/testing.rb', line 14

def self.bot_user_agents
  @bot_user_agents ||= YAML.load_file(Browser.root.join("test/ua_bots.yml"))
end

.browser_user_agentsObject



10
11
12
# File 'lib/browser/testing.rb', line 10

def self.browser_user_agents
  @browser_user_agents ||= YAML.load_file(Browser.root.join("test/ua.yml"))
end

.matchersObject

Hold the list of browser matchers. Order is important.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/browser/browser.rb', line 43

def self.matchers
  @matchers ||= [
    Nokia,
    UCBrowser,
    PhantomJS,
    BlackBerry,
    Opera,
    Edge,
    InternetExplorer,
    Firefox,
    Weibo,                # must be placed before Chrome and Safari
    QQ,                   # must be placed before Chrome and Safari
    Alipay,               # must be placed before Chrome and Safari
    Chrome,
    Safari,
    MicroMessenger,
    Generic
  ]
end

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


73
74
75
# File 'lib/browser/browser.rb', line 73

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

.new(user_agent, **kwargs) ⇒ Object



86
87
88
89
90
# File 'lib/browser/browser.rb', line 86

def self.new(user_agent, **kwargs)
  matchers
    .map {|klass| klass.new(user_agent || EMPTY_STRING, **kwargs) }
    .find(&:match?)
end

.rootObject



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

def self.root
  @root ||= Pathname.new(File.expand_path("../../..", __FILE__))
end

.search_engine_user_agentsObject



18
19
20
21
22
# File 'lib/browser/testing.rb', line 18

def self.search_engine_user_agents
  @search_engine_user_agents ||= begin
    YAML.load_file(Browser.root.join("test/ua_search_engines.yml"))
  end
end

.user_agentsObject



4
5
6
7
8
# File 'lib/browser/testing.rb', line 4

def self.user_agents
  @user_agents ||= browser_user_agents
                   .merge(bot_user_agents)
                   .merge(search_engine_user_agents)
end