Class: JSONSEQ::Reader
- Inherits:
-
Object
- Object
- JSONSEQ::Reader
- Defined in:
- lib/jsonseq/reader.rb
Defined Under Namespace
Classes: EndOfFile, JSONObject, MaybeTruncated, ParsingError
Constant Summary collapse
- DEFAULT_DECODER =
-> (string) { JSON.parse(string) }
Instance Attribute Summary collapse
-
#decoder ⇒ Object
readonly
Returns the value of attribute decoder.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Instance Method Summary collapse
- #each ⇒ Object
- #each_object ⇒ Object
-
#initialize(io:, decoder: DEFAULT_DECODER) ⇒ Reader
constructor
A new instance of Reader.
- #read ⇒ Object
- #read_object ⇒ Object
Constructor Details
#initialize(io:, decoder: DEFAULT_DECODER) ⇒ Reader
Returns a new instance of Reader.
39 40 41 42 |
# File 'lib/jsonseq/reader.rb', line 39 def initialize(io:, decoder: DEFAULT_DECODER) @io = io @decoder = decoder end |
Instance Attribute Details
#decoder ⇒ Object (readonly)
Returns the value of attribute decoder.
37 38 39 |
# File 'lib/jsonseq/reader.rb', line 37 def decoder @decoder end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
36 37 38 |
# File 'lib/jsonseq/reader.rb', line 36 def io @io end |
Instance Method Details
#each ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/jsonseq/reader.rb', line 67 def each if block_given? while true object = read yield object return if object.is_a?(EndOfFile) end else enum_for :each end end |
#each_object ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/jsonseq/reader.rb', line 93 def each_object if block_given? each do |value| if value.is_a?(JSONObject) yield value.object end end else enum_for :each_object end end |
#read ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jsonseq/reader.rb', line 44 def read begin source = io.readline(RS).chomp(RS) end while source == "" object = decode_string(source) case object when Numeric, TrueClass, FalseClass, nil if truncated?(source) MaybeTruncated.new(source: source, object: object) else JSONObject.new(object: object) end when ParsingError object else JSONObject.new(object: object) end rescue EOFError EndOfFile.new end |
#read_object ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/jsonseq/reader.rb', line 79 def read_object value = read case value when MaybeTruncated, ParsingError read_object when EndOfFile nil when JSONObject value.object else raise "Unexpected value: #{value}" end end |