class ServiceMgrOptions

Constants

DEFAULT_COMMAND
DEFAULT_RESOURCE
DEFAULT_SERVER
DEFAULT_TOKEN
DEFAULT_USE_SSL
LOCAL_SERVER
TEST_SERVER
UAT_SERVER

Public Class Methods

new() click to toggle source
# File lib/pvdgm-svc-client/service_mgr_options.rb, line 12
def initialize
  @options = {}

  @optparse = OptionParser.new do | opts |
    # Set a banner, displayed at the top of the help screen
    opts.banner = "Usage: service_mgr [ 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 '-u', '--unsafe', "If specified, will turn of SSL CA verification" do
      OpenSSL::SSL.const_set(:VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE) 
    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 '--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 '--test', 'Set the host to the test machine and ssl as a testing convenience' do
      @options[:server] = TEST_SERVER
      @options[:use_ssl] = true
    end

    @options[:trace] = false
    opts.on '--trace', 'Set the output to verbose logging (including stack trace)' do
      @options[:trace] = true
    end

    opts.on '--account-mapping-id ID', 'The ID of the account mapping object' do | id |
      @options[:account_mapping_id] = id
    end

    opts.on '--configured-account-id ID', 'The ID of the configured account' do | id |
      @options[:configured_account_id] = id
    end

    opts.on '--facility-mapping-id ID', 'The ID of the facility mapping' do | id |
      @options[:facility_mapping_id] = id
    end

    opts.on '--service-definition-id ID', 'The ID of the service definition' do | id |
      @options[:service_definition_id] = id
    end

    opts.on '--service-id ID', 'The ID of the service' do | id |
      @options[:service_id] = id
    end

    opts.on '--third-party-id ID', 'The ID of the third party' do | id |
      @options[:third_party_id] = id
    end

  end
end

Public Instance Methods

invoke_command() click to toggle source
# File lib/pvdgm-svc-client/service_mgr_options.rb, line 137
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) click to toggle source
# File lib/pvdgm-svc-client/service_mgr_options.rb, line 98
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

Private Instance Methods

print_valid_commands(resource) click to toggle source
print_valid_resources() click to toggle source
valid_command?(resource, command) click to toggle source
# File lib/pvdgm-svc-client/service_mgr_options.rb, line 145
def valid_command?(resource, command)
  return false if resource.blank? || command.blank?
  eval("Resources::#{ActiveSupport::Inflector.camelize(resource)}").instance_methods(false).include?(command.to_sym)
end
valid_resource?(resource) click to toggle source
# File lib/pvdgm-svc-client/service_mgr_options.rb, line 150
def valid_resource?(resource)
  Resources.constants.map { | const | ActiveSupport::Inflector.underscore(const.to_s) }.include?(resource)
end