Class: Resourceful::NetHttpAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/resourceful/net_http_adapter.rb

Instance Method Summary collapse

Instance Method Details

#make_request(method, uri, body = nil, header = nil) ⇒ Object

Make an HTTP request using the standard library net/http.

Will use a proxy defined in the http_proxy environment variable, if set.

Parameters:

  • body (#read) (defaults to: nil)

    An IO-ish thing containing the body of the request



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/resourceful/net_http_adapter.rb', line 30

def make_request(method, uri, body = nil, header = nil)
  uri = uri.is_a?(Addressable::URI) ? uri : Addressable::URI.parse(uri)

  if [:put, :post].include? method
    body = body ? body.read : ""
    header[:content_length] = body.size 
  end

  req = net_http_request_class(method).new(uri.absolute_path)
  header.each_field { |k,v| req[k] = v } if header
  https = ("https" == uri.scheme)
  conn_class = proxy_details ? Net::HTTP.Proxy(*proxy_details) : Net::HTTP
  conn = conn_class.new(uri.host, uri.port || (https ? 443 : 80))
  conn.use_ssl = https
  begin 
    conn.start
    res = if body
            conn.request(req, body)
          else
            conn.request(req)
          end
  ensure
    conn.finish if conn.started?
  end

  [ Integer(res.code),
    Resourceful::Header.new(res.header.to_hash),
    res.body
  ]
ensure
  
end