Class: Wavefront::Response
- Inherits:
-
Object
- Object
- Wavefront::Response
- Includes:
- Mixins
- Defined in:
- lib/wavefront-sdk/response.rb
Overview
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.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#response ⇒ Map
readonly
The response from the API turned into a Map, which allows.
- #status ⇒ Wavefront::Types::Status readonly
Instance Method Summary collapse
- #build_response(raw) ⇒ Object
- #build_status(raw, status) ⇒ Object
-
#initialize(json, status, opts = {}) ⇒ Response
constructor
Create and return a Wavefront::Response object.
- #raw_response(json, status) ⇒ Object
- #setup_opts ⇒ Object
Methods included from Mixins
#parse_relative_time, #parse_time, #relative_time, #time_multiplier, #valid_relative_time?
Constructor Details
#initialize(json, status, opts = {}) ⇒ Response
Create and return a Wavefront::Response object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/wavefront-sdk/response.rb', line 34 def initialize(json, status, opts = {}) raw = raw_response(json, status) @status = build_status(raw, status) @response = build_response(raw) @opts = opts setup_opts logger.log(self, :debug) rescue StandardError => e logger.log(format("could not parse:\n%s", json), :debug) logger.log(e..to_s, :debug) raise Wavefront::Exception::UnparseableResponse end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
24 25 26 |
# File 'lib/wavefront-sdk/response.rb', line 24 def logger @logger end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
24 25 26 |
# File 'lib/wavefront-sdk/response.rb', line 24 def opts @opts end |
#response ⇒ Map (readonly)
Returns the response from the API turned into a Map, which allows.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wavefront-sdk/response.rb', line 22 class Response include Wavefront::Mixins attr_reader :status, :response, :opts, :logger # 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. # def initialize(json, status, opts = {}) raw = raw_response(json, status) @status = build_status(raw, status) @response = build_response(raw) @opts = opts setup_opts logger.log(self, :debug) rescue StandardError => e logger.log(format("could not parse:\n%s", json), :debug) logger.log(e..to_s, :debug) raise Wavefront::Exception::UnparseableResponse end def setup_opts @logger = Wavefront::Logger.new(opts) end def raw_response(json, status) json.empty? ? {} : JSON.parse(json, symbolize_names: true) rescue StandardError { message: json, code: status } end def build_status(raw, status) Wavefront::Type::Status.new(raw, status) end 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 end |
#status ⇒ Wavefront::Types::Status (readonly)
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wavefront-sdk/response.rb', line 22 class Response include Wavefront::Mixins attr_reader :status, :response, :opts, :logger # 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. # def initialize(json, status, opts = {}) raw = raw_response(json, status) @status = build_status(raw, status) @response = build_response(raw) @opts = opts setup_opts logger.log(self, :debug) rescue StandardError => e logger.log(format("could not parse:\n%s", json), :debug) logger.log(e..to_s, :debug) raise Wavefront::Exception::UnparseableResponse end def setup_opts @logger = Wavefront::Logger.new(opts) end def raw_response(json, status) json.empty? ? {} : JSON.parse(json, symbolize_names: true) rescue StandardError { message: json, code: status } end def build_status(raw, status) Wavefront::Type::Status.new(raw, status) end 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 end |
Instance Method Details
#build_response(raw) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/wavefront-sdk/response.rb', line 63 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) ⇒ Object
59 60 61 |
# File 'lib/wavefront-sdk/response.rb', line 59 def build_status(raw, status) Wavefront::Type::Status.new(raw, status) end |
#raw_response(json, status) ⇒ Object
53 54 55 56 57 |
# File 'lib/wavefront-sdk/response.rb', line 53 def raw_response(json, status) json.empty? ? {} : JSON.parse(json, symbolize_names: true) rescue StandardError { message: json, code: status } end |