Module: OandaAPI::Streaming::JsonParser
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
-
#adapter ⇒ .parse
Loads (if not already loaded) and returns the current adapter class.
-
#default_adapter ⇒ Symbol
Returns a symbol identifying either the currently loaded adapter or one that can be loaded.
-
#load_adapter(new_adapter) ⇒ .parse
Loads the requested adapter.
-
#use(new_adapter) ⇒ Object
(also: #adapter=)
Loads the requested adapter.
Instance Method Details
#adapter ⇒ .parse
Loads (if not already loaded) and returns the current adapter class.
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_adapter ⇒ Symbol
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.
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.
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.
42 43 44 |
# File 'lib/oanda_api/streaming/json_parser.rb', line 42 def use(new_adapter) @adapter = load_adapter(new_adapter) end |