Class: HaveAPI::Client::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/client/response.rb

Overview

Represents a response from the API.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#actionObject (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

#eachObject

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

#errorsObject



41
42
43
# File 'lib/haveapi/client/response.rb', line 41

def errors
  @response[:errors]
end

#failed?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/haveapi/client/response.rb', line 16

def failed?
  !ok?
end

#messageObject



37
38
39
# File 'lib/haveapi/client/response.rb', line 37

def message
  @response[:message]
end

#metaObject



29
30
31
# File 'lib/haveapi/client/response.rb', line 29

def meta
  @response[:response][:_meta] # FIXME: read _meta from API description
end

#ok?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/haveapi/client/response.rb', line 12

def ok?
  @response[:status]
end

#responseObject



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_hashObject



33
34
35
# File 'lib/haveapi/client/response.rb', line 33

def to_hash
  response
end

#wait_for_completion(*args, &block) ⇒ Object

Block until the action is completed or timeout occurs. If the block is given, it is regularly called with the action’s state.

See Also:

  • Action#wait_for_completion


64
65
66
67
68
69
# File 'lib/haveapi/client/response.rb', line 64

def wait_for_completion(*args, &block)
  id = meta[:action_state_id]
  return nil unless id

  HaveAPI::Client::Action.wait_for_completion(@action.client, id, *args, &block)
end