Class: UAgent::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/uagent.rb

Constant Summary collapse

@@default_database =
{
  :mobile => ['Mobile', 'Opera Mini', 'iPhone', 'ACER', 'Alcatel', 'AUDIOVOX',
              'BlackBerry', 'CDM', 'Ericsson', 'LG', 'LGE', 'Motorola', 'MOT',
              'NEC', 'Nokia', 'Panasonic', 'QCI', 'SAGEM', 'SAMSUNG', 'SEC',
              'Sanyo', 'Sendo', 'SHARP', 'SonyEricsson', 'Telit',
              'Telit_mobile_Terminals', 'TSM', 'Palm'],
  :iphone => ['iPhone'],
  :blackberry => ['BlackBerry']
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*specific_keys) ⇒ Parser

Returns a new instance of Parser.



19
20
21
22
23
# File 'lib/uagent.rb', line 19

def initialize(*specific_keys)
  @specific_keys = specific_keys.clone
  @keys = [:desktop, :mobile] + @specific_keys
  set_database @@default_database
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



7
8
9
# File 'lib/uagent.rb', line 7

def keys
  @keys
end

#specific_keysObject (readonly)

Returns the value of attribute specific_keys.



7
8
9
# File 'lib/uagent.rb', line 7

def specific_keys
  @specific_keys
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/uagent.rb', line 32

def call(env)
  # Check devices in http user agent
  http_user_agent = env['HTTP_USER_AGENT']
  (@specific_keys + [:mobile]).each do |k|
    return k if @database[k] === http_user_agent
  end
  # Device is not found, return the default
  return :desktop
end

#set_database(database) ⇒ Object



25
26
27
28
29
30
# File 'lib/uagent.rb', line 25

def set_database(database)
  @database = database.clone
  @database.each do |k,v|
    @database[k] = Regexp.new( "(" + v.join("|") + ")" )
  end
end