<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>lib/active_merchant/lib/connection.rb</filename>
    </added>
    <added>
      <filename>test/unit/connection_test.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,6 @@
 = ActiveMerchant CHANGELOG
 
+* Refactor ActiveMerchant::Connection out of the PostsData module. Add support for logging and wiredumping requests [cody]
 * Assume a valid load path when running tests [cody]
 * Use SHIPTOSTREET2 element instead of STREET2 element for Payflow Express Uk address [cody]
 * Clean up the test helper [cody]</diff>
      <filename>CHANGELOG</filename>
    </modified>
    <modified>
      <diff>@@ -1,3 +1,4 @@
 require 'active_merchant'
 require 'active_merchant/billing/integrations/action_view_helper'
 ActionView::Base.send(:include, ActiveMerchant::Billing::Integrations::ActionViewHelper)
+ActiveMerchant::Billing::Gateway.logger = Rails.logger
\ No newline at end of file</diff>
      <filename>init.rb</filename>
    </modified>
    <modified>
      <diff>@@ -44,11 +44,12 @@ require 'cgi'
 require 'active_merchant/lib/utils'
 require 'active_merchant/lib/error'
 require 'active_merchant/lib/validateable'
+require 'active_merchant/lib/connection'
 require 'active_merchant/lib/posts_data'
 require 'active_merchant/lib/post_data'
 require 'active_merchant/lib/requires_parameters'
-
 require 'active_merchant/lib/country'
+
 require 'active_merchant/billing/avs_result'
 require 'active_merchant/billing/cvv_result'
 require 'active_merchant/billing/credit_card_methods'</diff>
      <filename>lib/active_merchant.rb</filename>
    </modified>
    <modified>
      <diff>@@ -130,9 +130,6 @@ module ActiveMerchant #:nodoc:
         self.class.name.scan(/\:\:(\w+)Gateway/).flatten.first
       end
       
-      # Return a String with the amount in the appropriate format
-      #--
-      # TODO Refactor this method. It's a tad on the ugly side.
       def amount(money)
         return nil if money.nil?
         cents = money.respond_to?(:cents) ? money.cents : money 
@@ -148,7 +145,6 @@ module ActiveMerchant #:nodoc:
         end
       end
       
-      # Ascertains the currency to be used on the money supplied.
       def currency(money)
         money.respond_to?(:currency) ? money.currency : self.default_currency
       end</diff>
      <filename>lib/active_merchant/billing/gateway.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,108 +1,46 @@
 module ActiveMerchant #:nodoc:
-  class ConnectionError &lt; ActiveMerchantError
-  end
-  
-  class RetriableConnectionError &lt; ConnectionError
-  end
-  
   module PostsData  #:nodoc:
-    MAX_RETRIES = 3
-    OPEN_TIMEOUT = 60
-    READ_TIMEOUT = 60
-    
+
     def self.included(base)
       base.superclass_delegating_accessor :ssl_strict
       base.ssl_strict = true
       
-      base.class_inheritable_accessor :pem_password
-      base.pem_password = false
-      
       base.class_inheritable_accessor :retry_safe
       base.retry_safe = false
 
       base.superclass_delegating_accessor :open_timeout
-      base.open_timeout = OPEN_TIMEOUT
+      base.open_timeout = 60
 
       base.superclass_delegating_accessor :read_timeout
-      base.read_timeout = READ_TIMEOUT
+      base.read_timeout = 60
+      
+      base.superclass_delegating_accessor :logger
+      base.superclass_delegating_accessor :wiredump_device
     end
     
-    def ssl_get(url, headers={})
-      ssl_request(:get, url, nil, headers)
+    def ssl_get(endpoint, headers={})
+      ssl_request(:get, endpoint, nil, headers)
     end
     
-    def ssl_post(url, data, headers = {})
-      ssl_request(:post, url, data, headers)
+    def ssl_post(endpoint, data, headers = {})
+      ssl_request(:post, endpoint, data, headers)
     end
     
     private
-    def retry_exceptions
-      retries = MAX_RETRIES
-      begin
-        yield
-      rescue RetriableConnectionError =&gt; e
-        retries -= 1
-        retry unless retries.zero?
-        raise ConnectionError, e.message
-      rescue ConnectionError
-        retries -= 1
-        retry if retry_safe &amp;&amp; !retries.zero?
-        raise
-      end
-    end
-    
-    def ssl_request(method, url, data, headers = {})
-      if method == :post
-        # Ruby 1.8.4 doesn't automatically set this header
-        headers['Content-Type'] ||= &quot;application/x-www-form-urlencoded&quot;
-      end
+    def ssl_request(method, endpoint, data, headers = {})
+      connection = Connection.new(endpoint)
+      connection.open_timeout = open_timeout
+      connection.read_timeout = read_timeout
+      connection.retry_safe   = retry_safe
+      connection.verify_peer  = ssl_strict
+      connection.logger       = logger
+      connection.tag          = self.class.name
+      connection.wiredump_device = wiredump_device
       
-      uri   = URI.parse(url)
-
-      http = Net::HTTP.new(uri.host, uri.port)
-      http.open_timeout = self.class.open_timeout
-      http.read_timeout = self.class.read_timeout
+      connection.pem          = @options[:pem] if @options
+      connection.pem_password = @options[:pem_password] if @options
       
-      if uri.scheme == &quot;https&quot;
-        http.use_ssl = true
-        
-        if ssl_strict
-          http.verify_mode    = OpenSSL::SSL::VERIFY_PEER
-          http.ca_file        = File.dirname(__FILE__) + '/../../certs/cacert.pem'
-        else
-          http.verify_mode    = OpenSSL::SSL::VERIFY_NONE
-        end
-      
-        if @options &amp;&amp; !@options[:pem].blank?
-          http.cert           = OpenSSL::X509::Certificate.new(@options[:pem])
-        
-          if pem_password
-            raise ArgumentError, &quot;The private key requires a password&quot; if @options[:pem_password].blank?
-            http.key            = OpenSSL::PKey::RSA.new(@options[:pem], @options[:pem_password])
-          else
-            http.key            = OpenSSL::PKey::RSA.new(@options[:pem])
-          end
-        end
-      end
-
-      retry_exceptions do 
-        begin
-          case method
-          when :get
-            http.get(uri.request_uri, headers).body
-          when :post
-            http.post(uri.request_uri, data, headers).body
-          end
-        rescue EOFError =&gt; e
-          raise ConnectionError, &quot;The remote server dropped the connection&quot;
-        rescue Errno::ECONNRESET =&gt; e
-          raise ConnectionError, &quot;The remote server reset the connection&quot;
-        rescue Errno::ECONNREFUSED =&gt; e
-          raise RetriableConnectionError, &quot;The remote server refused the connection&quot;
-        rescue Timeout::Error, Errno::ETIMEDOUT =&gt; e
-          raise ConnectionError, &quot;The connection to the remote server timed out&quot;
-        end
-      end
+      connection.request(method, data, headers)
     end
     
   end</diff>
      <filename>lib/active_merchant/lib/posts_data.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,14 +1,9 @@
 require 'test_helper'
 
-class MockResponse
-  def body
-  end
-end
-
 class PostsDataTests &lt; Test::Unit::TestCase
-  URL = 'http://example.com'
-  
+
   def setup
+    @url = 'http://example.com'
     @gateway = SimpleTestGateway.new
   end
   
@@ -17,69 +12,22 @@ class PostsDataTests &lt; Test::Unit::TestCase
   end
   
   def test_single_successful_post
-    Net::HTTP.any_instance.expects(:post).once.returns(MockResponse.new)
+    ActiveMerchant::Connection.any_instance.expects(:request).returns('')
     
     assert_nothing_raised do
-      @gateway.ssl_post(URL, '') 
+      @gateway.ssl_post(@url, '') 
     end
   end
   
   def test_multiple_successful_posts
-    responses = [ MockResponse.new, MockResponse.new ]
-    Net::HTTP.any_instance.expects(:post).times(2).returns(*responses)
-    
-    assert_nothing_raised do
-      @gateway.ssl_post(URL, '')
-      @gateway.ssl_post(URL, '') 
-    end
-  end
-  
-  def test_unrecoverable_exception
-    Net::HTTP.any_instance.expects(:post).raises(EOFError)
-    
-    assert_raises(ActiveMerchant::ConnectionError) do
-      @gateway.ssl_post(URL, '') 
-    end
-  end
-  
-  def test_failure_then_success_with_recoverable_exception
-    Net::HTTP.any_instance.expects(:post).times(2).raises(Errno::ECONNREFUSED).then.returns(MockResponse.new)
+    ActiveMerchant::Connection.any_instance.expects(:request).times(2).returns('', '')
     
     assert_nothing_raised do
-      @gateway.ssl_post(URL, '')
+      @gateway.ssl_post(@url, '')
+      @gateway.ssl_post(@url, '') 
     end
   end
-  
-  def test_failure_limit_reached
-    Net::HTTP.any_instance.expects(:post).times(ActiveMerchant::PostsData::MAX_RETRIES).raises(Errno::ECONNREFUSED)
     
-    assert_raises(ActiveMerchant::ConnectionError) do 
-      @gateway.ssl_post(URL, '')
-    end
-  end
-  
-  def test_failure_then_success_with_retry_safe_enabled
-    Net::HTTP.any_instance.expects(:post).times(2).raises(EOFError).then.returns(MockResponse.new)
-    
-    @gateway.retry_safe = true
-    
-    assert_nothing_raised do
-      @gateway.ssl_post(URL, '')
-    end
-  end
-  
-  def test_mixture_of_failures_with_retry_safe_enabled
-    Net::HTTP.any_instance.expects(:post).times(3).raises(Errno::ECONNRESET).
-                                                   raises(Errno::ECONNREFUSED).
-                                                   raises(EOFError)
-      
-    @gateway.retry_safe = true
-                                                     
-    assert_raises(ActiveMerchant::ConnectionError) do  
-      @gateway.ssl_post(URL, '')
-    end
-  end
-  
   def test_setting_ssl_strict_outside_class_definition
     assert_equal SimpleTestGateway.ssl_strict, SubclassGateway.ssl_strict
     SimpleTestGateway.ssl_strict = !SimpleTestGateway.ssl_strict
@@ -87,14 +35,14 @@ class PostsDataTests &lt; Test::Unit::TestCase
   end
 
   def test_setting_timeouts
-    @gateway.class.open_timeout=50
-    @gateway.class.read_timeout=37
-    Net::HTTP.any_instance.expects(:post).once.returns(MockResponse.new)
-    Net::HTTP.any_instance.expects(:open_timeout=).with(50)
-    Net::HTTP.any_instance.expects(:read_timeout=).with(37)
+    @gateway.class.open_timeout = 50
+    @gateway.class.read_timeout = 37
+    ActiveMerchant::Connection.any_instance.expects(:request).returns('')
+    ActiveMerchant::Connection.any_instance.expects(:open_timeout=).with(50)
+    ActiveMerchant::Connection.any_instance.expects(:read_timeout=).with(37)
 
     assert_nothing_raised do
-      @gateway.ssl_post(URL, '')
+      @gateway.ssl_post(@url, '')
     end
   end
 end
\ No newline at end of file</diff>
      <filename>test/unit/posts_data_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>3de7c1f244744ec5b416a33cf877677592ec931e</id>
    </parent>
  </parents>
  <author>
    <name>Cody Fauser</name>
    <email>codyfauser@gmail.com</email>
  </author>
  <url>http://github.com/elevation/active_merchant/commit/462e635eca4e63b7e4e91b3f6fbc65348ab9fbd0</url>
  <id>462e635eca4e63b7e4e91b3f6fbc65348ab9fbd0</id>
  <committed-date>2009-05-09T21:03:00-07:00</committed-date>
  <authored-date>2009-05-09T21:03:00-07:00</authored-date>
  <message>Refactor ActiveMerchant::Connection out of the PostsData module. Add support for logging and wiredumping requests</message>
  <tree>7d7dd391e7de36187fb86dc7744c633dc1b65c69</tree>
  <committer>
    <name>Cody Fauser</name>
    <email>codyfauser@gmail.com</email>
  </committer>
</commit>
