Class: CheckoutSdk::JsonSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/checkout_sdk/json_serializer.rb

Constant Summary collapse

KEYS_TRANSFORMATIONS =
{ three_ds: '3ds',
account_holder_type: 'account-holder-type',
payment_network: 'payment-network',
from_: 'from',
if_match: 'if-match' }.freeze

Class Method Summary collapse

Class Method Details

.keys_transformation(key_name) ⇒ Object



23
24
25
26
27
28
# File 'lib/checkout_sdk/json_serializer.rb', line 23

def self.keys_transformation(key_name)
  key = key_name.to_s.delete('@')
  return KEYS_TRANSFORMATIONS[key.to_sym] if KEYS_TRANSFORMATIONS.key?(key.to_sym)

  key
end

.serialize_array(input_array) ⇒ Object

Parameters:

  • input_array (Array)


31
32
33
34
35
36
37
38
# File 'lib/checkout_sdk/json_serializer.rb', line 31

def self.serialize_array(input_array)
  aux_array = []
  input_array.map do |value|
    value = serialize_by_type(value)
    aux_array.append(value)
  end
  aux_array
end

.serialize_by_type(value) ⇒ Object



50
51
52
53
54
55
# File 'lib/checkout_sdk/json_serializer.rb', line 50

def self.serialize_by_type(value)
  value = serialize_array(value) if value.is_a?(Array)
  value = serialize_hash(value) if value.is_a?(Hash)
  value = to_custom_hash(value) if value.class.name.start_with? CheckoutSdk.name
  value
end

.serialize_hash(input_hash) ⇒ Object

Parameters:

  • input_hash (Hash)


41
42
43
44
45
46
47
48
# File 'lib/checkout_sdk/json_serializer.rb', line 41

def self.serialize_hash(input_hash)
  aux_hash = {}
  input_hash.map do |key, value|
    value = serialize_by_type(value)
    aux_hash[key] = value
  end
  aux_hash
end

.to_custom_hash(object) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/checkout_sdk/json_serializer.rb', line 11

def self.to_custom_hash(object)
  hash = {}
  return object if object.is_a? Hash

  object.instance_variables.each do |v|
    value = object.instance_variable_get(v)
    value = serialize_by_type(value)
    hash[keys_transformation(v)] = value
  end
  hash
end