Class: ActiveMerchant::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_merchant/common/connection.rb

Constant Summary collapse

MAX_RETRIES =
3
OPEN_TIMEOUT =
60
READ_TIMEOUT =
60
VERIFY_PEER =
true
RETRY_SAFE =
false
RUBY_184_POST_HEADERS =
{ "Content-Type" => "application/x-www-form-urlencoded" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ Connection

Returns a new instance of Connection.



45
46
47
48
49
50
51
# File 'lib/active_merchant/common/connection.rb', line 45

def initialize(endpoint)
  @endpoint     = endpoint.is_a?(URI) ? endpoint : URI.parse(endpoint)
  @open_timeout = OPEN_TIMEOUT
  @read_timeout = READ_TIMEOUT
  @retry_safe   = RETRY_SAFE
  @verify_peer  = VERIFY_PEER
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



34
35
36
# File 'lib/active_merchant/common/connection.rb', line 34

def endpoint
  @endpoint
end

#loggerObject

Returns the value of attribute logger.



42
43
44
# File 'lib/active_merchant/common/connection.rb', line 42

def logger
  @logger
end

#open_timeoutObject

Returns the value of attribute open_timeout.



35
36
37
# File 'lib/active_merchant/common/connection.rb', line 35

def open_timeout
  @open_timeout
end

#pemObject

Returns the value of attribute pem.



39
40
41
# File 'lib/active_merchant/common/connection.rb', line 39

def pem
  @pem
end

#pem_passwordObject

Returns the value of attribute pem_password.



40
41
42
# File 'lib/active_merchant/common/connection.rb', line 40

def pem_password
  @pem_password
end

#read_timeoutObject

Returns the value of attribute read_timeout.



36
37
38
# File 'lib/active_merchant/common/connection.rb', line 36

def read_timeout
  @read_timeout
end

#retry_safeObject

Returns the value of attribute retry_safe.



38
39
40
# File 'lib/active_merchant/common/connection.rb', line 38

def retry_safe
  @retry_safe
end

#tagObject

Returns the value of attribute tag.



43
44
45
# File 'lib/active_merchant/common/connection.rb', line 43

def tag
  @tag
end

#verify_peerObject

Returns the value of attribute verify_peer.



37
38
39
# File 'lib/active_merchant/common/connection.rb', line 37

def verify_peer
  @verify_peer
end

#wiredump_deviceObject

Returns the value of attribute wiredump_device.



41
42
43
# File 'lib/active_merchant/common/connection.rb', line 41

def wiredump_device
  @wiredump_device
end

Instance Method Details

#request(method, body, headers = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_merchant/common/connection.rb', line 53

def request(method, body, headers = {})
  retry_exceptions do 
    begin
      info "#{method.to_s.upcase} #{endpoint}", tag

      result = nil
      
      realtime = Benchmark.realtime do
        result = case method
        when :get
          raise ArgumentError, "GET requests do not support a request body" if body
          http.get(endpoint.request_uri, headers)
        when :post
          debug body
          http.post(endpoint.request_uri, body, RUBY_184_POST_HEADERS.merge(headers))
        else
          raise ArgumentError, "Unsupported request method #{method.to_s.upcase}"
        end
      end
      
      info "--> %d %s (%d %.4fs)" % [result.code, result.message, result.body ? result.body.length : 0, realtime], tag
      response = handle_response(result)
      debug response
      response
    rescue EOFError => e
      raise ConnectionError, "The remote server dropped the connection"
    rescue Errno::ECONNRESET => e
      raise ConnectionError, "The remote server reset the connection"
    rescue Errno::ECONNREFUSED => e
      raise RetriableConnectionError, "The remote server refused the connection"
    rescue OpenSSL::X509::CertificateError => e
      raise ClientCertificateError, "The remote server did not accept the provided SSL certificate"
    rescue Timeout::Error, Errno::ETIMEDOUT => e
      raise ConnectionError, "The connection to the remote server timed out"
    end
  end
end