Class: Weary::Adapter::NetHttp

Inherits:
Object
  • Object
show all
Extended by:
Weary::Adapter
Includes:
Weary::Adapter
Defined in:
lib/weary/adapters/net_http.rb

Constant Summary

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Weary::Adapter

call, initialize, normalize_request_headers, normalize_response

Class Method Details

.connect(request) ⇒ Object



11
12
13
14
15
# File 'lib/weary/adapters/net_http.rb', line 11

def connect(request)
  connection = socket(request)
  response = connection.request prepare(request)
  Rack::Response.new response.body || "", response.code, normalize_response(response.to_hash)
end

.prepare(request) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/weary/adapters/net_http.rb', line 17

def prepare(request)
  req_class = request_class(request.request_method)
  net_http_req = req_class.new(request.fullpath, normalize_request_headers(request.env))
  if net_http_req.request_body_permitted? # What's the best way of passing the body?
    net_http_req.body = request.body.read
    request.body.rewind
  end
  net_http_req.content_type = request.content_type if request.content_type
  net_http_req.content_length = request.content_length if request.content_length
  net_http_req
end

.request_class(method) ⇒ Object



36
37
38
39
# File 'lib/weary/adapters/net_http.rb', line 36

def request_class(method)
  capitalized = method.capitalize
  Net::HTTP.const_get capitalized
end

.socket(request) ⇒ Object



29
30
31
32
33
34
# File 'lib/weary/adapters/net_http.rb', line 29

def socket(request)
  connection = Net::HTTP.new request.host, request.port.to_s
  connection.use_ssl = request.scheme == 'https'
  connection.verify_mode = OpenSSL::SSL::VERIFY_NONE if connection.use_ssl?
  connection
end

Instance Method Details

#connect(rack_request) ⇒ Object



44
45
46
# File 'lib/weary/adapters/net_http.rb', line 44

def connect(rack_request)
  self.class.connect(rack_request)
end