Module: UaDict

Defined in:
lib/ua_dict.rb,
lib/ua_dict/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#brower_infoObject

name, alias


65
66
67
68
69
70
71
# 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) ⇒ Object



55
56
57
58
59
60
61
62
# 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_infoObject

name, alias


74
75
76
77
78
79
80
# 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) ⇒ Object



31
32
33
34
35
36
37
38
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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_parserObject



12
13
14
15
16
17
18
19
20
# 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?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/ua_dict.rb', line 91

def is_mobile?
  @mobile
end

#operating_infoObject

name, alias


83
84
85
86
87
88
89
# 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) ⇒ Object



22
23
24
25
26
27
28
29
# 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