class BrowserslistUseragent::Resolver

Resolves uaser agent string to {family, version} hash

Attributes

user_agent_string[R]

Public Class Methods

new(user_agent_string) click to toggle source
# File lib/browserslist_useragent/resolver.rb, line 10
def initialize(user_agent_string)
  @user_agent_string = user_agent_string
end
user_agent_parser() click to toggle source
# File lib/browserslist_useragent/resolver.rb, line 52
def self.user_agent_parser
  @user_agent_parser ||= UserAgentParser::Parser.new
end

Public Instance Methods

call() click to toggle source
# 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