Method: TestPack1::APIHelper.serialize_array

Defined in:
lib/test_pack_1/api_helper.rb

.serialize_array(key, array, formatting: 'indexed') ⇒ Object

Serializes an array parameter (creates key value pairs).

Parameters:

  • The (String)

    name of the parameter.

  • The (Array)

    value of the parameter.

  • The (String)

    format of the serialization.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/test_pack_1/api_helper.rb', line 13

def self.serialize_array(key, array, formatting: 'indexed')
  tuples = []

  if formatting == 'unindexed'
    tuples += array.map { |element| ["#{key}[]", element] }
  elsif formatting == 'indexed'
    tuples += array.map.with_index do |element, index|
      ["#{key}[#{index}]", element]
    end
  elsif formatting == 'plain'
    tuples += array.map { |element| [key, element] }
  else
    raise ArgumentError, 'Invalid format provided.'
  end
  tuples
end