Class: Transit::Reader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/transit/reader.rb

Overview

Transit::Reader converts incoming transit data into appropriate values/objects in Ruby.

Instance Method Summary collapse

Constructor Details

#initialize(format, io, opts = {}) ⇒ Reader

Creates a new Reader configured to read from io, expecting format (:json, :msgpack).

Use opts to register custom read handlers, associating each one with its tag.

Examples:


json_reader                 = Transit::Reader.new(:json, io)
# ^^ reads both :json and :json_verbose formats ^^
msgpack_writer              = Transit::Reader.new(:msgpack, io)
writer_with_custom_handlers = Transit::Reader.new(:json, io,
  :handlers => {"point" => PointReadHandler})

Parameters:

  • format (Symbol)

    required any of :msgpack, :json, :json_verbose

  • io (IO)

    required

  • opts (Hash) (defaults to: {})

    optional

See Also:



56
57
58
59
60
61
62
63
# File 'lib/transit/reader.rb', line 56

def initialize(format, io, opts={})
  @reader = case format
            when :json, :json_verbose
              Unmarshaler::Json.new(io, opts)
            else
              Unmarshaler::MessagePack.new(io, opts)
            end
end

Instance Method Details

#readObject

Reads transit values from an IO (file, stream, etc), and converts each one to the appropriate Ruby object.

With a block, yields each object to the block as it is processed.

Without a block, returns a single object.

Examples:

reader = Transit::Reader.new(:json, io)
reader.read {|obj| do_something_with(obj)}

reader = Transit::Reader.new(:json, io)
obj = reader.read


36
# File 'lib/transit/reader.rb', line 36

def_delegators :@reader, :read