Sha256: f4c406cb0317b21e1c1b4fc9fef6171daca7806f6f56bd046a6372f969baf004

Contents?: true

Size: 1.49 KB

Versions: 8

Compression:

Stored size: 1.49 KB

Contents

module Duracloud
  class Request
    attr_reader :client, :url, :http_method, :body, :headers, :query

    # @param client [Duracloud::Client] the client
    # @param http_method [Symbol] the lower-case symbol corresponding to HTTP method
    # @param url [String] relative or absolute URL
    # @param body [String] the body of the request
    # @param headers [Hash] HTTP headers
    # @param query [Hash] Query string parameters
    # def initialize(client, http_method, url, body: nil, headers: nil, query: nil)
    def initialize(client, http_method, url, **options)
      @client      = client
      @http_method = http_method
      @url         = url
      set_options(options.dup)
    end

    def execute(&block)
      response_class.new original_response(&block)
    end

    private

    def original_response(&block)
      connection.send(http_method,
                      url,
                      body: body,
                      query: query,
                      header: headers,
                      &block)
    end

    def set_options(options)
      @body    = options.delete(:body)
      @headers = options.delete(:headers)
      query    = options.delete(:query) || {}
      # Treat other keywords args as query params and ignore empty params
      @query = query.merge(options).reject { |k, v| v.to_s.empty? }
    end

    def base_path
      '/'
    end

    def response_class
      Response
    end

    def connection
      @connection ||= Connection.new(client, base_path)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
duracloud-client-0.9.0 lib/duracloud/request.rb
duracloud-client-0.8.0 lib/duracloud/request.rb
duracloud-client-0.7.2 lib/duracloud/request.rb
duracloud-client-0.7.1 lib/duracloud/request.rb
duracloud-client-0.7.0 lib/duracloud/request.rb
duracloud-client-0.6.0 lib/duracloud/request.rb
duracloud-client-0.5.0 lib/duracloud/request.rb
duracloud-client-0.4.0 lib/duracloud/request.rb