Class: WURFL
- Inherits:
-
Object
- Object
- WURFL
- Defined in:
- lib/wurfl-lite.rb,
lib/wurfl-lite/version.rb
Defined Under Namespace
Classes: Hash
Constant Summary collapse
- LATEST =
'http://downloads.sourceforge.net/project/wurfl/WURFL/2.3/wurfl-2.3.xml.gz'
- BROWSER_PATCH =
'http://downloads.sourceforge.net/project/wurfl/WURFL/2.3/web_browsers_patch.xml'
- VERSION =
"1.1.2"
Instance Attribute Summary collapse
-
#deletion ⇒ Object
Returns the value of attribute deletion.
-
#insertion ⇒ Object
Returns the value of attribute insertion.
-
#match_threshold ⇒ Object
Returns the value of attribute match_threshold.
-
#substitution ⇒ Object
Returns the value of attribute substitution.
Instance Method Summary collapse
- #[](user_agent) ⇒ Object
- #clear! ⇒ Object
-
#initialize(filenames = [ LATEST, BROWSER_PATCH ]) ⇒ WURFL
constructor
A new instance of WURFL.
- #process_xml!(filename) ⇒ Object
- #reset! ⇒ Object
Constructor Details
#initialize(filenames = [ LATEST, BROWSER_PATCH ]) ⇒ WURFL
Returns a new instance of WURFL.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/wurfl-lite.rb', line 14 def initialize( filenames = [ LATEST, BROWSER_PATCH ] ) clear! [filenames].flatten.each do |filename| process_xml!( filename ) end @insertion = 2 @substitution = 1 @deletion = 1.5 @match_threshold = 0.15 end |
Instance Attribute Details
#deletion ⇒ Object
Returns the value of attribute deletion.
11 12 13 |
# File 'lib/wurfl-lite.rb', line 11 def deletion @deletion end |
#insertion ⇒ Object
Returns the value of attribute insertion.
11 12 13 |
# File 'lib/wurfl-lite.rb', line 11 def insertion @insertion end |
#match_threshold ⇒ Object
Returns the value of attribute match_threshold.
12 13 14 |
# File 'lib/wurfl-lite.rb', line 12 def match_threshold @match_threshold end |
#substitution ⇒ Object
Returns the value of attribute substitution.
11 12 13 |
# File 'lib/wurfl-lite.rb', line 11 def substitution @substitution end |
Instance Method Details
#[](user_agent) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/wurfl-lite.rb', line 85 def []( user_agent ) device = @devices[ user_agent ] if device device[ :wurfl_match ] = { :distance => 0, :distance_normalized => 0 } return device end match = Amatch::Sellers.new( user_agent ) match.insertion = @insertion match.substitution = @substitution match.deletion = @deletion keys = @devices.keys distances = match.match( keys ) sorted_list = keys.zip( distances ).sort{|a,b|a.last<=>b.last} use_key = sorted_list.first.first device = @devices[ use_key ] distance = sorted_list.first.last distance_normalised = distance/(user_agent.length+1) if distance_normalised > @match_threshold nil else device[ :wurfl_match ] = { :distance => distance, :distance_normalised => distance_normalised } @devices[ user_agent ] = device end end |
#clear! ⇒ Object
31 32 33 34 35 |
# File 'lib/wurfl-lite.rb', line 31 def clear! @devices = Hash.new @devices_by_id = {} @files = [] end |
#process_xml!(filename) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/wurfl-lite.rb', line 45 def process_xml!( filename ) @files << filename data = open( filename ).read begin # Try decompress it, in case it's a compressed file # XXX Yes, this does seem ugly, but is there another way? data = Zlib::GzipReader.new(StringIO.new(data.to_s)).read rescue Zlib::GzipFile::Error end doc = Hpricot( data ) keys_added = [] (doc/'devices'/'device').each do |device_element| device = Hash.new %w|id user_agent fall_back|.each do |attribute| device[ attribute.to_sym ] = device_element.attributes[ attribute ] end (device_element/'capability').each do |capability| name = capability.attributes[ 'name' ].to_sym value = capability.attributes[ 'value' ] next if value.empty? if value.to_i.to_s == value.strip value = value.to_i elsif value.strip.downcase =~ /^(true|false)$/ value = ( value.strip.downcase == 'true' ) end device[ name ] = value end keys_added << device[ :user_agent ] @devices[ device[ :user_agent ] ] = device @devices_by_id[ device[ :id ] ] = device end keys_added.each do |key| @devices[ key ][ :fall_back ] = @devices_by_id[ @devices[ key ][ :fall_back ] ] end end |
#reset! ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/wurfl-lite.rb', line 37 def reset! files = @files clear! files.each do |filename| process_xml!( filename ) end end |