Module: Adyen::Util
Instance Method Summary collapse
-
#camelize(identifier) ⇒ String
Returns the camelized version of a string.
-
#deflatten(flattened_hash, return_hash = {}) ⇒ Hash
Transforms a flat hash into a nested hash structure.
-
#flatten(nested_hash, prefix = "", return_hash = {}) ⇒ Hash
Transforms the nested parameters Hash into a ‘flat’ Hash which is understood by adyen.
-
#format_date(date) ⇒ Object
Returns a valid Adyen string representation for a date.
-
#format_timestamp(time) ⇒ Object
Returns a valid Adyen string representation for a timestamp.
-
#gzip_base64(message) ⇒ String
Retuns a message gzip-compressed and base64-encoded.
-
#hmac_base64(hmac_key, message) ⇒ String
Returns a base64-encoded signature for a message.
-
#underscore(identifier) ⇒ String
Returns the underscore version of a string.
Instance Method Details
#camelize(identifier) ⇒ String
Returns the camelized version of a string.
58 59 60 |
# File 'lib/adyen/util.rb', line 58 def camelize(identifier) CAMELCASE_EXCEPTIONS[identifier.to_s] || identifier.to_s.gsub(/_+(.)/) { $1.upcase } end |
#deflatten(flattened_hash, return_hash = {}) ⇒ Hash
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
114 115 116 117 118 119 120 |
# File 'lib/adyen/util.rb', line 114 def deflatten(flattened_hash, return_hash = {}) return return_hash if flattened_hash.nil? flattened_hash.each do |key, value| deflatten_pair(key, value, return_hash) end return_hash end |
#flatten(nested_hash, prefix = "", return_hash = {}) ⇒ Hash
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
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/adyen/util.rb', line 87 def flatten(nested_hash, prefix = "", return_hash = {}) nested_hash ||= {} nested_hash.inject(return_hash) do |hash, (key, value)| key = "#{prefix}#{camelize(key)}" if value.is_a?(Hash) flatten(value, "#{key}.", return_hash) else hash[key] = value.to_s end hash end end |
#format_date(date) ⇒ Object
Returns a valid Adyen string representation for a date
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/adyen/util.rb', line 10 def format_date(date) case date when Date, DateTime, Time date.strftime('%Y-%m-%d') when String raise ArgumentError, "Invalid date notation: #{date.inspect}!" unless /^\d{4}-\d{2}-\d{2}$/ =~ date date else raise ArgumentError, "Cannot convert #{date.inspect} to date!" end end |
#format_timestamp(time) ⇒ Object
Returns a valid Adyen string representation for a timestamp
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/adyen/util.rb', line 23 def (time) case time when Date, DateTime, Time time.strftime('%Y-%m-%dT%H:%M:%SZ') when String raise ArgumentError, "Invalid timestamp notation: #{time.inspect}!" unless /^\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}Z$/ =~ time time else raise ArgumentError, "Cannot convert #{time.inspect} to timestamp!" end end |
#gzip_base64(message) ⇒ String
Retuns a message gzip-compressed and base64-encoded.
47 48 49 50 51 52 53 |
# File 'lib/adyen/util.rb', line 47 def gzip_base64() sio = StringIO.new gz = Zlib::GzipWriter.new(sio) gz.write() gz.close Base64.strict_encode64(sio.string) end |
#hmac_base64(hmac_key, message) ⇒ String
Returns a base64-encoded signature for a message
39 40 41 42 |
# File 'lib/adyen/util.rb', line 39 def hmac_base64(hmac_key, ) digest = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), hmac_key, ) Base64.strict_encode64(digest).strip end |
#underscore(identifier) ⇒ String
Returns the underscore version of a string.
65 66 67 68 69 70 |
# File 'lib/adyen/util.rb', line 65 def underscore(identifier) UNDERSCORE_EXCEPTIONS[identifier.to_s] || identifier.to_s .gsub(/([A-Z]{2,})([A-Z])/) { "#{$1.downcase}#{$2}" } .gsub(/(?!\A)([A-Z][a-z]*)/, '_\1') .downcase end |