Module: Justifi::Util

Defined in:
lib/justifi/util.rb

Class Method Summary collapse

Class Method Details

.compute_signature(received_event, timestamp, secret_key) ⇒ Object

Creates a computed signature that can verify the payload sent. Each webhook has its own signature key that can be achieved in JustiFi’s platform



84
85
86
87
# File 'lib/justifi/util.rb', line 84

def self.compute_signature(received_event, timestamp, secret_key)
  timestamp_payload = "#{timestamp}.#{received_event.to_json}"
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret_key, timestamp_payload)
end

.encode_parameters(params) ⇒ Object

Encodes a hash of parameters in a way that’s suitable for use as query parameters in a URI.



33
34
35
36
# File 'lib/justifi/util.rb', line 33

def self.encode_parameters(params)
  Util.flatten_params(params)
    .map { |k, v| "#{url_encode(k)}=#{url_encode(v)}" }.join("&")
end

.flatten_params(params, parent_key = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/justifi/util.rb', line 49

def self.flatten_params(params, parent_key = nil)
  result = []

  # do not sort the final output because arrays (and arrays of hashes
  # especially) can be order sensitive, but do sort incoming parameters
  params.each do |key, value|
    calculated_key = parent_key ? "#{parent_key}[#{key}]" : key.to_s
    if value.is_a?(Hash)
      result += flatten_params(value, calculated_key)
    elsif value.is_a?(Array)
      result += flatten_params_array(value, calculated_key)
    else
      result << [calculated_key, value]
    end
  end

  result
end

.flatten_params_array(value, calculated_key) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/justifi/util.rb', line 68

def self.flatten_params_array(value, calculated_key)
  result = []
  value.each_with_index do |elem, i|
    if elem.is_a?(Hash)
      result += flatten_params(elem, "#{calculated_key}[#{i}]")
    elsif elem.is_a?(Array)
      result += flatten_params_array(elem, calculated_key)
    else
      result << ["#{calculated_key}[#{i}]", elem]
    end
  end
  result
end

.normalize_headers(headers) ⇒ Object

Normalizes header keys so that they’re all lower case and each hyphen-delimited section starts with a single capitalized letter. For example, ‘request-id` becomes `Request-Id`. This is useful for extracting certain key values when the user could have set them with a variety of diffent naming schemes.



22
23
24
25
26
27
28
29
# File 'lib/justifi/util.rb', line 22

def self.normalize_headers(headers)
  headers.each_with_object({}) do |(k, v), new_headers|
    k = k.to_s.tr("_", "-") if k.is_a?(Symbol)
    k = k.split("-").reject(&:empty?).map(&:capitalize).join("-")

    new_headers[k] = v
  end
end

.normalize_params(params) ⇒ Object



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

def self.normalize_params(params)
  case params
  when Hash
    params.dup.to_json
  else
    raise TypeError, "normalize_params expects a hash"
  end
end

.url_encode(key) ⇒ Object

Encodes a string in a way that makes it suitable for use in a set of query parameters in a URI or in a set of form parameters in a request body.



41
42
43
44
45
46
47
# File 'lib/justifi/util.rb', line 41

def self.url_encode(key)
  CGI.escape(key.to_s).
    # Don't use strict form encoding by changing the square bracket control
    # characters back to their literals. This is fine by the server, and
    # makes these parameter strings easier to read.
    gsub("%5B", "[").gsub("%5D", "]")
end