Class: Braintree::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/braintree/http.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Http

Returns a new instance of Http.



4
5
6
# File 'lib/braintree/http.rb', line 4

def initialize(config)
  @config = config
end

Instance Method Details

#_body(response) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/braintree/http.rb', line 102

def _body(response)
  if response.header["Content-Encoding"] == "gzip"
    Zlib::GzipReader.new(StringIO.new(response.body)).read
  else
    raise UnexpectedError, "expected a gzipped response"
  end
end

#_build_query_string(params) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/braintree/http.rb', line 50

def _build_query_string(params)
  if params.empty?
    ""
  else
    "?" + params.map do |x, y|
      raise(ArgumentError, "Nested hashes aren't supported in query parameters") if y.respond_to?(:to_hash)
      "#{x}=#{y}"
    end.join("&")
  end
end

#_build_xml(params) ⇒ Object



45
46
47
48
# File 'lib/braintree/http.rb', line 45

def _build_xml(params)
  return "" if params.nil?
  Braintree::Xml.hash_to_xml params
end

#_current_timeObject



110
111
112
# File 'lib/braintree/http.rb', line 110

def _current_time
  Time.now.utc.strftime("%d/%b/%Y %H:%M:%S %Z")
end

#_format_and_sanitize_body_for_log(input_xml) ⇒ Object



114
115
116
117
118
119
# File 'lib/braintree/http.rb', line 114

def _format_and_sanitize_body_for_log(input_xml)
  formatted_xml = input_xml.gsub(/^/, "[Braintree] ")
  formatted_xml = formatted_xml.gsub(/<number>(.{6}).+?(.{4})<\/number>/, '<number>\1******\2</number>')
  formatted_xml = formatted_xml.gsub(/<cvv>.+?<\/cvv>/, '<cvv>***</cvv>')
  formatted_xml
end

#_http_do(http_verb, path, body = nil) ⇒ Object



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
# File 'lib/braintree/http.rb', line 61

def _http_do(http_verb, path, body = nil)
  connection = Net::HTTP.new(@config.server, @config.port)
  connection.open_timeout = @config.http_open_timeout
  connection.read_timeout = @config.http_read_timeout
  if @config.ssl?
    connection.use_ssl = true
    connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
    connection.ca_file = @config.ca_file
    connection.verify_callback = proc { |preverify_ok, ssl_context| _verify_ssl_certificate(preverify_ok, ssl_context) }
  end
  connection.start do |http|
    request = http_verb.new(path)
    request["Accept"] = "application/xml"
    request["User-Agent"] = @config.user_agent
    request["Accept-Encoding"] = "gzip"
    request["X-ApiVersion"] = @config.api_version
    if @config.client_credentials?
      request.basic_auth @config.client_id, @config.client_secret
    elsif @config.access_token
      request["Authorization"] = "Bearer #{@config.access_token}"
    else
      request.basic_auth @config.public_key, @config.private_key
    end
    @config.logger.debug "[Braintree] [#{_current_time}] #{request.method} #{path}"
    if body
      request["Content-Type"] = "application/xml"
      request.body = body
      @config.logger.debug _format_and_sanitize_body_for_log(body)
    end
    response = http.request(request)
    @config.logger.info "[Braintree] [#{_current_time}] #{request.method} #{path} #{response.code}"
    @config.logger.debug "[Braintree] [#{_current_time}] #{response.code} #{response.message}"
    if @config.logger.level == Logger::DEBUG
      @config.logger.debug _format_and_sanitize_body_for_log(_body(response))
    end
    response
  end
rescue OpenSSL::SSL::SSLError
  raise Braintree::SSLCertificateError
end

#_verify_ssl_certificate(preverify_ok, ssl_context) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/braintree/http.rb', line 121

def _verify_ssl_certificate(preverify_ok, ssl_context)
  if preverify_ok != true || ssl_context.error != 0
    err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
    @config.logger.error err_msg
    false
  else
    true
  end
end

#delete(path) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/braintree/http.rb', line 8

def delete(path)
  response = _http_do Net::HTTP::Delete, path
  if response.code.to_i == 200
    true
  else
    Util.raise_exception_for_status_code(response.code)
  end
end

#get(_path, query_params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/braintree/http.rb', line 17

def get(_path, query_params={})
  path = _path + _build_query_string(query_params)
  response = _http_do Net::HTTP::Get, path
  if response.code.to_i == 200 || response.code.to_i == 422
    Xml.hash_from_xml(_body(response))
  else
    Util.raise_exception_for_status_code(response.code)
  end
end

#post(path, params = nil) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/braintree/http.rb', line 27

def post(path, params = nil)
  response = _http_do Net::HTTP::Post, path, _build_xml(params)
  if response.code.to_i == 200 || response.code.to_i == 201 || response.code.to_i == 422
    Xml.hash_from_xml(_body(response))
  else
    Util.raise_exception_for_status_code(response.code)
  end
end

#put(path, params = nil) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/braintree/http.rb', line 36

def put(path, params = nil)
  response = _http_do Net::HTTP::Put, path, _build_xml(params)
  if response.code.to_i == 200 || response.code.to_i == 201 || response.code.to_i == 422
    Xml.hash_from_xml(_body(response))
  else
    Util.raise_exception_for_status_code(response.code)
  end
end