Module: OandaAPI::Streaming::Adapters::Generic

Extended by:
Generic
Included in:
Generic
Defined in:
lib/oanda_api/streaming/adapters/generic.rb

Overview

Uses the JSON library. This parser does not handle multiple json objects in a json stream unless the objects are separated with whitespace.

Constant Summary collapse

DELIMITER =

A delimiter for separating multiple json objects in a stream.

"<oanda_api::delimiter>"
MULTI_OBJECT_DELIMITER =
"}#{DELIMITER}{"

Instance Method Summary collapse

Instance Method Details

#parse(string) ⇒ Array<Hash>

Deserializes a stream of JSON objects.

Parameters:

  • string (String)

    serialized json.

Returns:

  • (Array<Hash>)

    an array of hashes.



19
20
21
22
23
24
25
# File 'lib/oanda_api/streaming/adapters/generic.rb', line 19

def parse(string)
  string.strip!
  return [] if string.empty?
  string.gsub(/}\s*{/, MULTI_OBJECT_DELIMITER).split(DELIMITER).map do |json|
    JSON.parse json, symbolize_names: true
  end
end