Class: AdobeMediaEncoder::API::CLI

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

Constant Summary collapse

LOGGING_LEVELS =
{
  :debug => Logger::DEBUG,
  :info => Logger::INFO,
  :warn => Logger::WARN,
  :error => Logger::ERROR,
  :fatal => Logger::FATAL
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ CLI

Returns a new instance of CLI.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/adobe_media_encoder/api/cli.rb', line 54

def initialize(args = {})
  args = parse_arguments.merge(args)
  @logger = Logger.new(args[:log_to])
  logger.level = args[:log_level] if args[:log_level]
  args[:logger] = logger

  @api = AdobeMediaEncoder::API::Client.new(args)

  ## LIST METHODS
  #methods = api.methods; methods -= Object.methods; methods.sort.each { |method| puts "#{method} #{api.method(method).parameters rescue ''}" }; exit

  # http.log_request_body = true
  # http.log_response_body = true
  # http.log_pretty_print_body = true


  method_name = args[:method_name]
  send(method_name, args[:method_arguments], :pretty_print => args[:pretty_print]) if method_name

end

Instance Attribute Details

#apiObject

Returns the value of attribute api.



22
23
24
# File 'lib/adobe_media_encoder/api/cli.rb', line 22

def api
  @api
end

#loggerObject

Returns the value of attribute logger.



22
23
24
# File 'lib/adobe_media_encoder/api/cli.rb', line 22

def logger
  @logger
end

Instance Method Details

#parse_argumentsObject



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
# File 'lib/adobe_media_encoder/api/cli.rb', line 24

def parse_arguments
  arguments = {
    :log_to => STDERR,
    :log_level => Logger::WARN,
    :options_file_path => File.expand_path(File.basename($0, '.*'), '~/.options'),
    :host => AdobeMediaEncoder::API::Client::HTTPClient::DEFAULT_HTTP_HOST_ADDRESS,
    :port => AdobeMediaEncoder::API::Client::HTTPClient::DEFAULT_HTTP_HOST_PORT,
  }
  op = OptionParser.new
  op.on('--host-address HOSTADDRESS', 'The AdobeAnywhere server address.',
        "\tdefault: #{arguments[:host]}") { |v| arguments[:host] = v }
  op.on('--host-port PORT', 'The port on the AdobeAnywhere server to connect to.',
        "\tdefault: #{arguments[:port]}") { |v| arguments[:port] = v }
  op.on('--method-name METHODNAME', '') { |v| arguments[:method_name] = v }
  op.on('--method-arguments JSON', '') { |v| arguments[:method_arguments] = v }
  op.on('--pretty-print', '') { |v| arguments[:pretty_print] = v }
  op.on('--log-to FILENAME', 'Log file location.', "\tdefault: STDERR") { |v| arguments[:log_to] = v }
  op.on('--log-level LEVEL', LOGGING_LEVELS.keys, "Logging level. Available Options: #{LOGGING_LEVELS.keys.join(', ')}",
        "\tdefault: #{LOGGING_LEVELS.invert[arguments[:log_level]]}") { |v| arguments[:log_level] = LOGGING_LEVELS[v] }
  op.on('--[no-]options-file [FILENAME]', 'Path to a file which contains default command line arguments.', "\tdefault: #{arguments[:options_file_path]}" ) { |v| arguments[:options_file_path] = v}
  op.on_tail('-h', '--help', 'Show this message.') { puts op; exit }
  op.parse!(ARGV.dup)

  arguments_file_path = arguments[:options_file_path]
  # Make sure that arguments from the command line override those from the arguments file
  op.parse!(ARGV.dup) if op.load(arguments_file_path)
  arguments
end

#send(method_name, method_arguments, args = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/adobe_media_encoder/api/cli.rb', line 75

def send(method_name, method_arguments, args = {})
  method_name = method_name.to_sym
  logger.debug { "Executing Method: #{method_name}" }

  send_arguments = [ method_name ]

  if method_arguments
    method_arguments = JSON.parse(method_arguments) if method_arguments.is_a?(String) and method_arguments.start_with?('{', '[')
    send_arguments << method_arguments
  end

  response = api.__send__(*send_arguments)
  puts response.respond_to?(:body) ? response.body : response

  exit
end