lib/riak/client/excon_backend.rb in riak-client-1.0.2 vs lib/riak/client/excon_backend.rb in riak-client-1.0.3

- old
+ new

@@ -10,12 +10,13 @@ class ExconBackend < HTTPBackend def self.configured? begin require 'excon' Client::NETWORK_ERRORS << Excon::Errors::SocketError + Client::NETWORK_ERRORS << Excon::Errors::TimeoutError if defined? Excon::Errors::TimeoutError Client::NETWORK_ERRORS.uniq! - Gem::Version.new(Excon::VERSION) >= Gem::Version.new("0.5.7") && patch_excon + minimum_version?("0.5.7") && handle_deprecations && patch_excon rescue LoadError false end end @@ -32,10 +33,47 @@ end end @@patched = true end + # Defines instance methods that handle changes in the Excon API + # across different versions. + def self.handle_deprecations + # Define #make_request + if minimum_version?("0.10.2") + def make_request(params, block) + params[:response_block] = block if block + connection.request(params) + end + else + def make_request(params, block) + response = connection.request(params, &block) + end + end + + # Define #configure_ssl + if minimum_version?("0.9.6") + def configure_ssl + Excon.defaults[:ssl_verify_peer] = (@node.ssl_options[:verify_mode].to_s === "peer") + Excon.defaults[:ssl_ca_path] = @node.ssl_options[:ca_path] if @node.ssl_options[:ca_path] + end + else + def configure_ssl + Excon.ssl_verify_peer = (@node.ssl_options[:verify_mode].to_s === "peer") + Excon.ssl_ca_path = @node.ssl_options[:ca_path] if @node.ssl_options[:ca_path] + end + end + private :make_request, :configure_ssl + end + + # Returns true if the Excon library is at least the given + # version. This is used inside the backend to check how to + # provide certain request and configuration options. + def self.minimum_version?(version) + Gem::Version.new(Excon::VERSION) >= Gem::Version.new(version) + end + def teardown connection.reset end private @@ -52,11 +90,11 @@ params[:idempotent] = (method != :post) # Later versions of Excon pass multiple arguments to the block block = lambda {|*args| yield args.first } if block_given? - response = connection.request(params, &block) + response = make_request(params, block) response_headers.initialize_http_header(response.headers) if valid_response?(expect, response.status) result = {:headers => response_headers.to_hash, :code => response.status} if return_body?(method, response.status, block_given?) @@ -68,14 +106,9 @@ end end def connection @connection ||= Excon::Connection.new(root_uri.to_s) - end - - def configure_ssl - Excon.ssl_verify_peer = @node.ssl_options[:verify_mode].to_s === "peer" - Excon.ssl_ca_path = @node.ssl_options[:ca_path] if @node.ssl_options[:ca_path] end end end end