Sha256: 792617f659dce42c2d51f6e7e3f7e305f63cccc6d294dfcd7a6c2f22f7f9345a

Contents?: true

Size: 992 Bytes

Versions: 3

Compression:

Stored size: 992 Bytes

Contents

require 'forwardable'

require 'peddler/request/parameters'
require 'peddler/response_wrapper'

module Peddler
  class Request
    extend Forwardable

    def_delegator :last_response, :next_token
    def_delegator :last_response, :xml_payload, :last_xml_payload

    attr :client, :headers

    attr_accessor :body, :last_response

    def initialize(client)
      @client = client
      @headers = {}
    end

    def parameters(action = nil)
      @parameters = Parameters.new(action) if action
      @parameters
    end

    def execute
      fetch
      parse
    end

    private

    def parser
      @parser ||= Object.const_get(self.class.name.sub('Request', 'Parser'))
    end

    def fetch
      opts = { query: parameters, headers: headers }
      opts.update(body: body) if body
      res = client.post(opts)

      @last_response = ResponseWrapper.new(res)
    end

    def parse
      parser.new(parser.handle?(:xml) ? last_xml_payload : last_response.body)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peddler-0.6.2 lib/peddler/request.rb
peddler-0.6.1 lib/peddler/request.rb
peddler-0.6.0 lib/peddler/request.rb