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.
- data/VERSION +1 -1
- data/lib/uagent.rb +27 -40
- data/test/parser_test.rb +9 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/uagent.rb
CHANGED
@@ -4,52 +4,39 @@ module UAgent
|
|
4
4
|
|
5
5
|
class Parser
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
45
|
-
|
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
|
-
#
|
52
|
-
return
|
38
|
+
# Device is not found, return the default
|
39
|
+
return :desktop
|
53
40
|
end
|
54
41
|
|
55
42
|
end
|
data/test/parser_test.rb
CHANGED
@@ -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
|