Sha256: ed76b8782bbbb58b96591bd6261c921f233fd4c8a22ed5587bd1f17fe95377eb

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

# 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 = UserAgentParser.parse(user_agent_string)

      family = agent.family
      version = VersionNormalizer.new(agent.version.to_s).call

      # Case A: For Safari, Chrome and others browsers
      # that report as Safari after stripping tags
      family = 'iOS' if agent.family.include?('Safari')

      # 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 = '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
  end
end
# rubocop: enable Metrics/AbcSize, Metrics/CyclomaticComplexity
# rubocop: enable Metrics/MethodLength, Metrics/PerceivedComplexity

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
browserslist_useragent-0.1.0 lib/browserslist_useragent/resolver.rb