Sha256: 9cf4604dad9865f5288a3bc7f76b5b147b28e475fd24260cdb694ac302cbf9b2

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

# Represents a response from the API.
class HaveAPI::Client::Response
  attr_reader :action

  # Create instance.
  # +action+ being the called action and +response+ a received hash.
  def initialize(action, response)
    @action = action
    @response = response
  end

  def ok?
    @response[:status]
  end

  def failed?
    !ok?
  end

  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

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

  def to_hash
    response
  end

  def message
    @response[:message]
  end

  def errors
    @response[:errors]
  end

  # Access namespaced params directly.
  def [](key)
    return unless %i(object hash).include?(@action.output_layout.to_sym)

    @response[:response][@action.namespace(:output).to_sym][key]
  end

  # Iterate over namespaced items directly. Works for only for
  # object_list or hash_list.
  def each
    return unless %i(list).include?(@action.layout.to_sym)

    @response[:response][@action.namespace(:output).to_sym].each
  end

  # Block until the action is completed or timeout occurs. If the block is given,
  # it is regularly called with the action's state.
  #
  # @see HaveAPI::Client::Action#wait_for_completion
  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
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
haveapi-client-0.14.2 lib/haveapi/client/response.rb
haveapi-client-0.14.1 lib/haveapi/client/response.rb
haveapi-client-0.14.0 lib/haveapi/client/response.rb
haveapi-client-0.13.3 lib/haveapi/client/response.rb
haveapi-client-0.13.2 lib/haveapi/client/response.rb
haveapi-client-0.13.1 lib/haveapi/client/response.rb
haveapi-client-0.13.0 lib/haveapi/client/response.rb
haveapi-client-0.12.1 lib/haveapi/client/response.rb
haveapi-client-0.12.0 lib/haveapi/client/response.rb
haveapi-client-0.11.1 lib/haveapi/client/response.rb
haveapi-client-0.11.0 lib/haveapi/client/response.rb
haveapi-client-0.10.0 lib/haveapi/client/response.rb