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', 'ViewPad'],
  :iphone => ['iPhone'],
  :blackberry => ['BlackBerry'],
  :android => ['ViewPad7']
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*specific_keys) ⇒ Parser

Returns a new instance of Parser.



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

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



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

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



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

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