Class: HTTPI::Adapter::NetHTTP

Inherits:
Base
  • Object
show all
Defined in:
lib/httpi/adapter/net_http.rb

Overview

HTTPI::Adapter::NetHTTP

Adapter for the Net::HTTP client. ruby-doc.org/stdlib/libdoc/net/http/rdoc/

Direct Known Subclasses

NetHTTPPersistent

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

register

Constructor Details

#initialize(request) ⇒ NetHTTP

Returns a new instance of NetHTTP.



18
19
20
21
22
# File 'lib/httpi/adapter/net_http.rb', line 18

def initialize(request)
  check_net_ntlm_version! if request.auth.ntlm?
  @request = request
  @client = create_client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



24
25
26
# File 'lib/httpi/adapter/net_http.rb', line 24

def client
  @client
end

Instance Method Details

#request(method) ⇒ Object

Executes arbitrary HTTP requests.

See Also:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/httpi/adapter/net_http.rb', line 28

def request(method)
  # Determine if Net::HTTP supports the method using reflection
  unless Net::HTTP.const_defined?(:"#{method.to_s.capitalize}") &&
      Net::HTTP.const_get(:"#{method.to_s.capitalize}").class == Class

    raise NotSupportedError, "Net::HTTP does not support "\
      "#{method.to_s.upcase}"
  end
  do_request(method) do |http, http_request|
    http_request.body = @request.body
    if @request.on_body then
      perform(http, http_request) do |res|
        res.read_body do |seg|
          @request.on_body.call(seg)
        end
      end
    else
      perform(http, http_request)
    end
  end
rescue OpenSSL::SSL::SSLError
  raise SSLError
rescue Errno::ECONNREFUSED   # connection refused
  $!.extend ConnectionError
  raise
end