Class: HaveAPI::Client::Response
- Inherits:
-
Object
- Object
- HaveAPI::Client::Response
- Defined in:
- lib/haveapi/client/response.rb
Overview
Represents a response from the API.
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access namespaced params directly.
-
#each ⇒ Object
Iterate over namespaced items directly.
- #errors ⇒ Object
- #failed? ⇒ Boolean
-
#initialize(action, response) ⇒ Response
constructor
Create instance.
- #message ⇒ Object
- #meta ⇒ Object
- #ok? ⇒ Boolean
- #response ⇒ Object
- #to_hash ⇒ Object
-
#wait_for_completion ⇒ Object
Block until the action is completed or timeout occurs.
Constructor Details
#initialize(action, response) ⇒ Response
Create instance. action
being the called action and response
a received hash.
7 8 9 10 |
# File 'lib/haveapi/client/response.rb', line 7 def initialize(action, response) @action = action @response = response end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
3 4 5 |
# File 'lib/haveapi/client/response.rb', line 3 def action @action end |
Instance Method Details
#[](key) ⇒ Object
Access namespaced params directly.
46 47 48 49 50 |
# File 'lib/haveapi/client/response.rb', line 46 def [](key) return unless %i[object hash].include?(@action.output_layout.to_sym) @response[:response][@action.namespace(:output).to_sym][key] end |
#each ⇒ Object
Iterate over namespaced items directly. Works for only for object_list or hash_list.
54 55 56 57 58 |
# File 'lib/haveapi/client/response.rb', line 54 def each return unless %i[list].include?(@action.layout.to_sym) @response[:response][@action.namespace(:output).to_sym].each end |
#errors ⇒ Object
41 42 43 |
# File 'lib/haveapi/client/response.rb', line 41 def errors @response[:errors] end |
#failed? ⇒ Boolean
16 17 18 |
# File 'lib/haveapi/client/response.rb', line 16 def failed? !ok? end |
#message ⇒ Object
37 38 39 |
# File 'lib/haveapi/client/response.rb', line 37 def @response[:message] end |
#meta ⇒ Object
29 30 31 |
# File 'lib/haveapi/client/response.rb', line 29 def @response[:response][:_meta] # FIXME: read _meta from API description end |
#ok? ⇒ Boolean
12 13 14 |
# File 'lib/haveapi/client/response.rb', line 12 def ok? @response[:status] end |
#response ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/haveapi/client/response.rb', line 20 def response case @action.output_layout when :object, :object_list, :hash, :hash_list @response[:response][@action.namespace(:output).to_sym] else @response[:response] end end |
#to_hash ⇒ Object
33 34 35 |
# File 'lib/haveapi/client/response.rb', line 33 def to_hash response end |
#wait_for_completion ⇒ Object
Block until the action is completed or timeout occurs. If the block is given, it is regularly called with the action’s state.
64 65 66 67 68 69 |
# File 'lib/haveapi/client/response.rb', line 64 def wait_for_completion(...) id = [:action_state_id] return nil unless id HaveAPI::Client::Action.wait_for_completion(@action.client, id, ...) end |