Class: ActiveMerchant::Connection

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

Constant Summary collapse

MAX_RETRIES =
3
OPEN_TIMEOUT =
60
READ_TIMEOUT =
60
VERIFY_PEER =
true
CA_FILE =
File.expand_path('../certs/cacert.pem', File.dirname(__FILE__))
CA_PATH =
nil
MIN_VERSION =
:TLS1_1
RETRY_SAFE =
false
RUBY_184_POST_HEADERS =
{ 'Content-Type' => 'application/x-www-form-urlencoded' }

Constants included from NetworkConnectionRetries

NetworkConnectionRetries::DEFAULT_CONNECTION_ERRORS, NetworkConnectionRetries::DEFAULT_RETRIES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NetworkConnectionRetries

included, log, #retry_exceptions

Constructor Details

#initialize(endpoint) ⇒ Connection

Returns a new instance of Connection.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_merchant/connection.rb', line 29

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
  @ca_file      = CA_FILE
  @ca_path      = CA_PATH
  @max_retries  = MAX_RETRIES
  @ignore_http_status = false
  @ssl_version = nil
  if Net::HTTP.instance_methods.include?(:min_version=)
    @min_version = MIN_VERSION
    @max_version = nil
  end
  @ssl_connection = {}
  @proxy_address = :ENV
  @proxy_port = nil
  @proxy_user = nil
  @proxy_password = nil
end

Instance Attribute Details

#ca_fileObject

Returns the value of attribute ca_file.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def ca_file
  @ca_file
end

#ca_pathObject

Returns the value of attribute ca_path.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def ca_path
  @ca_path
end

#endpointObject

Returns the value of attribute endpoint.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def endpoint
  @endpoint
end

#ignore_http_statusObject

Returns the value of attribute ignore_http_status.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def ignore_http_status
  @ignore_http_status
end

#loggerObject

Returns the value of attribute logger.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def logger
  @logger
end

#max_retriesObject

Returns the value of attribute max_retries.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def max_retries
  @max_retries
end

#max_versionObject

Returns the value of attribute max_version.



25
26
27
# File 'lib/active_merchant/connection.rb', line 25

def max_version
  @max_version
end

#min_versionObject

Returns the value of attribute min_version.



24
25
26
# File 'lib/active_merchant/connection.rb', line 24

def min_version
  @min_version
end

#open_timeoutObject

Returns the value of attribute open_timeout.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def open_timeout
  @open_timeout
end

#pemObject

Returns the value of attribute pem.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def pem
  @pem
end

#pem_passwordObject

Returns the value of attribute pem_password.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def pem_password
  @pem_password
end

#proxy_addressObject

Returns the value of attribute proxy_address.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def proxy_address
  @proxy_address
end

#proxy_passwordObject

Returns the value of attribute proxy_password.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def proxy_password
  @proxy_password
end

#proxy_portObject

Returns the value of attribute proxy_port.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def proxy_port
  @proxy_port
end

#proxy_userObject

Returns the value of attribute proxy_user.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def proxy_user
  @proxy_user
end

#read_timeoutObject

Returns the value of attribute read_timeout.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def read_timeout
  @read_timeout
end

#ssl_connectionObject (readonly)

Returns the value of attribute ssl_connection.



27
28
29
# File 'lib/active_merchant/connection.rb', line 27

def ssl_connection
  @ssl_connection
end

#ssl_versionObject

Returns the value of attribute ssl_version.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def ssl_version
  @ssl_version
end

#tagObject

Returns the value of attribute tag.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def tag
  @tag
end

#verify_peerObject

Returns the value of attribute verify_peer.



21
22
23
# File 'lib/active_merchant/connection.rb', line 21

def verify_peer
  @verify_peer
end

#wiredump_deviceObject

Returns the value of attribute wiredump_device.



27
28
29
# File 'lib/active_merchant/connection.rb', line 27

def wiredump_device
  @wiredump_device
end

Instance Method Details

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



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_merchant/connection.rb', line 57

def request(method, body, headers = {})
  request_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  headers = headers.dup
  headers['connection'] ||= 'close'

  retry_exceptions(max_retries: max_retries, logger: logger, tag: tag) do
    info "connection_http_method=#{method.to_s.upcase} connection_uri=#{endpoint}", tag

    result = nil

    realtime = Benchmark.realtime do
      http.start unless http.started?
      @ssl_connection = http.ssl_connection
      info "connection_ssl_version=#{ssl_connection[:version]} connection_ssl_cipher=#{ssl_connection[:cipher]}", tag

      result =
        case method
        when :get
          http.get(endpoint.request_uri, headers)
        when :post
          debug body
          http.post(endpoint.request_uri, body, RUBY_184_POST_HEADERS.merge(headers))
        when :put
          debug body
          http.put(endpoint.request_uri, body, headers)
        when :patch
          debug body
          http.patch(endpoint.request_uri, body, headers)
        when :delete
          # It's kind of ambiguous whether the RFC allows bodies
          # for DELETE requests. But Net::HTTP's delete method
          # very unambiguously does not.
          if body
            debug body
            req = Net::HTTP::Delete.new(endpoint.request_uri, headers)
            req.body = body
            http.request(req)
          else
            http.delete(endpoint.request_uri, headers)
          end
        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
    debug result.body
    result
  end
ensure
  info 'connection_request_total_time=%.4fs' % [Process.clock_gettime(Process::CLOCK_MONOTONIC) - request_start], tag
  http.finish if http.started?
end