Class: BeanstalkApiOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/pvdgm-bs-client/beanstalk_api_options.rb

Constant Summary collapse

DEFAULT_SERVER =
'www.abaqis.com'
LOCAL_SERVER =
'localhost:3000'
UAT_SERVER =
'uat.abaqis.com'
DEFAULT_USE_SSL =
true
DEFAULT_RESOURCE =
'statistics'
DEFAULT_COMMAND =
'list'
DEFAULT_TOKEN =
ENV['API_TOKEN']

Instance Method Summary collapse

Constructor Details

#initializeBeanstalkApiOptions

Returns a new instance of BeanstalkApiOptions.



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
# File 'lib/pvdgm-bs-client/beanstalk_api_options.rb', line 11

def initialize
  @options = { all_stats: false}

  @optparse = OptionParser.new do | opts |
    # Set a banner, displayed at the top of the help screen
    opts.banner = "Usage: beanstalk_api [ options ] [ resource [ command ] ]"

    opts.on '-h', '--help', 'Display the help message' do
      puts opts
      puts 
      print_valid_resources
      puts 
      exit
    end

    @options[:server] = DEFAULT_SERVER
    opts.on '-s', '--server SERVER', "Specify the abaqis server to hit (default #{DEFAULT_SERVER})" do | server |
      @options[:server] = server
    end

    @options[:use_ssl] = DEFAULT_USE_SSL
    opts.on '-n', '--no-ssl', "If specified, SSL will NOT be used for the call to the server" do
      @options[:use_ssl] = false
    end

    opts.on '--local', 'Set the host to the localhost and non-ssl as a testing convenience' do
      @options[:server] = LOCAL_SERVER
      @options[:use_ssl] = false
    end

    opts.on '--uat', 'Set the host to the uat machine and ssl as a testing convenience' do
      @options[:server] = UAT_SERVER
      @options[:use_ssl] = true
    end

    opts.on '-l', '--list', 'List the valid commands' do
      print_valid_resources
      exit
    end

    @options[:api_token] = DEFAULT_TOKEN
    opts.on '-t', '--token TOKEN', 'The API security token to use (defaults to ENV["API_TOKEN"])' do | token |
      @options[:api_token] = token
    end

    opts.on '--tube TUBE_NAME', 'The name of the tube to use for the query' do | tube_name |
      @options[:tube] = tube_name
    end

    opts.on '--all', 'Indicates that all statistics values should be show' do 
      @options[:all_stats] = true
    end
    
  end
end

Instance Method Details

#invoke_commandObject



106
107
108
109
110
# File 'lib/pvdgm-bs-client/beanstalk_api_options.rb', line 106

def invoke_command 
  options, resource, command = parse_options!

  eval("Resources::#{ActiveSupport::Inflector.camelize(resource)}").new(options).send(command.to_sym)
end

#parse_options!(argv = ARGV) ⇒ Object



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
# File 'lib/pvdgm-bs-client/beanstalk_api_options.rb', line 67

def parse_options!(argv=ARGV)
  @optparse.parse!(argv)

  resource = argv.shift || DEFAULT_RESOURCE
  command = argv.shift || DEFAULT_COMMAND

  # Check to make sure we have a valid resource
  unless valid_resource?(resource)
    puts @optparse
    puts
    puts "Invalid resource: #{resource}"
    puts
    print_valid_resources
    exit
  end

  # Must have been a valid resource; do we have a valid command
  unless valid_command?(resource, command)
    puts @optparse
    puts
    puts "Invalid command (#{command}) for resource (#{resource})"
    puts
    print_valid_resources
    exit
  end

  # Check the API token
  if @options[:api_token].nil?
    puts @optparse
    puts
    puts "Must specify an API token either via the environment variable 'API_TOKEN' or via the -t option"
    puts
    print_valid_resources
    exit
  end

  [ @options, resource, command ]
end