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.



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

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.



15
16
17
# File 'lib/wavefront-cli/output/base.rb', line 15

def cmd
  @cmd
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/wavefront-cli/output/base.rb', line 15

def options
  @options
end

#respObject (readonly)

Returns the value of attribute resp.



15
16
17
# File 'lib/wavefront-cli/output/base.rb', line 15

def resp
  @resp
end

Instance Method Details

#_runObject



30
31
32
# File 'lib/wavefront-cli/output/base.rb', line 30

def _run
  command_class.run
end

#allow_items_only?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/wavefront-cli/output/base.rb', line 69

def allow_items_only?
  false
end

#command_classObject



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

def command_class
  require_relative command_file
  Object.const_get(command_class_name).new(resp, options)
end

#command_class_nameObject



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

def command_class_name
  format('Wavefront%sOutput::%s', my_format.capitalize, cmd.capitalize)
end

#command_fileObject



60
61
62
# File 'lib/wavefront-cli/output/base.rb', line 60

def command_file
  File.join(my_format, cmd)
end

#filtered_response(resp) ⇒ Object



34
35
36
37
# File 'lib/wavefront-cli/output/base.rb', line 34

def filtered_response(resp)
  return resp unless options[:itemsonly]
  items_only(resp)
end

#items_only(resp) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wavefront-cli/output/base.rb', line 39

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("'%s' format does not support items-only output.",
               my_format))
end

#my_formatObject



52
53
54
# File 'lib/wavefront-cli/output/base.rb', line 52

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.



26
27
28
# File 'lib/wavefront-cli/output/base.rb', line 26

def run
  puts _run
end