Class: WavefrontOutput::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-cli/output/base.rb

Overview

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.

Direct Known Subclasses

Csv, Hcl, Json, Ruby, Wavefront, Yaml

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resp = {}, options = {}) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
# File 'lib/wavefront-cli/output/base.rb', line 19

def initialize(resp = {}, options = {})
  @cmd = options[:class]
  @options = options
  @resp = filtered_response(resp)
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



17
18
19
# File 'lib/wavefront-cli/output/base.rb', line 17

def cmd
  @cmd
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/wavefront-cli/output/base.rb', line 17

def options
  @options
end

#respObject (readonly)

Returns the value of attribute resp.



17
18
19
# File 'lib/wavefront-cli/output/base.rb', line 17

def resp
  @resp
end

Instance Method Details

#_runObject



32
33
34
# File 'lib/wavefront-cli/output/base.rb', line 32

def _run
  command_class.run
end

#allow_items_only?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/wavefront-cli/output/base.rb', line 74

def allow_items_only?
  false
end

#command_classObject



69
70
71
72
# 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_nameObject



59
60
61
62
63
# 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_fileObject



65
66
67
# File 'lib/wavefront-cli/output/base.rb', line 65

def command_file
  File.join(my_format, cmd)
end

#filtered_response(resp) ⇒ Object



36
37
38
39
40
# 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) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# 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_formatObject



55
56
57
# File 'lib/wavefront-cli/output/base.rb', line 55

def my_format
  self.class.name.split('::').last.downcase
end

#runObject

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



28
29
30
# File 'lib/wavefront-cli/output/base.rb', line 28

def run
  puts _run
end