Module: TelphinApi::Utils

Defined in:
lib/telphin_api/utils.rb

Overview

An utility module able to flatten arguments (join arrays into comma-separated strings).

Class Method Summary collapse

Class Method Details

.flatten_argument(argument) ⇒ Object

If an argument is an array, it will be joined with a comma; otherwise it'll be returned untouched.

Parameters:

  • argument (Object)

    The argument to flatten.



17
18
19
20
21
22
23
24
25
# File 'lib/telphin_api/utils.rb', line 17

def flatten_argument(argument)
  if argument.respond_to?(:join)
    # if argument is an array, we join it with a comma
    argument.join(',')
  else
    # otherwise leave it untouched
    argument
  end
end

.flatten_arguments(arguments) ⇒ Hash

A multiple version of #flatten_argument. It transforms a hash flattening each value and keeping the keys untouched.

Parameters:

  • arguments (Hash)

    The arguments to flatten.

Returns:

  • (Hash)

    Flattened arguments.



8
9
10
11
12
13
# File 'lib/telphin_api/utils.rb', line 8

def flatten_arguments(arguments)
  arguments.inject({}) do |flat_args, (arg_name, arg_value)|
    flat_args[arg_name] = flatten_argument(arg_value)
    flat_args
  end
end