Class: Cybersourcery::MerchantDataSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/cybersourcery/merchant_data_serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_count = 1) ⇒ MerchantDataSerializer

Returns a new instance of MerchantDataSerializer.



5
6
7
# File 'lib/cybersourcery/merchant_data_serializer.rb', line 5

def initialize(start_count = 1)
  @start_count = start_count
end

Instance Attribute Details

#start_countObject (readonly)

Returns the value of attribute start_count.



3
4
5
# File 'lib/cybersourcery/merchant_data_serializer.rb', line 3

def start_count
  @start_count
end

Instance Method Details

#deserialize(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cybersourcery/merchant_data_serializer.rb', line 26

def deserialize(params)
  merchant_data = params.select { |k,v| k =~ /^merchant_defined_data/ }.symbolize_keys
  merchant_data_string = ''

  # it's important to reassemble the data in the right order!
  merchant_data.length.times do |i|
    merchant_data_string << merchant_data["merchant_defined_data#{i+1}".to_sym]
  end

  JSON.parse(merchant_data_string).symbolize_keys
end

#serialize(merchant_data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cybersourcery/merchant_data_serializer.rb', line 9

def serialize(merchant_data)
  scanned_data = merchant_data.to_json.scan(/.{1,100}/)
  serialized_data = {}

  scanned_data.each_with_index do |item, index|
    count = index + @start_count

    if count < 1 || count > 100
      raise Cybersourcery::CybersourceryError, "The supported merchant_defined_data range is 1 to 100. #{count} is out of range."
    end

    serialized_data["merchant_defined_data#{count}".to_sym] = item
  end

  serialized_data
end