Class: Faraday::Adapter::Patron

Inherits:
Faraday::Adapter show all
Defined in:
lib/faraday/adapter/patron.rb

Constant Summary

Constants inherited from Faraday::Adapter

CONTENT_LENGTH

Instance Attribute Summary

Attributes included from Parallelism

#supports_parallel

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

adapter?, #prepend_proxy_auth_string, #save_response

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants

Methods included from MiddlewareRegistry

#lookup_middleware, #register_middleware

Methods included from Parallelism

#inherited, #supports_parallel?

Methods inherited from Middleware

adapter?, dependency, inherited, loaded?, new

Constructor Details

#initialize(app, &block) ⇒ Patron

Returns a new instance of Patron.



6
7
8
9
# File 'lib/faraday/adapter/patron.rb', line 6

def initialize(app, &block)
  super(app)
  @block = block if block_given?
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/faraday/adapter/patron.rb', line 11

def call(env)
  super

  # TODO: support streaming requests
  env[:body] = env[:body].read if env[:body].respond_to? :read

  session = @session ||= create_session

  if req = env[:request]
    session.timeout = session.connect_timeout = req[:timeout] if req[:timeout]
    session.connect_timeout = req[:open_timeout]              if req[:open_timeout]
              
    if proxy = req[:proxy]
      session.proxy = proxy[:uri].to_s
      if proxy[:user] && proxy[:password]
        prepend_proxy_auth_string(proxy, session)
      end
    end
  end

  response = begin
    data = env[:body] ? env[:body].to_s : nil
    session.request(env[:method], env[:url].to_s, env[:request_headers], :data => data)
  rescue Errno::ECONNREFUSED
    raise Error::ConnectionFailed, $!
  end

  save_response(env, response.status, response.body, response.headers)

  @app.call env
rescue ::Patron::TimeoutError => err
  raise Faraday::Error::TimeoutError, err
end

#create_sessionObject



54
55
56
57
58
# File 'lib/faraday/adapter/patron.rb', line 54

def create_session
  session = ::Patron::Session.new
  @block.call(session) if @block
  session
end