module UaDict

Constants

VERSION

Public Instance Methods

brower_info() click to toggle source
name, alias
# File lib/ua_dict.rb, line 65
def brower_info
  if @b_info
    [@b_info[0]['name'], @b_info[1]]
  else
    ['Unknown', 'Unknown']
  end
end
decode_ua(ua) click to toggle source
# File lib/ua_dict.rb, line 55
def decode_ua ua
  @b_info = fetch_info(@browser_data, ua)[1]
  if ua.upcase.include?('MOBILE')
    @mobile = true
    @d_info = fetch_info(@device_data, ua)[1]
  end
  @o_info = fetch_info(@operating_data, ua)[1]
end
device_info() click to toggle source
name, alias
# File lib/ua_dict.rb, line 74
def device_info
  if @d_info
    [@d_info[0]['name'], @d_info[1]]
  else
    ['Unknown', 'Unknown']
  end
end
fetch_aliases(op, ua) click to toggle source
# File lib/ua_dict.rb, line 31
def fetch_aliases op, ua
  op['aliases'].each do |_alias|
    if ua.upcase.include?(_alias.upcase)
      return op, _alias
    end
  end
  nil
end
fetch_info(type_data, ua) click to toggle source
# File lib/ua_dict.rb, line 40
def fetch_info type_data, ua
  found, info = false, nil
  type_data.each do |ua_dict|
    has_res = fetch_aliases(ua_dict, ua)
    if has_res
      found, info = true, has_res
      if ua_dict['children'] != []
        found, info = fetch_info(ua_dict['children'], ua)
      end
      break if found
    end
  end
  return found, info
end
initialize_parser() click to toggle source

@browser_data 浏览器版本和制造商的关系数据 @operating_data 操作系统和制造商的关系数据 @device_data 设备和制造商的关系数据

# File lib/ua_dict.rb, line 12
def initialize_parser
  @ua_dict_path   = File.dirname(__FILE__) + '/ua_dict/dict/'

  @browser_data   = ua_data('Browser.json')
  @operating_data = ua_data('OperatingSystem.json')
  @device_data    = ua_data('Device.json')

  @mobile         = false
end
is_mobile?() click to toggle source
# File lib/ua_dict.rb, line 91
def is_mobile?
  @mobile
end
operating_info() click to toggle source
name, alias
# File lib/ua_dict.rb, line 83
def operating_info
  if @o_info
    [@o_info[0]['name'], @o_info[1]]
  else
    ['Unknown', 'Unknown']
  end
end
ua_data(json_file) click to toggle source
# File lib/ua_dict.rb, line 22
def ua_data json_file
  result = []
  File.open(@ua_dict_path + json_file) do |f|
    result = JSON.load(f)
  end
  raise "Parse error: #{json_file}" if result == []
  result
end