Class: Cardflex::Http

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Http

Returns a new instance of Http.



5
6
7
# File 'lib/cardflex/http.rb', line 5

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/cardflex/http.rb', line 3

def config
  @config
end

Instance Method Details

#_add_api_key(params) ⇒ Object



79
80
81
82
# File 'lib/cardflex/http.rb', line 79

def _add_api_key(params)
  key = params.keys[0]
  { key => params[key].merge(:api_key => @config.api_key) }
end

#_current_timeObject



65
66
67
# File 'lib/cardflex/http.rb', line 65

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

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cardflex/http.rb', line 21

def _do_http(http_verb, path, body=nil)
  connection = Net::HTTP.new(@config.server, @config.port)
  connection.open_timeout = 60
  connection.read_timeout = 60
  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'] = 'text/xml'
    @config.logger.debug("[Cardflex] [#{_current_time}] #{request.method} as text/xml")
    if body
      request['Content-Type'] = 'text/xml'
      request.body = body
      @config.logger.debug _format_body_for_log(body)
    end

    response = http.request(request)
    @config.logger.info "[Cardflex] [#{_current_time}] #{request.method} #{response.code}"
    @config.logger.debug _format_body_for_log(response.body)
    response
  end
rescue OpenSSL::SSL::SSLError
  raise Cardflex::SSLCertificateError
end

#_format_body_for_log(body) ⇒ Object



84
85
86
# File 'lib/cardflex/http.rb', line 84

def _format_body_for_log(body)
  body.gsub(/^/, "[Cardflex] ")
end

#_hash_to_query_string(hash) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cardflex/http.rb', line 51

def _hash_to_query_string(hash)
  str = "?"
  hash.each do |key, value|
    case value
    when ::Array
      str << "#{key}=#{value.join(',')}&"
    when ::String
      str << "#{key}=#{value}&"
    end
  end

  str.gsub(/&$/, '') # remove trailing &, if any
end

#_verify_ssl_certificate(preverify_ok, ssl_context) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/cardflex/http.rb', line 69

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

#get(params = {}) ⇒ Object



9
10
11
12
13
# File 'lib/cardflex/http.rb', line 9

def get(params={})
  uri = "#{@config.query_path}#{_hash_to_query_string(params)}"
  response = _do_http(Net::HTTP::Get, uri)
  Xml.parse(response.body)
end

#post(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/cardflex/http.rb', line 15

def post(params={})
  params = _add_api_key(params)
  response = _do_http(Net::HTTP::Post, @config.three_step_path, Xml.hash_to_xml(params))
  Xml.parse(response.body)
end