Permalink
Browse files

Apply @myronmarston's suggestion and define methods instead of checki…

…ng library version frequently.
  • Loading branch information...
1 parent acee3ab commit b6744f99490d2d0b39476a8a92aa9246781cf325 @seancribbs seancribbs committed Apr 11, 2012
Showing with 33 additions and 17 deletions.
  1. +33 −17 lib/riak/client/excon_backend.rb
@@ -12,8 +12,9 @@ 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!
- minimum_version?("0.5.7") && patch_excon
+ minimum_version?("0.5.7") && handle_deprecations && patch_excon
rescue LoadError
false
end
@@ -34,6 +35,36 @@ def sockets
@@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.
@@ -61,12 +92,7 @@ def perform(method, uri, headers, expect, data=nil, &block)
# Later versions of Excon pass multiple arguments to the block
block = lambda {|*args| yield args.first } if block_given?
- if self.class.minimum_version?("0.10.2")
- params[:response_block] = block if block
- response = connection.request(params)
- else
- response = connection.request(params, &block)
- end
+ response = make_request(params, block)
response_headers.initialize_http_header(response.headers)
if valid_response?(expect, response.status)
@@ -83,16 +109,6 @@ def perform(method, uri, headers, expect, data=nil, &block)
def connection
@connection ||= Excon::Connection.new(root_uri.to_s)
end
-
- def configure_ssl
- if self.class.minimum_version?("0.9.6")
- 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]
- else
- 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
end

0 comments on commit b6744f9

Please sign in to comment.