Class: HTTP::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client/methods.rb

Class Method Summary collapse

Class Method Details

.create_type(&native_request_factory) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/http_client/methods.rb', line 3

def self.create_type(&native_request_factory)
  Class.new do
    def initialize(path, params = {})
      @path = path
      @params = params
      @headers = {}
    end

    def add_headers(headers)
      @headers.merge!(headers)
    end

    define_method(:create_native_request) do |uri_builder, encoding|
      params = @params.collect { |key, value| BasicNameValuePair.new(key.to_s, value.to_s) }
      request = native_request_factory.call(uri_builder, @path, params, encoding)

      @headers.each { |name, value| request.add_header(name.to_s, value.to_s) }

      request
    end
  end
end