Class: WurflDevice::UserAgentMatcher
- Inherits:
-
Object
- Object
- WurflDevice::UserAgentMatcher
- Defined in:
- lib/wurfl_device/user_agent_matcher.rb
Instance Attribute Summary collapse
-
#capabilities ⇒ Object
Returns the value of attribute capabilities.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #ld_match(user_agent, tolerance = nil) ⇒ Object
- #match(user_agent) ⇒ Object
- #ris_match(user_agent, tolerance = nil) ⇒ Object
Instance Attribute Details
#capabilities ⇒ Object
Returns the value of attribute capabilities.
6 7 8 |
# File 'lib/wurfl_device/user_agent_matcher.rb', line 6 def capabilities @capabilities end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
6 7 8 |
# File 'lib/wurfl_device/user_agent_matcher.rb', line 6 def user_agent @user_agent end |
Instance Method Details
#ld_match(user_agent, tolerance = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/wurfl_device/user_agent_matcher.rb', line 72 def ld_match(user_agent, tolerance=nil) tolerance = Settings::WORST_MATCH if tolerance.nil? user_agent_list = Cache::UserAgentsManufacturers.hkeys(user_agent.manufacturer).sort length = user_agent.length best = tolerance current = 0 match = nil user_agent_list.each do |ua| next unless ua.length.between?(length - tolerance, length + tolerance) current = Text::Levenshtein.distance(ua, user_agent) if current <= best best = current match = ua end end return match unless match.nil? return nil end |
#match(user_agent) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/wurfl_device/user_agent_matcher.rb', line 8 def match(user_agent) user_agent = UserAgent.new(user_agent) unless user_agent.kind_of?(UserAgent) @user_agent = user_agent.dup # exact match matched_data = Cache::UserAgentsMatched.get(@user_agent) unless matched_data.nil? @capabilities = Capability.new(MessagePack.unpack(matched_data.force_encoding('US-ASCII'))) if !(@capabilities.nil? || @capabilities.empty?) return self end end matched_ua = nil matcher = "matcher_#{@user_agent.manufacturer.downcase}" if self.respond_to?(matcher) matched_ua = self.send(matcher, @user_agent) else if @user_agent =~ /^Mozilla/i tolerance = 5 matched_ua = ld_match(@user_agent, tolerance) else tolerance = @user_agent.first_slash matched_ua = ris_match(@user_agent, tolerance) end end unless matched_ua.nil? device_id = Cache::UserAgents.get(matched_ua) device_id = Settings::GENERIC_XHTML if device_id.nil? || device_id.empty? @capabilities = Cache.build_capabilities(device_id) end if @capabilities.nil? || @capabilities.empty? device_id = Cache::UserAgents.get(last_attempts(@user_agent)) device_id = Settings::GENERIC_XHTML if device_id.nil? || device_id.empty? @capabilities = Cache.build_capabilities(device_id) end if !(@capabilities.nil? || @capabilities.empty?) Cache::UserAgentsMatched.set @user_agent, MessagePack.pack(@capabilities) end return self end |
#ris_match(user_agent, tolerance = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/wurfl_device/user_agent_matcher.rb', line 55 def ris_match(user_agent, tolerance=nil) tolerance = Settings::WORST_MATCH if tolerance.nil? user_agent_list = Cache::UserAgentsManufacturers.hkeys(user_agent.manufacturer).sort curlen = user_agent.length while curlen >= tolerance user_agent_list.each do |ua| next if ua.length < curlen if ua.index(user_agent) == 0 return ua end end user_agent = user_agent.slice(0, curlen-1) curlen = user_agent.length end return nil end |