wurfl-lite 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wurfl-lite.rb CHANGED
@@ -11,19 +11,21 @@ class WURFL
11
11
  end
12
12
  end
13
13
 
14
- def initialize( filename = 'http://downloads.sourceforge.net/project/wurfl/WURFL/latest/wurfl-latest.xml.gz' )
14
+ def clear!
15
+ @devices = Hash.new
16
+ @devices_by_id = {}
17
+ end
15
18
 
16
- file = open( filename )
17
- stream = begin
18
- Zlib::GzipReader.new( file )
19
- rescue Zlib::GzipFile::Error
20
- file
21
- end
22
- data = stream.read
19
+ def process_xml!( filename )
20
+ data = open( filename ).read
21
+ begin # Try decompress it, in case it's a compressed file
22
+ # XXX Yes, this does seem ugly, but is there another way?
23
+ data = Zlib::GzipReader.new(StringIO.new(data.to_s)).read
24
+ rescue Zlib::GzipFile::Error
25
+ end
23
26
  doc = Hpricot( data )
24
27
 
25
- @devices = Hash.new
26
- devices_by_id = {}
28
+ keys_added = []
27
29
 
28
30
  (doc/'devices'/'device').each do |device_element|
29
31
  device = Hash.new
@@ -42,16 +44,23 @@ class WURFL
42
44
  device[ name ] = value
43
45
  end
44
46
 
47
+ keys_added << device[ :user_agent ]
45
48
  @devices[ device[ :user_agent ] ] = device
46
- devices_by_id[ device[ :id ] ] = device
49
+ @devices_by_id[ device[ :id ] ] = device
47
50
  end
48
51
 
49
- @devices.keys.each do |key|
50
- @devices[ key ][ :fall_back ] = devices_by_id[ @devices[ key ][ :fall_back ] ]
52
+ keys_added.each do |key|
53
+ @devices[ key ][ :fall_back ] = @devices_by_id[ @devices[ key ][ :fall_back ] ]
51
54
  end
52
55
 
53
56
  end
54
57
 
58
+ def initialize( filename = 'http://downloads.sourceforge.net/project/wurfl/WURFL/latest/wurfl-latest.xml.gz' )
59
+ clear!
60
+ process_xml!( filename )
61
+ process_xml!( 'http://wurfl.sourceforge.net/web_browsers_patch.xml' )
62
+ end
63
+
55
64
  def []( user_agent )
56
65
  device = @devices[ user_agent ]
57
66
  return device if device
@@ -1,3 +1,3 @@
1
1
  class WURFL
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 1
9
- version: 1.0.1
8
+ - 2
9
+ version: 1.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Clive Crous
@@ -114,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- hash: 676964349
117
+ hash: 602051377
118
118
  segments:
119
119
  - 0
120
120
  version: "0"
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  requirements:
124
124
  - - ">="
125
125
  - !ruby/object:Gem::Version
126
- hash: 676964349
126
+ hash: 602051377
127
127
  segments:
128
128
  - 0
129
129
  version: "0"