Class: DeviceMap::Classifier
- Inherits:
-
Object
- Object
- DeviceMap::Classifier
- Includes:
- Singleton
- Defined in:
- lib/device_map/classifier.rb
Constant Summary collapse
- KEYWORD_NGRAM_SIZE =
4
Instance Attribute Summary collapse
-
#devices ⇒ Object
readonly
Returns the value of attribute devices.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Instance Method Summary collapse
-
#find_device(ua) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#initialize ⇒ Classifier
constructor
A new instance of Classifier.
Constructor Details
#initialize ⇒ Classifier
Returns a new instance of Classifier.
11 12 13 14 15 |
# File 'lib/device_map/classifier.rb', line 11 def initialize # TODO: Refactor @patterns = Marshal.load(File.open(PATTERNS_DUMP)) @devices = Marshal.load(File.open(DEVICES_DUMP)) end |
Instance Attribute Details
#devices ⇒ Object (readonly)
Returns the value of attribute devices.
9 10 11 |
# File 'lib/device_map/classifier.rb', line 9 def devices @devices end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
9 10 11 |
# File 'lib/device_map/classifier.rb', line 9 def patterns @patterns end |
Instance Method Details
#find_device(ua) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/device_map/classifier.rb', line 18 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 |