File: metadata_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 (28 lines) | stat: -rw-r--r-- 644 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
# frozen_string_literal: true

class DeviceDetector
  class MetadataExtractor < Struct.new(:user_agent, :regex_meta)
    def call
      regex_meta.any? ? extract_metadata : nil
    end

    private

    def metadata_string
      message = "#{name} (a child of MetadataExtractor) must implement the '#{__method__}' method."
      raise NotImplementedError, message
    end

    def extract_metadata
      user_agent.match(regex) do |match_data|
        metadata_string.gsub(/\$(\d)/) do
          match_data[Regexp.last_match(1).to_i].to_s
        end.strip
      end
    end

    def regex
      @regex ||= regex_meta[:regex]
    end
  end
end