Class: ThreeUsage::CLI

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

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



7
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/three_usage/cli.rb', line 7

def self.execute(stdout, arguments=[])
  config_file = File.expand_path("~/.three_usage.yml")
  
  if File.exist?(config_file)
    yaml=YAML.load_file(config_file)
    options = {
      :username => yaml['username'],
      :password => yaml['password'],
      :pin      => yaml['pin'],
      :full_details => false
    }
  else
    options = {
      :username => '',
      :password => '',
      :pin      => '',
      :full_details => false
    }
  end

  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      Retreive usage from 3 website

      Usage: #{File.basename($0)} [options]

      Options are:
    BANNER
    opts.separator ""
    opts.on("-u", "--username=USERNAME", String,
            "Username",
            "Default from ~/.three_usage.yml") { |arg| options[:username] = arg }
    opts.on("-p", "--password=PASSWORD", String,
            "Password",
            "Default from ~/.three_usage.yml") { |arg| options[:password] = arg }
    opts.on("-n", "--pin=PIN", String,
            "PIN",
            "Default from ~/.three_usage.yml") { |arg| options[:pin] = arg }
    opts.on("-f", "--full", "Show full usage information") { options[:full_details] = true}
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)

  end
  
  mandatory_options = %w( username password pin )
  abort = false
  mandatory_options.each do |field|
    if options[field.to_sym].nil? || options[field.to_sym] == ""
      puts "You must specify " + field
      abort = true
    end
  end
  exit(1) if abort
  
  path = options[:path]

  fetcher = Fetcher.new(options[:username],options[:password],options[:pin])
  parser = Parser.new(fetcher.page)
  
  if options[:full_details]
    parser.print_table
  else
    puts 'Remaining Usage: ' + parser.remaining_three_quota.to_s + ' MB'
  end
end