File: metadata_extractor.rb

package info (click to toggle)
ruby-device-detector 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 3,104 kB
  • sloc: ruby: 1,224; makefile: 5
file content (29 lines) | stat: -rw-r--r-- 599 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
class DeviceDetector
  class MetadataExtractor < Struct.new(:user_agent, :regex_meta)

    def call
      regex_meta.any? ? extract_metadata : nil
    end

    private

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

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

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

  end
end