Class: Alice::Adapter::NetHttp

Inherits:
Middleware show all
Defined in:
lib/alice/adapter/net_http.rb

Instance Method Summary collapse

Methods inherited from Middleware

#create_form_params, #full_path_for, #initialize, loaded?, #process_body_for_request, setup_parallel_manager

Constructor Details

This class inherits a constructor from Alice::Middleware

Instance Method Details

#call(env) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/alice/adapter/net_http.rb', line 5

def call(env)
  process_body_for_request(env)

  http      = Net::HTTP.new(env[:url].host, env[:url].port)
  full_path = full_path_for(env[:url].path, env[:url].query, env[:url].fragment)
  http_resp = http.send_request(env[:method].to_s.upcase, full_path, env[:body], env[:request_headers])

  raise Error::ResourceNotFound if http_resp.code == '404'

  resp_headers = {}
  http_resp.each_header do |key, value|
    resp_headers[key] = value
  end

  env.update \
    :status           => http_resp.code.to_i, 
    :response_headers => resp_headers, 
    :body             => http_resp.body

  @app.call env
rescue Errno::ECONNREFUSED
  raise Error::ConnectionFailed, "connection refused"
end