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

#initialize

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants

Methods included from Parallelism

#inherited, #supports_parallel?

Methods inherited from Middleware

dependency, inherited, #initialize, loaded?, new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware

Constructor Details

This class inherits a constructor from Faraday::Adapter

Instance Method Details

#call(env) ⇒ Object


6
7
8
9
10
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
44
45
46
47
48
49
50
51
52
# File 'lib/faraday/adapter/patron.rb', line 6

def call(env)
  super  # TODO: support streaming requests

  env[:body] = env[:body].read if env[:body].respond_to? :read

  session = ::Patron::Session.new
  @config_block.call(session) if @config_block
  configure_ssl(session, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]

  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]
      proxy_uri = proxy[:uri].dup
      proxy_uri.user = proxy[:user] && Utils.escape(proxy[:user]).gsub('+', '%20')
      proxy_uri.password = proxy[:password] && Utils.escape(proxy[:password]).gsub('+', '%20')
      session.proxy = proxy_uri.to_s
    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, ::Patron::ConnectionFailed
    raise Error::ConnectionFailed, $!
  end

  # Remove the "HTTP/1.1 200", leaving just the reason phrase
  reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')

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

  @app.call env
rescue ::Patron::TimeoutError => err
  if connection_timed_out_message?(err.message)
    raise Faraday::Error::ConnectionFailed, err
  else
    raise Faraday::Error::TimeoutError, err
  end
rescue ::Patron::Error => err
  if err.message.include?("code 407")
    raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
  else
    raise Error::ConnectionFailed, err
  end
end

#configure_ssl(session, ssl) ⇒ Object


69
70
71
72
73
74
75
# File 'lib/faraday/adapter/patron.rb', line 69

def configure_ssl(session, ssl)
  if ssl.fetch(:verify, true)
    session.cacert = ssl[:ca_file]
  else
    session.insecure = true
  end
end