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 = "      Retreive usage from 3 website\n\n      Usage: \#{File.basename($0)} [options]\n\n      Options are:\n    BANNER\n    opts.separator \"\"\n    opts.on(\"-u\", \"--username=USERNAME\", String,\n            \"Username\",\n            \"Default from ~/.three_usage.yml\") { |arg| options[:username] = arg }\n    opts.on(\"-p\", \"--password=PASSWORD\", String,\n            \"Password\",\n            \"Default from ~/.three_usage.yml\") { |arg| options[:password] = arg }\n    opts.on(\"-n\", \"--pin=PIN\", String,\n            \"PIN\",\n            \"Default from ~/.three_usage.yml\") { |arg| options[:pin] = arg }\n    opts.on(\"-f\", \"--full\", \"Show full usage information\") { options[:full_details] = true}\n    opts.on(\"-h\", \"--help\",\n            \"Show this help message.\") { stdout.puts opts; exit }\n    opts.parse!(arguments)\n\n  end\n  \n  mandatory_options = %w( username password pin )\n  abort = false\n  mandatory_options.each do |field|\n    if options[field.to_sym].nil? || options[field.to_sym] == \"\"\n      puts \"You must specify \" + field\n      abort = true\n    end\n  end\n  exit(1) if abort\n  \n  path = options[:path]\n\n  fetcher = Fetcher.new(options[:username],options[:password],options[:pin])\n  parser = Parser.new(fetcher.page)\n  \n  if options[:full_details]\n    parser.print_table\n  else\n    puts 'Remaining Usage: ' + parser.remaining_three_quota.to_s + ' MB'\n  end\nend\n".gsub(/^          /,'')