class WavefrontOutput::Base

WavefrontCli::Base looks for a class WavefrontOutput::Format where 'Format' is something like 'Json', or 'Yaml'. If it finds that class it creates a new instance, passing through the response object (@resp) and options hash (@options), then calls the run method.

All those classes are an extension of this one. Some, like Json or Yaml, are generic, and dump a straight translation of the response object. Others, like Hcl or Wavefront, have subclasses which deal with the output of specific commands.

Attributes

cmd[R]
options[R]
resp[R]

Public Class Methods

new(resp = {}, options = {}) click to toggle source
# File lib/wavefront-cli/output/base.rb, line 19
def initialize(resp = {}, options = {})
  @cmd = options[:class]
  @options = options
  @resp = filtered_response(resp)
end

Public Instance Methods

_run() click to toggle source
# File lib/wavefront-cli/output/base.rb, line 32
def _run
  command_class.run
end
allow_items_only?() click to toggle source
# File lib/wavefront-cli/output/base.rb, line 74
def allow_items_only?
  false
end
command_class() click to toggle source
# File lib/wavefront-cli/output/base.rb, line 69
def command_class
  require_relative command_file
  Object.const_get(command_class_name).new(resp, options)
end
command_class_name() click to toggle source
# File lib/wavefront-cli/output/base.rb, line 59
def command_class_name
  format('Wavefront%<format>sOutput::%<command>s',
         format: my_format.capitalize,
         command: cmd.capitalize)
end
command_file() click to toggle source
# File lib/wavefront-cli/output/base.rb, line 65
def command_file
  File.join(my_format, cmd)
end
filtered_response(resp) click to toggle source
# File lib/wavefront-cli/output/base.rb, line 36
def filtered_response(resp)
  return resp unless options[:itemsonly]

  items_only(resp)
end
items_only(resp) click to toggle source
# File lib/wavefront-cli/output/base.rb, line 42
def items_only(resp)
  if allow_items_only?
    return resp[:items] if resp.key?(:items)

    raise(WavefrontCli::Exception::UnsupportedOutput,
          'API response does not contain items object.')
  end

  raise(WavefrontCli::Exception::UnsupportedOutput,
        format("'%<format>s' format does not support items-only output.",
               format: my_format))
end
my_format() click to toggle source
# File lib/wavefront-cli/output/base.rb, line 55
def my_format
  self.class.name.split('::').last.downcase
end
run() click to toggle source

We used to call run directly, but now we use this wrapper to make it easier to test the #_run methods.

# File lib/wavefront-cli/output/base.rb, line 28
def run
  puts _run
end