Class: WurflDevice::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/wurfl_device/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



12
13
14
15
16
17
# File 'lib/wurfl_device/cli.rb', line 12

def initialize(*)
  super
  the_shell = (options["no-color"] ? Thor::Shell::Basic.new : shell)
  WurflDevice.ui = UI::Shell.new(the_shell)
  WurflDevice.ui.debug! if options["verbose"]
end

Instance Method Details

#dump(device_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wurfl_device/cli.rb', line 34

def dump(device_id)
  capabilities = WurflDevice.capabilities_from_id(device_id)
  capabilities = WurflDevice.capabilities_from_user_agent(device_id) if capabilities['id'].nil?

  if capabilities.nil?
    WurflDevice.ui.info "Nothing to dump"
  elsif options.json?
    WurflDevice.ui.info capabilities.to_json
  else
    WurflDevice.ui.info capabilities.to_yaml
  end
end

#help(cli = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/wurfl_device/cli.rb', line 24

def help(cli=nil)
  version
  WurflDevice.ui.info "http://github.com/aputs/wurfl_device for README"
  WurflDevice.ui.info ""
  super
end

#init(xml_file = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wurfl_device/cli.rb', line 66

def init(xml_file=nil)
  xml_file ||= Settings.default_wurfl_xml_file
  unless options.update?
    WurflDevice.ui.info "clearing existing device cache."
    Cache.clear
  end
  WurflDevice.ui.info "initializing wurfl device cache."
  Cache.initialize_cache(xml_file)

  if options.update?
    WurflDevice.ui.info "rebuilding user agent cache."
    Cache.rebuild_user_agents
  end

  status true
end

#listObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wurfl_device/cli.rb', line 49

def list
  matched_only = options['matched-only']
  if matched_only
    Cache::UserAgentsMatched.entries.each do |user_agent|
      capabilities = WurflDevice.capabilities_from_user_agent(user_agent)
      WurflDevice.ui.info "#{user_agent}:#{capabilities.id}"
    end
  else
    Cache::UserAgents.entries.each do |user_agent|
      device_id = Cache::UserAgents.get(user_agent)
      WurflDevice.ui.info "#{user_agent}:#{device_id}"
    end
  end
end

#status(skip_version = false) ⇒ Object



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
# File 'lib/wurfl_device/cli.rb', line 84

def status(skip_version=false)
  version unless skip_version
  unless Cache.initialized?
    WurflDevice.ui.info "cache is not initialized"
    return
  end
  WurflDevice.ui.info "cache info:"
  WurflDevice.ui.info "  wurfl-xml version: " + Cache::Status.version.join(' ')
  WurflDevice.ui.info "  cache last updated: " + Cache::Status.last_updated
  devices = Cache::Devices.entries
  user_agents = Cache::UserAgents.entries
  user_agents_message = ''
  user_agents_message = " (warning count should be greater than or equal to devices count)" if user_agents.length < devices.length 

  WurflDevice.ui.info "  " + commify(devices.length) + " device id's"
  WurflDevice.ui.info "  " + commify(user_agents.length) + " user agents in cache" + user_agents_message
  WurflDevice.ui.info "  " + commify(Cache::UserAgentsMatched.entries.count) + " user agents matched"

  user_agent_manufacturers = Array.new
  Cache::UserAgentsManufacturers.entries.each do |index|
    user_agent_manufacturers << "#{index}(" + commify(Cache::UserAgentsManufacturers.hkeys(index).length) + ")"
  end
  user_agent_manufacturers.sort!
  WurflDevice.ui.info "wurfl user agent manufacturers:"
  while !user_agent_manufacturers.empty?
    sub = user_agent_manufacturers.slice!(0, 7)
    WurflDevice.ui.info "  " + sub.join(', ')
  end
  WurflDevice.ui.info ""
end

#versionObject



117
118
119
# File 'lib/wurfl_device/cli.rb', line 117

def version
  WurflDevice.ui.info "wurfl_device version #{VERSION.freeze}"
end