Class: Faraday::Adapter::Patron

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

Overview

Patron adapter

Instance Method Summary collapse

Instance Method Details

#build_connection(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/faraday/adapter/patron.rb', line 7

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

  if (req = env[:request])
    configure_timeouts(session, req)
    configure_proxy(session, req[:proxy])
  end

  session
end

#call(env) ⇒ Object



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
53
54
55
56
57
58
59
60
# File 'lib/faraday/adapter/patron.rb', line 22

def call(env)
  super
  # TODO: support streaming requests
  env[:body] = env[:body].read if env[:body].respond_to? :read

  response = connection(env) do |session|
    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 Faraday::ConnectionFailed, $ERROR_INFO
  end

  if (req = env[:request]).stream_response?
    warn "Streaming downloads for #{self.class.name} " \
      'are not yet implemented.'
    req.on_data.call(response.body, response.body.bytesize)
  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 => e
  if connection_timed_out_message?(e.message)
    raise Faraday::ConnectionFailed, e
  end

  raise Faraday::TimeoutError, e
rescue ::Patron::Error => e
  if e.message.include?('code 407')
    raise Faraday::ConnectionFailed,
          %(407 "Proxy Authentication Required ")
  end

  raise Faraday::ConnectionFailed, e
end

#configure_proxy(session, proxy) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/faraday/adapter/patron.rb', line 97

def configure_proxy(session, proxy)
  return unless 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

#configure_ssl(session, ssl) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/faraday/adapter/patron.rb', line 77

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

#configure_timeouts(session, req) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/faraday/adapter/patron.rb', line 85

def configure_timeouts(session, req)
  return unless req

  if (sec = request_timeout(:read, req))
    session.timeout = sec
  end

  return unless (sec = request_timeout(:open, req))

  session.connect_timeout = sec
end