Module: Adyen::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/adyen/util.rb

Instance Method Summary collapse

Instance Method Details

#camelize(identifier) ⇒ String

Returns the camelized version of a string.

Parameters:

  • identifier (:to_s)

    The identifier to turn to camelcase.

Returns:

  • (String)

    The camelcase version of the identifier provided.



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

Examples:

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

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

Parameters:

  • flattened_hash (Hash)

    The flat hash to transform

  • return_hash (Hash) (defaults to: {})

    The new hash which will be returned (needed for recursive calls)

Returns:

  • (Hash)

    A nested hash structure, using strings as key.



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

Examples:

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

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

Parameters:

  • nested_hash (Hash)

    The nested hash to transform

  • prefix (String) (defaults to: "")

    The prefix to add to the key

  • return_hash (Hash) (defaults to: {})

    The new hash which is retruned (needed for recursive calls)

Returns:

  • (Hash)

    The return hash filled with camelized and prefixed key, stringified value



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 format_timestamp(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.

Parameters:

  • message (String)

    The message to compress and encode.

Returns:

  • (String)

    The compressed and encoded version of the message



47
48
49
50
51
52
53
# File 'lib/adyen/util.rb', line 47

def gzip_base64(message)
  sio = StringIO.new
  gz  = Zlib::GzipWriter.new(sio)
  gz.write(message)
  gz.close
  Base64.strict_encode64(sio.string)
end

#hmac_base64(hmac_key, message) ⇒ String

Returns a base64-encoded signature for a message

Parameters:

  • hmac_key (String)

    The secret key to use for the HMAC signature.

  • message (String)

    The message to sign.

Returns:

  • (String)

    The signature, base64-encoded.



39
40
41
42
# File 'lib/adyen/util.rb', line 39

def hmac_base64(hmac_key, message)
  digest = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), hmac_key, message)
  Base64.strict_encode64(digest).strip
end

#underscore(identifier) ⇒ String

Returns the underscore version of a string.

Parameters:

  • identifier (:to_s)

    The identifier to turn to underscore notation.

Returns:

  • (String)

    The underscore version of the identifier provided.



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