-
Notifications
You must be signed in to change notification settings - Fork 14
/
resolver.rb
58 lines (49 loc) · 2.17 KB
/
resolver.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
# frozen_string_literal: true
# rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity
# rubocop: disable Metrics/MethodLength, Metrics/PerceivedComplexity
module BrowserslistUseragent
# Resolves uaser agent string to {family, version} hash
class Resolver
attr_reader :user_agent_string
def initialize(user_agent_string)
@user_agent_string = user_agent_string
end
def call
agent = self.class.user_agent_parser.parse(user_agent_string)
family = agent.family
version = VersionNormalizer.new(agent.version.to_s).call
# Case A: For Safari, Chrome and others browsers on iOS
# that report as Safari after stripping tags
family = 'iOS' if agent.family.include?('Safari') && agent.os.family == 'iOS'
# Case B: The browser on iOS didn't report as safari,
# so we use the iOS version as a proxy to the browser
# version. This is based on the assumption that the
# underlying Safari Engine used will be *atleast* equal
# to the iOS version it's running on.
if agent.os.family == 'iOS'
return {
family: 'iOS',
version: VersionNormalizer.new(agent.os.version.to_s).call
}
end
# Case C: The caniuse database does not contain
# historical browser versions for so called `minor`
# browsers like Chrome for Android, Firefox for Android etc
# In this case, we proxy to the desktop version
# @see https://github.com/Fyrd/caniuse/issues/3518
family = 'Chrome' if agent.family.include?('Chrome Mobile')
family = 'Chrome' if agent.family == 'HeadlessChrome'
family = 'Firefox' if agent.family == 'Firefox Mobile'
family = 'Explorer' if agent.family == 'IE'
family = 'ExplorerMobile' if agent.family == 'IE Mobile'
family = 'QQAndroid' if agent.family == 'QQ Browser Mobile'
family = 'UCAndroid' if agent.family == 'UC Browser'
{ family: family, version: version }
end
def self.user_agent_parser
@user_agent_parser ||= UserAgentParser::Parser.new
end
end
end
# rubocop: enable Metrics/AbcSize, Metrics/CyclomaticComplexity
# rubocop: enable Metrics/MethodLength, Metrics/PerceivedComplexity