module Browser

Constants

EMPTY_STRING
VERSION

Public Class Methods

[](key) click to toggle source
# File lib/browser/testing.rb, line 23
def self.[](key)
  user_agents.fetch(key)
end
bot_user_agents() click to toggle source
# File lib/browser/testing.rb, line 13
def self.bot_user_agents
  @bot_user_agents ||= YAML.load_file(Browser.root.join("test/ua_bots.yml"))
end
browser_user_agents() click to toggle source
# File lib/browser/testing.rb, line 9
def self.browser_user_agents
  @browser_user_agents ||= YAML.load_file(Browser.root.join("test/ua.yml"))
end
matchers() click to toggle source

Hold the list of browser matchers. Order is important.

# File lib/browser/browser.rb, line 45
def self.matchers
  @matchers ||= [
    Nokia,
    UCBrowser,
    PhantomJS,
    BlackBerry,
    Opera,
    Edge,
    InternetExplorer,
    Firefox,
    Otter,
    Facebook,             # must be placed before Chrome and Safari
    Weibo,                # must be placed before Chrome and Safari
    QQ,                   # must be placed before Chrome and Safari
    Alipay,               # must be placed before Chrome and Safari
    Electron,             # must be placed before Chrome and Safari
    Chrome,
    Safari,
    MicroMessenger,
    Generic
  ]
end
modern_rules() click to toggle source

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" }
# File lib/browser/browser.rb, line 78
def self.modern_rules
  @modern_rules ||= []
end
new(user_agent, **kwargs) click to toggle source
# File lib/browser/browser.rb, line 91
def self.new(user_agent, **kwargs)
  matchers
    .map {|klass| klass.new(user_agent || EMPTY_STRING, **kwargs) }
    .find(&:match?)
end
root() click to toggle source
# File lib/browser/browser.rb, line 39
def self.root
  @root ||= Pathname.new(File.expand_path("../../..", __FILE__))
end
search_engine_user_agents() click to toggle source
# File lib/browser/testing.rb, line 17
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_agents() click to toggle source
# File lib/browser/testing.rb, line 3
def self.user_agents
  @user_agents ||= browser_user_agents
                   .merge(bot_user_agents)
                   .merge(search_engine_user_agents)
end