Class: BrowserslistUseragent::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/browserslist_useragent/resolver.rb

Overview

Resolves uaser agent string to version hash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent_string) ⇒ Resolver

Returns a new instance of Resolver.



10
11
12
# File 'lib/browserslist_useragent/resolver.rb', line 10

def initialize(user_agent_string)
  @user_agent_string = user_agent_string
end

Instance Attribute Details

#user_agent_stringObject (readonly)

Returns the value of attribute user_agent_string.



8
9
10
# File 'lib/browserslist_useragent/resolver.rb', line 8

def user_agent_string
  @user_agent_string
end

Class Method Details

.user_agent_parserObject



52
53
54
# File 'lib/browserslist_useragent/resolver.rb', line 52

def self.user_agent_parser
  @user_agent_parser ||= UserAgentParser::Parser.new
end

Instance Method Details

#callObject



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
# File 'lib/browserslist_useragent/resolver.rb', line 14

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