class DeviceDetector::Device

Constants

DEVICE_NAMES

order is relevant for testing with fixtures

Public Instance Methods

brand() click to toggle source
# File lib/device_detector/device.rb, line 31
def brand
  regex_meta[:brand]
end
known?() click to toggle source
# File lib/device_detector/device.rb, line 19
def known?
  regex_meta.any?
end
name() click to toggle source
# File lib/device_detector/device.rb, line 23
def name
  ModelExtractor.new(user_agent, regex_meta).call
end
type() click to toggle source
# File lib/device_detector/device.rb, line 27
def type
  hbbtv? ? 'tv' : regex_meta[:device]
end

Private Instance Methods

filenames() click to toggle source

The order of files needs to be the same as the order of device parser classes used in the piwik project.

# File lib/device_detector/device.rb, line 39
def filenames
  [
    'device/televisions.yml',
    'device/consoles.yml',
    'device/car_browsers.yml',
    'device/cameras.yml',
    'device/portable_media_player.yml',
    'device/mobiles.yml',
  ]
end
hbbtv?() click to toggle source
# File lib/device_detector/device.rb, line 66
def hbbtv?
  @regex_hbbtv ||= build_regex('HbbTV/([1-9]{1}(?:\.[0-9]{1}){1,2})')
  user_agent =~ @regex_hbbtv
end
matching_regex() click to toggle source
# File lib/device_detector/device.rb, line 50
def matching_regex
  from_cache([self.class.name, user_agent]) do
    regex_list = hbbtv? ? regexes_for_hbbtv : regexes_other
    regex = regex_list.find { |r| user_agent =~ r[:regex] }
    if regex && regex[:models]
      model_regex = regex[:models].find { |m| user_agent =~ m[:regex]}
      if model_regex
        regex = regex.merge(:regex_model => model_regex[:regex], :model => model_regex[:model], :brand => model_regex[:brand])
        regex[:device] = model_regex[:device] if model_regex.key?(:device)
        regex.delete(:models)
      end
    end
    regex
  end
end
parse_regexes(path, raw_regexes) click to toggle source
# File lib/device_detector/device.rb, line 79
def parse_regexes(path, raw_regexes)
  raw_regexes.map do |brand, meta|
    fail "invalid device spec: #{meta.inspect}" unless meta[:regex].is_a? String
    meta[:regex] = build_regex(meta[:regex])
    if meta.key?(:models)
      meta[:models].each do |model|
        fail "invalid model spec: #{model.inspect}" unless model[:regex].is_a? String
        model[:regex] = build_regex(model[:regex])
        model[:brand] = brand.to_s unless model[:brand]
      end
    end
    meta[:path] = path
    meta
  end
end
regexes_for_hbbtv() click to toggle source
# File lib/device_detector/device.rb, line 71
def regexes_for_hbbtv
  regexes.select { |r| r[:path] == :'device/televisions.yml' }
end
regexes_other() click to toggle source
# File lib/device_detector/device.rb, line 75
def regexes_other
  regexes.select { |r| r[:path] != :'device/televisions.yml' }
end