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, #retry_exceptions

Constructor Details

#initialize(endpoint) ⇒ Connection

Returns a new instance of Connection.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_merchant/connection.rb', line 43

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
end

Instance Attribute Details

#ca_fileObject

Returns the value of attribute ca_file.



31
32
33
# File 'lib/active_merchant/connection.rb', line 31

def ca_file
  @ca_file
end

#ca_pathObject

Returns the value of attribute ca_path.



32
33
34
# File 'lib/active_merchant/connection.rb', line 32

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.



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

def ignore_http_status
  @ignore_http_status
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#max_retriesObject

Returns the value of attribute max_retries.



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

def max_retries
  @max_retries
end

#max_versionObject

Returns the value of attribute max_version.



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

def max_version
  @max_version
end

#min_versionObject

Returns the value of attribute min_version.



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

def min_version
  @min_version
end

#open_timeoutObject

Returns the value of attribute open_timeout.



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

def open_timeout
  @open_timeout
end

#pemObject

Returns the value of attribute pem.



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

def pem
  @pem
end

#pem_passwordObject

Returns the value of attribute pem_password.



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

def pem_password
  @pem_password
end

#proxy_addressObject

Returns the value of attribute proxy_address.



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

def proxy_address
  @proxy_address
end

#proxy_portObject

Returns the value of attribute proxy_port.



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

def proxy_port
  @proxy_port
end

#read_timeoutObject

Returns the value of attribute read_timeout.



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

def read_timeout
  @read_timeout
end

#ssl_connectionObject (readonly)

Returns the value of attribute ssl_connection.



30
31
32
# File 'lib/active_merchant/connection.rb', line 30

def ssl_connection
  @ssl_connection
end

#ssl_versionObject

Returns the value of attribute ssl_version.



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

def ssl_version
  @ssl_version
end

#tagObject

Returns the value of attribute tag.



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

def tag
  @tag
end

#verify_peerObject

Returns the value of attribute verify_peer.



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

def verify_peer
  @verify_peer
end

#wiredump_deviceObject

Returns the value of attribute wiredump_device.



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

def wiredump_device
  @wiredump_device
end

Instance Method Details

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



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
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_merchant/connection.rb', line 68

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
    begin
      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
          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))
        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
  end
ensure
  info 'connection_request_total_time=%.4fs' % [Process.clock_gettime(Process::CLOCK_MONOTONIC) - request_start], tag
  http.finish if http.started?
end