Sha256: 17da5688b06de45d4f282a8483170ed5b1fce2711aa0c17f4d0f7067e1b37953

Contents?: true

Size: 960 Bytes

Versions: 3

Compression:

Stored size: 960 Bytes

Contents

require 'singleton'

module DeviceMap
  class Classifier
    include Singleton

    KEYWORD_NGRAM_SIZE = 4

    attr_reader :patterns, :devices

    def initialize
      # TODO: Refactor
      @patterns = Marshal.load(File.open(PATTERNS_DUMP))
      @devices = Marshal.load(File.open(DEVICES_DUMP))
    end

    # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    def find_device(ua)
      user_agent = UserAgent.new(ua)
      keyword_ngrams = user_agent.keyword_ngrams(KEYWORD_NGRAM_SIZE)

      search_hits = keyword_ngrams.each_with_object(Set.new) do |ngram, hits|
        hits.merge patterns.find(ngram.join)
      end

      matched_pattern = search_hits.sort.reverse.find do |pattern|
        # FIXME: Match only against keyword hits
        pattern.matches?(keyword_ngrams.map(&:join))
      end

      if matched_pattern
        devices.find(matched_pattern.device_id)
      else
        DeviceData::Device.unknown
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
device_map-0.1.2 lib/device_map/classifier.rb
device_map-0.1.1 lib/device_map/classifier.rb
device_map-0.1.0 lib/device_map/classifier.rb