Sha256: 8b5501f6600695d5fe1bea0cdb56c8d55a606b289f084b85d9a216ad3b4cb5ad

Contents?: true

Size: 1.56 KB

Versions: 15

Compression:

Stored size: 1.56 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(**kwargs, &block)
    id = meta[:action_state_id]
    return nil unless id

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
haveapi-client-0.20.0 lib/haveapi/client/response.rb
haveapi-client-0.19.3 lib/haveapi/client/response.rb
haveapi-client-0.19.2 lib/haveapi/client/response.rb
haveapi-client-0.19.1 lib/haveapi/client/response.rb
haveapi-client-0.19.0 lib/haveapi/client/response.rb
haveapi-client-0.18.2 lib/haveapi/client/response.rb
haveapi-client-0.18.1 lib/haveapi/client/response.rb
haveapi-client-0.18.0 lib/haveapi/client/response.rb
haveapi-client-0.17.0 lib/haveapi/client/response.rb
haveapi-client-0.16.3 lib/haveapi/client/response.rb
haveapi-client-0.16.2 lib/haveapi/client/response.rb
haveapi-client-0.16.1 lib/haveapi/client/response.rb
haveapi-client-0.16.0 lib/haveapi/client/response.rb
haveapi-client-0.15.1 lib/haveapi/client/response.rb
haveapi-client-0.15.0 lib/haveapi/client/response.rb