Module: IGMarkets::ResponseParser

Defined in:
lib/ig_markets/response_parser.rb

Overview

Contains methods for parsing responses received from the IG Markets API.

Class Method Summary collapse

Class Method Details

.camel_case_to_snake_case(camel_case) ⇒ String

Converts a camel case string to a snake case string.

Parameters:

  • camel_case (String, Symbol)

    The camel case ‘String` or `Symbol` to convert to snake case.

Returns:

  • (String)


34
35
36
# File 'lib/ig_markets/response_parser.rb', line 34

def camel_case_to_snake_case(camel_case)
  camel_case.to_s.gsub(/([a-z])([A-Z])/, '\1_\2').gsub(/([A-Z])([A-Z])([a-z])/, '\1_\2\3').downcase
end

.parse(response) ⇒ Hash, ...

Parses the specified value that was returned from a call to the IG Markets API.

Parameters:

  • response (Hash, Array, Object)

    The response or part of a response that should be parsed. If this is of type ‘Hash` then all hash keys will converted from camel case into snake case and their values will each be parsed individually by a recursive call. If this is of type `Array` then each item will be parsed individually by a recursive call. All other types are passed through unchanged.

Returns:

  • (Hash, Array, Object)

    The parsed object, the type depends on the type of the ‘response` parameter.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ig_markets/response_parser.rb', line 16

def parse(response)
  case response
  when Hash
    response.each_with_object({}) do |(key, value), new_hash|
      new_hash[camel_case_to_snake_case(key).to_sym] = parse(value)
    end
  when Array
    response.map { |item| parse item }
  else
    response
  end
end