Class: IspUsage::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/ispusage.rb

Class Method Summary collapse

Class Method Details

.run!(*args) ⇒ Object



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
54
55
56
57
# File 'lib/ispusage.rb', line 22

def run!(*args)
  options = {:output => :text}
  optparse = OptionParser.new do|opts|
    opts.on('-i', '--isp isp', 'ISP') {|i| options[:isp] = i}
    opts.on('-u', '--username username', 'Username') {|u| options[:username] = u}
    opts.on('-p', '--password password', 'Password') {|p| options[:password] = p}
    opts.on('-q', '--pin ', 'PIN') {|pin| options[:pin] = pin}
    opts.on('-j', '--json', 'JSON Output') {|j| options[:output] = :json}
    opts.on('--list-isps', 'List ISP classes available') {|opt| options[:list_isps] = true}
  end
  optparse.parse!

  if options[:list_isps]
    puts ISPS.to_json
    return 0
  end
  
  unless ISPS.map{|i| i[:class]}.include?(options[:isp])
    puts 'Unknown ISP: ' + options[:isp]
    return 1
  end

  ispclass = eval("IspUsage::Fetchers::#{options[:isp]}")
  fetcher = ispclass.new(options)

  fetcher.fetch_usage
  case options[:output]
  when :text
    fetcher.usage_periods.each do |usage_period|
      puts usage_period.label + ':' + usage_period.used.to_s + '/' + usage_period.quota.to_s
    end
  when :json
    puts fetcher.to_json
  end
  return 0
end