-
-
Notifications
You must be signed in to change notification settings - Fork 358
/
browser.rb
99 lines (89 loc) · 2.7 KB
/
browser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true
require "set"
require "yaml"
require "pathname"
require "browser/version"
require "browser/detect_version"
require "browser/accept_language"
require "browser/base"
require "browser/safari"
require "browser/chrome"
require "browser/internet_explorer"
require "browser/firefox"
require "browser/edge"
require "browser/opera"
require "browser/blackberry"
require "browser/generic"
require "browser/phantom_js"
require "browser/uc_browser"
require "browser/nokia"
require "browser/micro_messenger"
require "browser/weibo"
require "browser/qq"
require "browser/alipay"
require "browser/electron"
require "browser/facebook"
require "browser/otter"
require "browser/instagram"
require "browser/bot"
require "browser/middleware"
require "browser/platform"
require "browser/device"
require "browser/meta"
module Browser
EMPTY_STRING = ""
def self.root
@root ||= Pathname.new(File.expand_path("../..", __dir__))
end
# Hold the list of browser matchers.
# Order is important.
def self.matchers
@matchers ||= [
Nokia,
UCBrowser,
PhantomJS,
BlackBerry,
Opera,
Edge,
InternetExplorer,
Firefox,
Otter,
Facebook, # must be placed before Chrome and Safari
Instagram, # 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
# 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" }
#
def self.modern_rules
@modern_rules ||= []
end
modern_rules.tap do |rules|
rules << ->(b) { b.webkit? }
rules << ->(b) { b.firefox? && b.version.to_i >= 17 }
rules << ->(b) { b.ie? && b.version.to_i >= 9 && !b.compatibility_view? }
rules << ->(b) { b.edge? && !b.compatibility_view? }
rules << ->(b) { b.opera? && b.version.to_i >= 12 }
rules << ->(b) { b.firefox? && b.device.tablet? && b.platform.android? && b.version.to_i >= 14 } # rubocop:disable Metrics/LineLength
end
def self.new(user_agent, **kwargs)
matchers
.map {|klass| klass.new(user_agent || EMPTY_STRING, **kwargs) }
.find(&:match?)
end
end