Class: Wavefront::Response
- Inherits:
-
Object
- Object
- Wavefront::Response
- Includes:
- Mixins
- Defined in:
- lib/wavefront-sdk/core/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
The response from the API turned into a Map, which allows.
- #status ⇒ Wavefront::Types::Status readonly
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Were there items in the response?.
-
#ids ⇒ Object
We often want the IDs of all the objects we retrieved.
-
#initialize(json, status, opts = {}) ⇒ Response
constructor
Create and return a Wavefront::Response object.
-
#more_items? ⇒ Bool
Are there more items in paginated output?.
-
#names ⇒ Object
We often want the names of all the objects we retrieved.
-
#next_item ⇒ Integer, ...
On paginated output, the offset of the next item, or nil.
-
#ok? ⇒ Bool
Was the API’s response positive?.
-
#to_s ⇒ String
A printable version of a Wavefront::Response object.
Methods included from Mixins
#log, #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
39 40 41 42 43 44 45 46 47 48 49 |
# 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..to_s, :debug) raise Wavefront::Exception::UnparseableResponse end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
28 29 30 |
# File 'lib/wavefront-sdk/core/response.rb', line 28 def logger @logger end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
28 29 30 |
# File 'lib/wavefront-sdk/core/response.rb', line 28 def opts @opts end |
#response ⇒ Map
Returns the response from the API turned into a Map, which allows.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/wavefront-sdk/core/response.rb', line 26 class Response include Wavefront::Mixins attr_reader :status, :opts, :logger attr_accessor :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 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 = {}) 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..to_s, :debug) raise Wavefront::Exception::UnparseableResponse end # Were there items in the response? # def empty? response.items.empty? rescue StandardError false end # Was the API's response positive? # @return [Bool] # def ok? respond_to?(:status) && status.result == 'OK' end # Are there more items in paginated output? # @return [Bool] # def more_items? response.moreItems ? true : false rescue StandardError false 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] # def next_item return nil unless more_items? return response.cursor if response.respond_to?(:cursor) response.offset + response.limit rescue StandardError nil end # A printable version of a Wavefront::Response object # @return [String] # def to_s inspect.to_s end # We often want the IDs of all the objects we retrieved. This # does it. def ids response.items.map(&:id) end # We often want the names of all the objects we retrieved. # def names response.items.map(&:name) end private def setup_vars(opts) @opts = opts @logger = Wavefront::Logger.new(opts) end # @params raw [Hash] created by #raw_response # 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 # Turn the API's JSON response and HTTP status code into a Ruby # object. # @return [Hash] # 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 end |
#status ⇒ Wavefront::Types::Status (readonly)
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/wavefront-sdk/core/response.rb', line 26 class Response include Wavefront::Mixins attr_reader :status, :opts, :logger attr_accessor :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 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 = {}) 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..to_s, :debug) raise Wavefront::Exception::UnparseableResponse end # Were there items in the response? # def empty? response.items.empty? rescue StandardError false end # Was the API's response positive? # @return [Bool] # def ok? respond_to?(:status) && status.result == 'OK' end # Are there more items in paginated output? # @return [Bool] # def more_items? response.moreItems ? true : false rescue StandardError false 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] # def next_item return nil unless more_items? return response.cursor if response.respond_to?(:cursor) response.offset + response.limit rescue StandardError nil end # A printable version of a Wavefront::Response object # @return [String] # def to_s inspect.to_s end # We often want the IDs of all the objects we retrieved. This # does it. def ids response.items.map(&:id) end # We often want the names of all the objects we retrieved. # def names response.items.map(&:name) end private def setup_vars(opts) @opts = opts @logger = Wavefront::Logger.new(opts) end # @params raw [Hash] created by #raw_response # 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 # Turn the API's JSON response and HTTP status code into a Ruby # object. # @return [Hash] # 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 end |
Instance Method Details
#empty? ⇒ Boolean
Were there items in the response?
53 54 55 56 57 |
# File 'lib/wavefront-sdk/core/response.rb', line 53 def empty? response.items.empty? rescue StandardError false end |
#ids ⇒ Object
We often want the IDs of all the objects we retrieved. This does it.
99 100 101 |
# File 'lib/wavefront-sdk/core/response.rb', line 99 def ids response.items.map(&:id) end |
#more_items? ⇒ Bool
Are there more items in paginated output?
69 70 71 72 73 |
# File 'lib/wavefront-sdk/core/response.rb', line 69 def more_items? response.moreItems ? true : false rescue StandardError false end |
#names ⇒ Object
We often want the names of all the objects we retrieved.
105 106 107 |
# File 'lib/wavefront-sdk/core/response.rb', line 105 def names response.items.map(&:name) end |
#next_item ⇒ Integer, ...
On paginated output, the offset of the next item, or nil. For classes which use a cursor rather than an offset (Source), that.
80 81 82 83 84 85 86 87 |
# 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? ⇒ Bool
Was the API’s response positive?
62 63 64 |
# File 'lib/wavefront-sdk/core/response.rb', line 62 def ok? respond_to?(:status) && status.result == 'OK' end |
#to_s ⇒ String
A printable version of a Wavefront::Response object
92 93 94 |
# File 'lib/wavefront-sdk/core/response.rb', line 92 def to_s inspect.to_s end |