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
Public Class Methods
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
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
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
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
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
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
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
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
@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
# File lib/wavefront-sdk/core/response.rb, line 136 def build_status(raw, status) Wavefront::Type::Status.new(raw, status) end
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
# File lib/wavefront-sdk/core/response.rb, line 111 def setup_vars(opts) @opts = opts @logger = Wavefront::Logger.new(opts) end