Sha256: c93c03631356385a6af45b3f0886af3ea44a44a73b579a6ba5b149c6678100db

Contents?: true

Size: 1.55 KB

Versions: 4

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.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

4 entries across 4 versions & 1 rubygems

Version Path
haveapi-client-0.9.0 lib/haveapi/client/response.rb
haveapi-client-0.8.0 lib/haveapi/client/response.rb
haveapi-client-0.7.1 lib/haveapi/client/response.rb
haveapi-client-0.7.0 lib/haveapi/client/response.rb