File: version_extractor.rb

package info (click to toggle)
ruby-device-detector 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,820 kB
  • sloc: ruby: 3,650; makefile: 5
file content (39 lines) | stat: -rw-r--r-- 908 bytes parent folder | download
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
# frozen_string_literal: true

class DeviceDetector
  class VersionExtractor < MetadataExtractor
    MAJOR_VERSION_2 = Gem::Version.new('2.0')
    MAJOR_VERSION_3 = Gem::Version.new('3.0')
    MAJOR_VERSION_4 = Gem::Version.new('4.0')
    MAJOR_VERSION_8 = Gem::Version.new('8.0')

    def call
      simple_version = super&.chomp('.')

      return simple_version unless simple_version&.empty?

      os_version_by_regexes
    end

    private

    def os_version_by_regexes
      version_matches = regex_meta[:versions]
      return '' unless version_matches

      version_matches.detect do |matcher|
        user_agent.match(matcher[:regex]) do |match_data|
          return matcher[:version].gsub(/\$(\d)/) do
            match_data[Regexp.last_match(1).to_i].to_s
          end.strip
        end
      end

      ''
    end

    def metadata_string
      String(regex_meta[:version])
    end
  end
end