Sha256: 36357df083976af29951f691a0525408c39f38fbbf95e61feda5b75a3cc8c5c9

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Apidiesel

  # Wrapper for API requests
  class Request
    attr_accessor :action, :action_arguments, :parameters, :url, :response_body, :http_request, :http_response, :metadata, :result

    # @param [Apidiesel::Action] action
    # @param [Hash] action_arguments
    # @param [Hash] parameters
    # @param [Hash] metadata
    def initialize(action:, action_arguments:, parameters:, metadata: {})
      @action           = action
      @action_arguments = action_arguments
      @parameters       = parameters
      @metadata         = metadata
    end

    def response_body
      @response_body || http_response.try(:body)
    end

    def process_response
      # Reraise ResponseErrors to include ourselves. Not
      # pretty, but I can't think of anything nicer right now
      begin
        @result = action.process_response(response_body)
      rescue ResponseError => e
        raise ResponseError.new(e.message, self)
      end
    end

    def to_s
      [
        "Apidiesel::Request",
        action.http_method.to_s.upcase,
        url.try(:to_s),
        action.endpoint,
        parameters.collect { |key, value| "#{key}: #{value}"}.join(',')
      ].join(' ')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apidiesel-0.13 lib/apidiesel/request.rb