Class: LightweightUserAgentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lightweight_user_agent_parser.rb,
lib/lightweight_user_agent_parser/regexp.rb,
lib/lightweight_user_agent_parser/version.rb,
lib/lightweight_user_agent_parser/content_analyzer.rb

Defined Under Namespace

Modules: ContentAnalyzer, NameTable

Constant Summary collapse

DEVICES_META =
[

  [ :Android,        'Android|Adr'                     ],
  [ :iPod,           'iPod'                            ],
  [ :iPhone,         'iPhone|(iOS.*Smartphone)'        ],
  [ :iPad,           'iPad|(iOS.*Tablet)'              ],
  [ :BlackBerry,     'BlackBerry'                      ],
  [ :WindowsMobile,  'Windows *(Mobile|Phone)'         ],
  [ :Windows,        'Windows *(?:NT|9(?:5|8)|7|8|10)' ],
  [ :Symbian,        'Symbian|Opera Mini'              ],
  [ :FreeBSD,        'FreeBSD'                         ],
  [ :Linux,          'Linux'                           ],
  [ :Macintosh,      '(Mac *)?OS *X|Darwin'            ],
  [ :PlayStation,    'ps\d+'                           ],
  [ :Xbox,           'xbox(?: *(?:\w+|\d+))?'          ],
  [ :QuickTime,      'QuickTime'                       ],
  [ :Nintendo,       'Nintendo'                        ],
  [ :Bada,           'bada'                            ],
  [ :anonymized,     'anonymized'                      ]

]
MOBILE_DEVICES =
[
    :Android,
    :iPod,
    :iPhone,
    :iPad,
    :BlackBerry,
    :WindowsMobile,
    :Symbian,
    :Bada
]
DESKTOP_DEVICES =
DEVICES_META.map { |ary| ary[0] } - MOBILE_DEVICES
DEVICES =
DEVICES_META.map { |sym, regexp| [sym, {lazy: /#{regexp}/i, strict: /\b#{regexp}\b/i}] }.freeze
MOBILE_REGEXP =
/#{mobile_regexp_str}/i.freeze
DESKTOP_REGEXP =
/#{desktop_device_matcher}/i.freeze
ANONYMIZED_REGEXP =
Regexp.new(DEVICES_META.find { |sym, regex_str| sym == :anonymized }.last, Regexp::IGNORECASE)
VERSION =
'1.6.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent_str) ⇒ LightweightUserAgentParser

Returns a new instance of LightweightUserAgentParser.



16
17
18
# File 'lib/lightweight_user_agent_parser.rb', line 16

def initialize(user_agent_str)
  @user_agent_string = (user_agent_str || raise(ArgumentError)).to_s
end

Instance Attribute Details

#user_agent_stringObject (readonly) Also known as: to_s

Returns the value of attribute user_agent_string.



13
14
15
# File 'lib/lightweight_user_agent_parser.rb', line 13

def user_agent_string
  @user_agent_string
end

Instance Method Details

#anonymized?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/lightweight_user_agent_parser.rb', line 48

def anonymized?
  !!(user_agent_string =~ ANONYMIZED_REGEXP)
end

#is_mobile?Boolean Also known as: mobile?

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/lightweight_user_agent_parser.rb', line 40

def is_mobile?
  mobile = !!(user_agent_string =~ MOBILE_REGEXP)
  desktop = !!(user_agent_string =~ DESKTOP_REGEXP)

  ((mobile or not desktop) and not empty?) and platform != :other
end

#md5Object



52
53
54
# File 'lib/lightweight_user_agent_parser.rb', line 52

def md5
  Digest::MD5.hexdigest(user_agent_string)
end

#platformObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lightweight_user_agent_parser.rb', line 20

def platform
  @platform ||= lambda {

    DEVICES.each do |name, regexp|
      if user_agent_string =~ regexp[:strict]
        return name
      end
    end

    DEVICES.each do |name, regexp|
      if user_agent_string =~ regexp[:lazy]
        return name
      end
    end

    return :other

  }.call
end

#to_hashObject



56
57
58
59
60
61
62
63
64
# File 'lib/lightweight_user_agent_parser.rb', line 56

def to_hash
  (TO_HASH_METHODS).reduce({}) do |memory, method_name|
    hash_key = method_name.to_s.sub(/\?$/, '').to_sym

    memory.merge!(hash_key => self.public_send(method_name))

    memory
  end
end