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)


31
32
33
# File 'lib/ig_markets/response_parser.rb', line 31

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')
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 reponse 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 each to parsed individually by a recursive call. If this is of type Array then each item will be parsed indidiaully 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.



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

def parse(response)
  if response.is_a? Hash
    response.each_with_object({}) do |(key, value), new_hash|
      new_hash[camel_case_to_snake_case(key).downcase.to_sym] = parse(value)
    end
  elsif response.is_a? Array
    response.map { |item| parse item }
  else
    response
  end
end