Module: OandaAPI::Streaming::JsonParser

Extended by:
JsonParser
Included in:
JsonParser
Defined in:
lib/oanda_api/streaming/json_parser.rb

Overview

Used to deserialize a stream of JSON objects. Will load and use a streaming JSON parser if one is installed, otherwise defaults to use the JSON gem.

Much of this module's code was borrowed from multi_json.

Constant Summary collapse

REQUIREMENT_MAP =

Map parser adapters to the gem library they require.

{
  gson: "gson",
  yajl: "yajl"
}

Instance Method Summary collapse

Instance Method Details

#adapter.parse

Loads (if not already loaded) and returns the current adapter class.

Returns:

  • (.parse)

    a class implementing a .parse method



21
22
23
24
25
26
27
# File 'lib/oanda_api/streaming/json_parser.rb', line 21

def adapter
  return @adapter if defined?(@adapter) && @adapter

  # Load default adapter
  self.use nil
  @adapter
end

#default_adapterSymbol

Returns a symbol identifying either the currently loaded adapter or one that can be loaded. Preference is given to an adapter optimized for parsing streaming json if it is installed.

Returns:

  • (Symbol)

    a symbol identifying an adapter either currently loaded or that one that can be loaded.



35
36
37
# File 'lib/oanda_api/streaming/json_parser.rb', line 35

def default_adapter
  jruby? ? try_adapter(:gson) : try_adapter(:yajl)
end

#load_adapter(new_adapter) ⇒ .parse

Loads the requested adapter.

Parameters:

  • new_adapter (String, Symbol, nil, false, Class, Module)

    identifies an adapter to load.

Returns:

  • (.parse)

    a Module or Class that implements a .parse method for deserializing a stream of JSON objects.

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/oanda_api/streaming/json_parser.rb', line 51

def load_adapter(new_adapter)
  case new_adapter
  when String, Symbol
    load_adapter_from_string_name new_adapter.to_s
  when NilClass, FalseClass
    load_adapter default_adapter
  when Class, Module
    new_adapter
  else
    fail ::LoadError, new_adapter
  end
rescue ::LoadError => exception
  raise AdapterError.build(exception)
end

#use(new_adapter) ⇒ Object Also known as: adapter=

Loads the requested adapter. @return[.parse] a Module or Class that implements a .parse method for deserializing a stream of JSON objects.

Parameters:

  • new_adapter (nil, String, Symbol, Module, Class)

    identifies an adapter to load.



42
43
44
# File 'lib/oanda_api/streaming/json_parser.rb', line 42

def use(new_adapter)
  @adapter = load_adapter(new_adapter)
end