From d9a1be15e91f302e1cc3193dfce94582eaae7c34 Mon Sep 17 00:00:00 2001 From: rick Date: Sun, 13 Feb 2011 14:27:41 -0800 Subject: [PATCH] experimental excon support --- lib/faraday/adapter.rb | 4 +++- lib/faraday/adapter/excon.rb | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 lib/faraday/adapter/excon.rb diff --git a/lib/faraday/adapter.rb b/lib/faraday/adapter.rb index a2ece1b9..03331f8a 100644 --- a/lib/faraday/adapter.rb +++ b/lib/faraday/adapter.rb @@ -12,6 +12,7 @@ module Faraday :Typhoeus => 'typhoeus', :EMSynchrony => 'em_synchrony', :Patron => 'patron', + :Excon => 'excon', :Test => 'test' register_lookup_modules \ @@ -20,7 +21,8 @@ module Faraday :net_http => :NetHttp, :typhoeus => :Typhoeus, :patron => :Patron, - :em_synchrony => :EMSynchrony + :em_synchrony => :EMSynchrony, + :excon => :Excon def call(env) process_body_for_request(env) diff --git a/lib/faraday/adapter/excon.rb b/lib/faraday/adapter/excon.rb new file mode 100644 index 00000000..dbcbfd8e --- /dev/null +++ b/lib/faraday/adapter/excon.rb @@ -0,0 +1,41 @@ +module Faraday + class Adapter + class Excon < Faraday::Adapter + begin + require 'excon' + rescue LoadError, NameError => e + self.load_error = e + end + + def call(env) + super + + conn = ::Excon.new(env[:url].to_s) + if ssl = (env[:url].scheme == 'https' && env[:ssl]) + ::Excon.ssl_verify_peer = !!ssl[:verify] if ssl.key?(:verify) + if ca_file = ssl[:ca_file] + ::Excon.ssl_ca_path = ca_file + end + end + + resp = conn.request \ + :method => env[:method].to_s.upcase, + :headers => env[:request_headers], + :body => env[:body] + + env.update \ + :status => resp.status.to_i, + :response_headers => {}, + :body => resp.body + + resp.headers.each do |key, value| + env[:response_headers][key.downcase] = value + end + + @app.call env + rescue ::Excon::Errors::SocketError => e + raise Error::ConnectionFailed.new(e) + end + end + end +end