module Adyen::Util

Constants

CAMELCASE_EXCEPTIONS

This hash contains exceptions to the standard underscore to camelcase conversion rules.

UNDERSCORE_EXCEPTIONS

This hash contains exceptions to the standard camelcase to underscore conversion rules.

Public Instance Methods

camelize(identifier) click to toggle source

Returns the camelized version of a string. @param [:to_s] identifier The identifier to turn to camelcase. @return [String] The camelcase version of the identifier provided.

   # File lib/adyen/util.rb
58 def camelize(identifier)
59   CAMELCASE_EXCEPTIONS[identifier.to_s] || identifier.to_s.gsub(/_+(.)/) { $1.upcase }
60 end
deflatten(flattened_hash, return_hash = {}) click to toggle source

Transforms a flat hash into a nested hash structure.

* all keys are underscored
* all keys are stringified
* flattened hash is deflattened, using . as namespace separator

@example

deflatten {'billingAddress.street' =>  'My Street'}

# resolves in:
{'billing_address' => { 'street' => 'My Street'}}

@param [Hash] flattened_hash The flat hash to transform @param [Hash] return_hash The new hash which will be returned (needed for recursive calls) @return [Hash] A nested hash structure, using strings as key.

    # File lib/adyen/util.rb
114 def deflatten(flattened_hash, return_hash = {})
115   return return_hash if flattened_hash.nil?
116   flattened_hash.each do |key, value|
117     deflatten_pair(key, value, return_hash)
118   end
119   return_hash
120 end
flatten(nested_hash, prefix = "", return_hash = {}) click to toggle source

Transforms the nested parameters Hash into a 'flat' Hash which is understood by adyen. This is:

* all keys are camelized
* all keys are stringified
* nested hash is flattened, keys are prefixed with root key

@example

flatten {:billing_address => { :street => 'My Street'}}

# resolves in:
{'billingAddress.street' =>  'My Street'}

@param [Hash] nested_hash The nested hash to transform @param [String] prefix The prefix to add to the key @param [Hash] return_hash The new hash which is retruned (needed for recursive calls) @return [Hash] The return hash filled with camelized and prefixed key, stringified value

   # File lib/adyen/util.rb
87 def flatten(nested_hash, prefix = "", return_hash = {})
88   nested_hash ||= {}
89   nested_hash.inject(return_hash) do |hash, (key, value)|
90     key = "#{prefix}#{camelize(key)}"
91     if value.is_a?(Hash)
92       flatten(value, "#{key}.", return_hash)
93     else
94       hash[key] = value.to_s
95     end
96     hash
97   end
98 end
format_date(date) click to toggle source

Returns a valid Adyen string representation for a date

   # File lib/adyen/util.rb
10 def format_date(date)
11   case date
12   when Date, DateTime, Time
13     date.strftime('%Y-%m-%d')
14   when String
15     raise ArgumentError, "Invalid date notation: #{date.inspect}!" unless /^\d{4}-\d{2}-\d{2}$/ =~ date
16     date
17   else
18     raise ArgumentError, "Cannot convert #{date.inspect} to date!"
19   end
20 end
format_timestamp(time) click to toggle source

Returns a valid Adyen string representation for a timestamp

   # File lib/adyen/util.rb
23 def format_timestamp(time)
24   case time
25   when Date, DateTime, Time
26     time.strftime('%Y-%m-%dT%H:%M:%SZ')
27   when String
28     raise ArgumentError, "Invalid timestamp notation: #{time.inspect}!" unless /^\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}Z$/ =~ time
29     time
30   else
31     raise ArgumentError, "Cannot convert #{time.inspect} to timestamp!"
32   end
33 end
gzip_base64(message) click to toggle source

Retuns a message gzip-compressed and base64-encoded. @param [String] message The message to compress and encode. @return [String] The compressed and encoded version of the message

   # File lib/adyen/util.rb
47 def gzip_base64(message)
48   sio = StringIO.new
49   gz  = Zlib::GzipWriter.new(sio)
50   gz.write(message)
51   gz.close
52   Base64.strict_encode64(sio.string)
53 end
hmac_base64(hmac_key, message) click to toggle source

Returns a base64-encoded signature for a message @param [String] hmac_key The secret key to use for the HMAC signature. @param [String] message The message to sign. @return [String] The signature, base64-encoded.

   # File lib/adyen/util.rb
39 def hmac_base64(hmac_key, message)
40   digest = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), hmac_key, message)
41   Base64.strict_encode64(digest).strip
42 end
underscore(identifier) click to toggle source

Returns the underscore version of a string. @param [:to_s] identifier The identifier to turn to underscore notation. @return [String] The underscore version of the identifier provided.

   # File lib/adyen/util.rb
65 def underscore(identifier)
66   UNDERSCORE_EXCEPTIONS[identifier.to_s] || identifier.to_s
67     .gsub(/([A-Z]{2,})([A-Z])/) { "#{$1.downcase}#{$2}" }
68     .gsub(/(?!\A)([A-Z][a-z]*)/, '_\1')
69     .downcase
70 end

Private Instance Methods

deflatten_pair(key, value, return_hash) click to toggle source
    # File lib/adyen/util.rb
124 def deflatten_pair(key, value, return_hash)
125   head, rest = key.split('.', 2)
126   key = underscore(head)
127   if rest.nil?
128     raise ArgumentError, "Duplicate key in flattened hash." if return_hash.key?(key)
129     return_hash[key] = value
130   else
131     return_hash[key] ||= {}
132     raise ArgumentError, "Key nesting conflict in flattened hash." unless return_hash[key].is_a?(Hash)
133     deflatten_pair(rest, value, return_hash[key])
134   end
135 end