Class: Wurfl::Command::Inspector

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

Defined Under Namespace

Classes: WurflInspector

Instance Method Summary collapse

Methods included from Utils

#load_wurfl_pstore, #save_wurfl_pstore

Instance Method Details

#executeObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wurfl/command/inspector.rb', line 60

def execute
  pstorefile = nil
  procstr = nil 
  handset = nil
  query = nil
  begin
    opt = GetoptLong.new(
                         ["-d","--database", GetoptLong::REQUIRED_ARGUMENT],
                         ["-s","--search", GetoptLong::REQUIRED_ARGUMENT],
                         ["-i","--id", GetoptLong::REQUIRED_ARGUMENT],
                         ["-q","--query", GetoptLong::REQUIRED_ARGUMENT]
                         )
    
    opt.each do |arg,val|
      case arg
      when "-d"
        pstorefile = val
      when "-s"
        procstr = val.strip
      when "-i"
        handset = 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)    
    insp = WurflInspector.new(handsets)
  rescue => err
    STDERR.puts "Error with file #{pstorefile}"
    STDERR.puts err.message
    exit 1
  end

  if procstr
    pr = nil
    eval("pr = proc#{procstr}")
    
    if pr.class != Proc
      puts "You must pass a valid ruby block!"
      exit 1
    end
    
    puts "--------- Searching handsets -----------"
    res = insp.search_handsets(pr)
    puts "Number of results: #{res.size}"
    
    res.each { |handset| puts handset.wurfl_id }
    exit 0
  end


  if handset
    handset = insp.get_handset(handset)
    unless handset
      puts "No handset found"
      exit 1
    end
    puts "Handset user agent: #{handset.user_agent}"
    if query
      puts "Result of handset query: #{query}"
      rez = handset.get_value_and_owner(query)
      puts "#{rez[0]} from #{rez[1]}"
    else
      puts "Attributes of handset"
      keys = handset.keys
      keys.each do |key|
        rez = handset.get_value_and_owner(key)
        puts "Attr:#{key} Val:#{rez[0]} from #{rez[1]}"
      end
    end
    exit 0
  end
end

#usageObject



51
52
53
54
55
56
57
58
# File 'lib/wurfl/command/inspector.rb', line 51

def usage
  puts "Usage: wurfltools.rb inspector  [-s rubyblock] [-i handsetid [-q attributename]]  -d pstorefile"
  puts "Examples:"
  puts "wurfltools.rb inspector -d pstorehandsets.db -s '{ |hand| hand[\"colors\"].to_i > 2 }'"
  puts "wurfltools inspector -d pstorehandsets.db -i sonyericsson_t300_ver1"
  puts "wurfltools inspector -d pstorehandsets.db -i sonyericsson_t300_ver1 -q backlight"
  exit 1
end