Class: HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/etzetera/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#request(method, uri, options = {}) ⇒ Object

Make an HTTP request



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/etzetera/core_ext.rb', line 14

def request(method, uri, options = {})
  opts = @default_options.merge(options)
  host = URI.parse(uri).host
  opts.headers["Host"] = host
  headers = opts.headers
  proxy = opts.proxy

  method_body = body(opts, headers)
  if opts.params && !opts.params.empty?
    uri="#{uri}?#{URI.encode_www_form(opts.params)}"
  end

  request = HTTP::Request.new method, uri, headers, proxy, method_body
  if opts.follow
    code = 302
    while code == 302 or code == 301
      # if the uri isn't fully formed complete it
      if not uri.match(/\./)
        uri = "#{method}://#{host}#{uri}"
      end
      host = URI.parse(uri).host
      opts.headers["Host"] = host
      method_body = body(opts, headers)
      request = HTTP::Request.new method, uri, headers, proxy, method_body
      response = perform request, opts
      code = response.code
      uri = response.headers["Location"]
    end
  end

  opts.callbacks[:request].each { |c| c.call(request) }
  response = perform request, opts
  opts.callbacks[:response].each { |c| c.call(response) }

  format_response method, response, opts.response
end