wurfl-lite 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wurfl-lite.rb CHANGED
@@ -5,6 +5,23 @@ require 'amatch'
5
5
 
6
6
  class WURFL
7
7
 
8
+ LATEST = 'http://downloads.sourceforge.net/project/wurfl/WURFL/latest/wurfl-latest.xml.gz'
9
+ BROWSER_PATCH = 'http://wurfl.sourceforge.net/web_browsers_patch.xml'
10
+
11
+ attr_accessor :insertion, :substitution, :deletion
12
+ attr_accessor :match_threshold
13
+
14
+ def initialize( filenames = [ LATEST, BROWSER_PATCH ] )
15
+ clear!
16
+ [filenames].flatten.each do |filename|
17
+ process_xml!( filename )
18
+ end
19
+ @insertion = 2
20
+ @substitution = 1
21
+ @deletion = 1.5
22
+ @match_threshold = 0.15
23
+ end
24
+
8
25
  class Hash < ::Hash
9
26
  def method_missing( method )
10
27
  has_key?( method ) ? self[ method ] : ( self[ :fall_back ] ? self[ :fall_back ].send( method ) : nil )
@@ -14,9 +31,19 @@ class WURFL
14
31
  def clear!
15
32
  @devices = Hash.new
16
33
  @devices_by_id = {}
34
+ @files = []
35
+ end
36
+
37
+ def reset!
38
+ files = @files
39
+ clear!
40
+ files.each do |filename|
41
+ process_xml!( filename )
42
+ end
17
43
  end
18
44
 
19
45
  def process_xml!( filename )
46
+ @files << filename
20
47
  data = open( filename ).read
21
48
  begin # Try decompress it, in case it's a compressed file
22
49
  # XXX Yes, this does seem ugly, but is there another way?
@@ -55,20 +82,33 @@ class WURFL
55
82
 
56
83
  end
57
84
 
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
-
64
85
  def []( user_agent )
65
86
  device = @devices[ user_agent ]
66
- return device if device
87
+ if device
88
+ device[ :wurfl_match ] = { :distance => 0, :distance_normalized => 0 }
89
+ return device
90
+ end
67
91
  match = Amatch::Sellers.new( user_agent )
92
+ match.insertion = @insertion
93
+ match.substitution = @substitution
94
+ match.deletion = @deletion
68
95
  keys = @devices.keys
69
96
  distances = match.match( keys )
70
- use_key = keys.zip( distances ).sort{|a,b|a.last<=>b.last}.first.first
71
- @devices[ user_agent ] = @devices[ use_key ]
97
+ sorted_list = keys.zip( distances ).sort{|a,b|a.last<=>b.last}
98
+ use_key = sorted_list.first.first
99
+ device = @devices[ use_key ]
100
+ distance = sorted_list.first.last
101
+ distance_normalised = distance/(user_agent.length+1)
102
+
103
+ if distance_normalised > @match_threshold
104
+ nil
105
+ else
106
+ device[ :wurfl_match ] = {
107
+ :distance => distance,
108
+ :distance_normalised => distance_normalised
109
+ }
110
+ @devices[ user_agent ] = device
111
+ end
72
112
  end
73
113
 
74
114
  end
@@ -1,3 +1,3 @@
1
1
  class WURFL
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
+ - 1
7
8
  - 0
8
- - 2
9
- version: 1.0.2
9
+ version: 1.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Clive Crous
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-09 00:00:00 +02:00
17
+ date: 2011-02-10 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -114,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- hash: 602051377
117
+ hash: 660134675
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: 602051377
126
+ hash: 660134675
127
127
  segments:
128
128
  - 0
129
129
  version: "0"