uagent 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/uagent.rb +27 -40
  3. data/test/parser_test.rb +9 -0
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -4,52 +4,39 @@ module UAgent
4
4
 
5
5
  class Parser
6
6
 
7
- @@keys = [['Mobile', :mobile],
8
- ['Opera Mini', :mobile],
9
- ['iPhone', :iphone, :mobile],
10
- ['ACER', :mobile],
11
- ['Alcatel', :mobile],
12
- ['AUDIOVOX', :mobile],
13
- ['BlackBerry', :blackberry, :mobile],
14
- ['CDM', :mobile],
15
- ['Ericsson', :mobile],
16
- ['LG', :mobile],
17
- ['LGE', :mobile],
18
- ['Motorola', :mobile],
19
- ['MOT', :mobile],
20
- ['NEC', :mobile],
21
- ['Nokia', :mobile],
22
- ['Panasonic', :mobile],
23
- ['QCI', :mobile],
24
- ['SAGEM', :mobile],
25
- ['SAMSUNG', :mobile],
26
- ['SEC', :mobile],
27
- ['Sanyo', :mobile],
28
- ['Sendo', :mobile],
29
- ['SHARP', :mobile],
30
- ['SonyEricsson', :mobile],
31
- ['Telit', :mobile],
32
- ['Telit_mobile_Terminals', :mobile],
33
- ['TSM', :mobile],
34
- ['Palm', :mobile]]
35
- @@priority = [:iphone, :blackberry, :mobile]
36
-
37
- def initialize(*keys)
38
- @keys = [:desktop, :mobile] + keys
7
+ attr_reader :keys, :specific_keys
8
+
9
+ @@default_database = {
10
+ :mobile => ['Mobile', 'Opera Mini', 'iPhone', 'ACER', 'Alcatel', 'AUDIOVOX',
11
+ 'BlackBerry', 'CDM', 'Ericsson', 'LG', 'LGE', 'Motorola', 'MOT',
12
+ 'NEC', 'Nokia', 'Panasonic', 'QCI', 'SAGEM', 'SAMSUNG', 'SEC',
13
+ 'Sanyo', 'Sendo', 'SHARP', 'SonyEricsson', 'Telit',
14
+ 'Telit_mobile_Terminals', 'TSM', 'Palm'],
15
+ :iphone => ['iPhone'],
16
+ :blackberry => ['BlackBerry']
17
+ }
18
+
19
+ def initialize(*specific_keys)
20
+ @specific_keys = specific_keys.clone
21
+ @keys = [:desktop, :mobile] + @specific_keys
22
+ set_database @@default_database
23
+ end
24
+
25
+ def set_database(database)
26
+ @database = database.clone
27
+ @database.each do |k,v|
28
+ @database[k] = Regexp.new( "(" + v.join("|") + ")" )
29
+ end
39
30
  end
40
31
 
41
32
  def call(env)
42
33
  # Check devices in http user agent
43
34
  http_user_agent = env['HTTP_USER_AGENT']
44
- @@priority.select{ |k| @keys.include? k }.each do |key|
45
- @@keys.each do |sample|
46
- if /#{sample[0]}/ === http_user_agent
47
- return key if sample[(1...sample.size)].include? key
48
- end
49
- end
35
+ (@specific_keys + [:mobile]).each do |k|
36
+ return k if @database[k] === http_user_agent
50
37
  end
51
- # As device is not found, return the default
52
- return @keys[0]
38
+ # Device is not found, return the default
39
+ return :desktop
53
40
  end
54
41
 
55
42
  end
@@ -56,4 +56,13 @@ describe 'UAgent::Parser' do
56
56
  parser.call(env).should == :desktop
57
57
  end
58
58
 
59
+ it 'allows to change the device database' do
60
+ parser = UAgent::Parser.new(:test)
61
+ parser.set_database({:test => ['Test']})
62
+
63
+ ua = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) Test"
64
+ env = { 'HTTP_USER_AGENT' => ua }
65
+ parser.call(env).should == :test
66
+ end
67
+
59
68
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Daniel Hern\xC3\xA1ndez"