Class: Wavefront::Response
- Inherits:
-
Object
- Object
- Wavefront::Response
- 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
-
#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, debug = false) ⇒ Response
constructor
Create and return a Wavefront::Response object.
- #raw_response(json, status) ⇒ Object
Constructor Details
#initialize(json, status, debug = false) ⇒ Response
Create and return a Wavefront::Response object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/wavefront-sdk/response.rb', line 33 def initialize(json, status, debug = false) raw = raw_response(json, status) @status = build_status(raw, status) @response = build_response(raw) p self if debug rescue => e puts "could not parse:\n#{json}" if debug puts e. if debug raise Wavefront::Exception::UnparseableResponse end |
Instance Attribute Details
#response ⇒ Map (readonly)
Returns the response from the API turned into a Map, which allows.
21 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 70 |
# File 'lib/wavefront-sdk/response.rb', line 21 class Response attr_reader :status, :response # 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 debug [Boolean] whether or not to print the exception # message if one is thrown # @raise [Wavefront::Exception::UnparseableResponse] if the # response cannot be parsed. This may be because the API # has changed underneath us. # def initialize(json, status, debug = false) raw = raw_response(json, status) @status = build_status(raw, status) @response = build_response(raw) p self if debug rescue => e puts "could not parse:\n#{json}" if debug puts e. if debug raise Wavefront::Exception::UnparseableResponse end def raw_response(json, status) json.empty? ? {} : JSON.parse(json, symbolize_names: true) rescue { message: json, code: status } end def build_status(raw, status) Wavefront::Type::Status.new(raw, status) end def build_response(raw) if raw.is_a?(Hash) if raw.key?(:response) if raw[:response].is_a?(Hash) Map(raw[:response]) else raw[:response] end else Map.new(raw) end else Map.new end end end |
#status ⇒ Wavefront::Types::Status (readonly)
21 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 70 |
# File 'lib/wavefront-sdk/response.rb', line 21 class Response attr_reader :status, :response # 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 debug [Boolean] whether or not to print the exception # message if one is thrown # @raise [Wavefront::Exception::UnparseableResponse] if the # response cannot be parsed. This may be because the API # has changed underneath us. # def initialize(json, status, debug = false) raw = raw_response(json, status) @status = build_status(raw, status) @response = build_response(raw) p self if debug rescue => e puts "could not parse:\n#{json}" if debug puts e. if debug raise Wavefront::Exception::UnparseableResponse end def raw_response(json, status) json.empty? ? {} : JSON.parse(json, symbolize_names: true) rescue { message: json, code: status } end def build_status(raw, status) Wavefront::Type::Status.new(raw, status) end def build_response(raw) if raw.is_a?(Hash) if raw.key?(:response) if raw[:response].is_a?(Hash) Map(raw[:response]) else raw[:response] end else Map.new(raw) end else Map.new end end end |
Instance Method Details
#build_response(raw) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wavefront-sdk/response.rb', line 55 def build_response(raw) if raw.is_a?(Hash) if raw.key?(:response) if raw[:response].is_a?(Hash) Map(raw[:response]) else raw[:response] end else Map.new(raw) end else Map.new end end |
#build_status(raw, status) ⇒ Object
51 52 53 |
# File 'lib/wavefront-sdk/response.rb', line 51 def build_status(raw, status) Wavefront::Type::Status.new(raw, status) end |
#raw_response(json, status) ⇒ Object
45 46 47 48 49 |
# File 'lib/wavefront-sdk/response.rb', line 45 def raw_response(json, status) json.empty? ? {} : JSON.parse(json, symbolize_names: true) rescue { message: json, code: status } end |