Class: Wurfl::Command::Uamatch

Inherits:
Wurfl::Command show all
Includes:
Utils
Defined in:
lib/wurfl/command/uamatch.rb

Overview

The uamatch command itself is based on the inspector command found in this

library.

Author:  Kai W. Zimmermann ([email protected])

Instance Method Summary collapse

Methods included from Utils

#load_wurfl_pstore, #save_wurfl_pstore

Instance Method Details

#executeObject



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
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
# File 'lib/wurfl/command/uamatch.rb', line 24

def execute
  pstorefile = nil
  useragent = nil
  query = nil
  begin
    opt = GetoptLong.new(
                         ["-d","--database", GetoptLong::REQUIRED_ARGUMENT],
                         ["-u","--useragent", GetoptLong::REQUIRED_ARGUMENT],
                         ["-q","--query", GetoptLong::REQUIRED_ARGUMENT]
                         )
    
    opt.each do |arg,val|
      case arg
      when "-d"
        pstorefile = val
      when "-u"
        useragent = val
      when "-q"
        query = val
      else
        usage
      end
    end

  rescue => err
    usage
  end

  if !pstorefile
    puts "You must specify a Wurfl PStore db"
    usage
  end

  begin
    handsets = load_wurfl_pstore(pstorefile)    
    puts "Number of Handsets: #{handsets.length}"
    uamatch = Wurfl::UserAgentMatcher.new(handsets)
  rescue => err
    STDERR.puts "Error with file #{pstorefile}"
    STDERR.puts err.message
    exit 1
  end

  if useragent 
    matches, distance = uamatch.match_handsets(useragent)
    puts "Shortest distance #{distance}, #{matches.length} match#{'es' if (matches.length!=1)}" 
    matches.each do |handset|
      puts "Handset wurfl id: #{handset.wurfl_id}"
      puts "User_agent found: #{handset.user_agent}"
      if query
        puts "Result of handset query: #{query}"
        puts "#{handset[query]} from #{handset.owner(query)}"
      end
    end
    exit 0
  end
end

#usageObject



16
17
18
19
20
21
22
# File 'lib/wurfl/command/uamatch.rb', line 16

def usage
  puts "Usage: wurfltools.rb uamatch [-u user_agent [-q attributename]] -d pstorefile"
  puts "Examples:"
  puts "wurfltools.rb uamatch -d pstorehandsets.db -u SL55"
  puts "wurfltools.rb uamatch -d pstorehandsets.db -u SL55 -q wallpaper_jpg"
  exit 1
end