Sha256: 79cc735c1124a11d81e74189b83a466b9c48898b0b52e0607f5c9cc323379428

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

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

    def has_next_token?
      !!next_token
    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

1 entries across 1 versions & 1 rubygems

Version Path
peddler-0.6.3 lib/peddler/request.rb