Class: Alice::Adapter::Patron

Inherits:
Middleware show all
Defined in:
lib/alice/adapter/patron.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


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/alice/adapter/patron.rb', line 10

def call(env)
  process_body_for_request(env)

  sess = ::Patron::Session.new
  args = [env[:method], env[:url].to_s, env[:request_headers]]
  if Alice::Connection::METHODS_WITH_BODIES.include?(env[:method])
    args.insert(2, env[:body].to_s)
  end
  resp = sess.send *args

  raise Alice::Error::ResourceNotFound if resp.status == 404

  env.update \
    :status           => resp.status,
    :response_headers => resp.headers.
      inject({}) { |memo, (k, v)| memo.update(k.downcase => v) },
    :body             => resp.body
  env[:response].finish(env)

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