class Wavefront::Response

Every API path has its own response class, which allows us to provide a stable interface. If the API changes underneath us, the SDK will break in a predictable way, throwing a Wavefront::Exception::UnparseableResponse exception.

Most Wavefront::Response classes present the returned data in two parts, each accessible by dot notation.

@!attribute [r] status

@return [Wavefront::Types::Status]

@!attribute [r] response

@return [Map] the response from the API turned into a Map,
  which  allows

Attributes

logger[R]
opts[R]
response[RW]
status[R]

Public Class Methods

new(json, status, opts = {}) click to toggle source

Create and return a Wavefront::Response object @param json [String] a raw response body from the Wavefront API @param status [Integer] HTTP return code from the API @param opts [Hash] options passed through from calling class. @raise [Wavefront::Exception::UnparseableResponse] if the

response cannot be parsed. This may be because the API
has changed underneath us.
# File lib/wavefront-sdk/core/response.rb, line 39
def initialize(json, status, opts = {})
  setup_vars(opts)
  raw       = raw_response(json, status)
  @status   = build_status(raw, status)
  @response = build_response(raw)
  logger.log(self, :debug)
rescue StandardError => e
  logger.log(format("could not parse:\n%<str>s", str: json), :debug)
  logger.log(e.message.to_s, :debug)
  raise Wavefront::Exception::UnparseableResponse
end

Public Instance Methods

empty?() click to toggle source

Were there items in the response?

# File lib/wavefront-sdk/core/response.rb, line 53
def empty?
  response.items.size.zero?
rescue StandardError
  false
end
ids() click to toggle source

We often want the IDs of all the objects we retrieved. This does it.

# File lib/wavefront-sdk/core/response.rb, line 99
def ids
  response.items.map(&:id)
end
more_items?() click to toggle source

Are there more items in paginated output? @return [Bool]

# File lib/wavefront-sdk/core/response.rb, line 69
def more_items?
  response.moreItems ? true : false
rescue StandardError
  false
end
names() click to toggle source

We often want the names of all the objects we retrieved.

# File lib/wavefront-sdk/core/response.rb, line 105
def names
  response.items.map(&:name)
end
next_item() click to toggle source

On paginated output, the offset of the next item, or nil. For classes which use a cursor rather than an offset (Source), that. @return [Integer,String,Nil]

# File lib/wavefront-sdk/core/response.rb, line 80
def next_item
  return nil unless more_items?
  return response.cursor if response.respond_to?(:cursor)

  response.offset + response.limit
rescue StandardError
  nil
end
ok?() click to toggle source

Was the API's response positive? @return [Bool]

# File lib/wavefront-sdk/core/response.rb, line 62
def ok?
  respond_to?(:status) && status.result == 'OK'
end
to_s() click to toggle source

A printable version of a Wavefront::Response object @return [String]

# File lib/wavefront-sdk/core/response.rb, line 92
def to_s
  inspect.to_s
end

Private Instance Methods

build_response(raw) click to toggle source

@params raw [Hash] created by raw_response

# File lib/wavefront-sdk/core/response.rb, line 118
def build_response(raw)
  return Map.new unless raw.is_a?(Hash)
  return Map.new(raw) unless raw.key?(:response)
  return raw[:response] unless raw[:response].is_a?(Hash)

  Map(raw[:response])
end
build_status(raw, status) click to toggle source
# File lib/wavefront-sdk/core/response.rb, line 136
def build_status(raw, status)
  Wavefront::Type::Status.new(raw, status)
end
raw_response(json, status) click to toggle source

Turn the API's JSON response and HTTP status code into a Ruby object. @return [Hash]

# File lib/wavefront-sdk/core/response.rb, line 130
def raw_response(json, status)
  json.empty? ? {} : JSON.parse(json, symbolize_names: true)
rescue StandardError
  { message: json, code: status }
end
setup_vars(opts) click to toggle source
# File lib/wavefront-sdk/core/response.rb, line 111
def setup_vars(opts)
  @opts   = opts
  @logger = Wavefront::Logger.new(opts)
end