Module: Transbank::Utils::NetHelper

Instance Method Summary collapse

Instance Method Details

#http_delete(uri_string:, body: nil, headers: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/transbank/sdk/utils/net_helper.rb', line 39

def http_delete(uri_string:, body: nil, headers: nil)
  uri = URI.parse(uri_string)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'

  request_headers = {'Content-Type' => 'application/json'}.merge(headers || {})
  request = Net::HTTP::Delete.new(uri.path, request_headers)
  request.body = JSON.generate(body)
  http.request(request)
end

#http_get(uri_string:, headers: nil) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/transbank/sdk/utils/net_helper.rb', line 4

def http_get(uri_string:, headers:nil)
  uri = URI.parse(uri_string)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  request_headers = {'Content-Type' => 'application/json'}.merge(headers || {})
  request = Net::HTTP::Get.new(uri.path, request_headers)
  http.request(request)
end

#http_post(uri_string:, body: nil, headers: nil, camel_case_keys: true) ⇒ Hash

POST a request to Transbank’s servers, and return the parsed response

Parameters:

  • uri_string (String)

    an URI to post to

  • body (Hash) (defaults to: nil)

    the body of your POST request

Returns:

  • (Hash)

    the JSON.parse’d response body



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/transbank/sdk/utils/net_helper.rb', line 16

def http_post(uri_string:, body: nil, headers: nil, camel_case_keys: true)
  uri = URI.parse(uri_string)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'

  request_headers = {'Content-Type' => 'application/json'}.merge(headers || {})
  request = Net::HTTP::Post.new(uri.path, request_headers)
  sendable_body = camel_case_keys ? keys_to_camel_case(body) : body
  request.body = sendable_body.to_json
  http.request(request)
end

#http_put(uri_string:, body: nil, headers: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/transbank/sdk/utils/net_helper.rb', line 28

def http_put(uri_string:, body: nil, headers: nil)
  uri = URI.parse(uri_string)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'

  request_headers = {'Content-Type' => 'application/json'}.merge(headers || {})
  request = Net::HTTP::Put.new(uri.path, request_headers)
  request.body = JSON.generate(body)
  http.request(request)
end

#keys_to_camel_case(hash) ⇒ Object

Required for sending data to Transbank on Onepay.



51
52
53
54
55
56
57
58
59
60
# File 'lib/transbank/sdk/utils/net_helper.rb', line 51

def keys_to_camel_case(hash)
  hash.reduce({}) do |new_hash, (key, val)|
    if val.is_a? Array
      val = val.map {|value| value.is_a?(Hash) ? keys_to_camel_case(value) : value }
    end
    new_key = snake_to_camel_case(key.to_s)
    new_hash[new_key] = val
    new_hash
  end
end

#patpass_comercio_headers(commerce_code:, api_key:) ⇒ Object



73
74
75
76
77
78
# File 'lib/transbank/sdk/utils/net_helper.rb', line 73

def patpass_comercio_headers(commerce_code:, api_key:)
  {
      "commercecode" => commerce_code,
      "Authorization" => api_key
  }
end

#snake_to_camel_case(str) ⇒ Object



62
63
64
# File 'lib/transbank/sdk/utils/net_helper.rb', line 62

def snake_to_camel_case(str)
  str.split('_').reduce { |string, current_word| string + current_word.capitalize }
end

#webpay_headers(commerce_code:, api_key:) ⇒ Object



66
67
68
69
70
71
# File 'lib/transbank/sdk/utils/net_helper.rb', line 66

def webpay_headers(commerce_code:, api_key:)
  {
      "Tbk-Api-Key-Id" => commerce_code,
      "Tbk-Api-Key-Secret" => api_key
  }
end