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
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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_merchant/connection.rb', line 36

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
  @proxy_address = nil
  @proxy_port = nil
end

Instance Attribute Details

#ca_fileObject

Returns the value of attribute ca_file.



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

def ca_file
  @ca_file
end

#ca_pathObject

Returns the value of attribute ca_path.



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

def ca_path
  @ca_path
end

#endpointObject

Returns the value of attribute endpoint.



19
20
21
# File 'lib/active_merchant/connection.rb', line 19

def endpoint
  @endpoint
end

#ignore_http_statusObject

Returns the value of attribute ignore_http_status.



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

def ignore_http_status
  @ignore_http_status
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#max_retriesObject

Returns the value of attribute max_retries.



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

def max_retries
  @max_retries
end

#open_timeoutObject

Returns the value of attribute open_timeout.



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

def open_timeout
  @open_timeout
end

#pemObject

Returns the value of attribute pem.



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

def pem
  @pem
end

#pem_passwordObject

Returns the value of attribute pem_password.



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

def pem_password
  @pem_password
end

#proxy_addressObject

Returns the value of attribute proxy_address.



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

def proxy_address
  @proxy_address
end

#proxy_portObject

Returns the value of attribute proxy_port.



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

def proxy_port
  @proxy_port
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_versionObject

Returns the value of attribute ssl_version.



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

def ssl_version
  @ssl_version
end

#tagObject

Returns the value of attribute tag.



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

def tag
  @tag
end

#verify_peerObject

Returns the value of attribute verify_peer.



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

def verify_peer
  @verify_peer
end

#wiredump_deviceObject

Returns the value of attribute wiredump_device.



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

def wiredump_device
  @wiredump_device
end

Instance Method Details

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



51
52
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
90
91
92
93
# File 'lib/active_merchant/connection.rb', line 51

def request(method, body, headers = {})
  request_start = Time.now.to_f

  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
        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.
          raise ArgumentError, "DELETE requests do not support a request body" if body
          http.delete(endpoint.request_uri, 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
      debug result.body
      result
    end
  end

ensure
  info "connection_request_total_time=%.4fs" % [Time.now.to_f - request_start], tag
end