Class: KazeClient::Response
Overview
Represents response from a Kaze API
Instance Attribute Summary collapse
-
#original_response ⇒ HTTParty::Response
readonly
The response object from HTTParty call.
-
#parsed_response ⇒ Hash
readonly
The JSON object resulting from HTTParty parsed response.
Instance Method Summary collapse
-
#fetch_child(id) ⇒ Hash?
Find the child whose id is the given one in this response.
-
#fetch_data_from_child(id, field: 'data') ⇒ Object?
Find the first child with the given id and return the given field.
-
#fetch_widgets ⇒ Array
Find all the widget in this response.
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #method_missing(method_name, *args, &block) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#update_data_in_child(id, value, field: 'data') ⇒ KazeClient::Response
Find the first child with the given id and update value of the given field Does nothing when
id
was not found.
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
19 20 21 22 |
# File 'lib/kaze_client/response.rb', line 19 def initialize(response) @original_response = response @parsed_response = response.parsed_response end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/kaze_client/response.rb', line 24 def method_missing(method_name, *args, &block) if %i[original_response parsed_response].include?(method_name) send(method_name) else @parsed_response.send(method_name, *args, &block) end end |
Instance Attribute Details
#original_response ⇒ HTTParty::Response (readonly)
Returns The response object from HTTParty call.
13 14 15 |
# File 'lib/kaze_client/response.rb', line 13 def original_response @original_response end |
#parsed_response ⇒ Hash (readonly)
Returns The JSON object resulting from HTTParty parsed response.
16 17 18 |
# File 'lib/kaze_client/response.rb', line 16 def parsed_response @parsed_response end |
Instance Method Details
#fetch_child(id) ⇒ Hash?
Find the child whose id is the given one in this response
41 42 43 |
# File 'lib/kaze_client/response.rb', line 41 def fetch_child(id) ::KazeClient::JsonUtils.fetch_node(self, %{$..children[?(@['id'] == '#{id}')]}) end |
#fetch_data_from_child(id, field: 'data') ⇒ Object?
Find the first child with the given id and return the given field
51 52 53 |
# File 'lib/kaze_client/response.rb', line 51 def fetch_data_from_child(id, field: 'data') fetch_child(id)&.[](field) end |
#fetch_widgets ⇒ Array
Find all the widget in this response. Technically, it fetch all the children whose type is widget_*
60 61 62 |
# File 'lib/kaze_client/response.rb', line 60 def ::KazeClient::JsonUtils.fetch_nodes(self, %{$..children[?(@['type'] =~ /^widget_/)]}) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
32 33 34 |
# File 'lib/kaze_client/response.rb', line 32 def respond_to_missing?(method_name, include_private=false) %i[original_response parsed_response].include?(method_name) || super end |
#update_data_in_child(id, value, field: 'data') ⇒ KazeClient::Response
Find the first child with the given id and update value of the given field Does nothing when id
was not found. Add a field when field
is not found.
73 74 75 76 77 |
# File 'lib/kaze_client/response.rb', line 73 def update_data_in_child(id, value, field: 'data') fetch_child(id)&.[]=(field.to_s, value) self end |