Class: Artifice::Net::HTTP

Inherits:
Net::HTTP
  • Object
show all
Defined in:
lib/artifice.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.endpointObject

Returns the value of attribute endpoint


55
56
57
# File 'lib/artifice.rb', line 55

def endpoint
  @endpoint
end

Instance Method Details

#connectObject

We don't need to connect, so blank out this method


64
65
# File 'lib/artifice.rb', line 64

def connect
end

#request(req, body = nil) {|Net::HTTPResponse| ... } ⇒ Net::HTTPResponse

Replace the Net::HTTP request method with a method that converts the request into a Rack request and dispatches it to the Rack endpoint.

Parameters:

  • req (Net::HTTPRequest)

    A Net::HTTPRequest object, or one if its subclasses

  • body (optional, String, #read) (defaults to: nil)

    This should be sent as “rack.input”. If it's a String, it will be converted to a StringIO.

Yields:

  • (Net::HTTPResponse)

    If a block is provided, this method will yield the Net::HTTPResponse to it after the body is read.

Returns:

  • (Net::HTTPResponse)

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/artifice.rb', line 81

def request(req, body = nil, &block)
  rack_request = RackRequest.new(self.class.endpoint)

  req.each_header do |header, value|
    rack_request.header(header, value)
  end

  scheme = use_ssl? ? "https" : "http"
  prefix = "#{scheme}://#{addr_port}"

  response = rack_request.request("#{prefix}#{req.path}",
    {:method => req.method, :input => body || req.body})

  make_net_http_response(response, &block)
end