Class: Sjs::SimpleStream
- Inherits:
-
Object
- Object
- Sjs::SimpleStream
- Defined in:
- lib/sjs/simple_stream.rb
Constant Summary collapse
- StreamError =
Class.new(::StandardError)
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
- #apply_callback(&callback) ⇒ Object
- #flush! ⇒ Object
-
#initialize(buffer = 8092) ⇒ SimpleStream
constructor
A new instance of SimpleStream.
- #reset! ⇒ Object
- #stream(raw_json) ⇒ Object
- #stream_from_url(url, timeout = 10, &callback) ⇒ Object
Constructor Details
#initialize(buffer = 8092) ⇒ SimpleStream
Returns a new instance of SimpleStream.
16 17 18 |
# File 'lib/sjs/simple_stream.rb', line 16 def initialize(buffer = 8092) @parser = ::SimpleStream.new(buffer) end |
Instance Attribute Details
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
13 14 15 |
# File 'lib/sjs/simple_stream.rb', line 13 def parser @parser end |
Instance Method Details
#apply_callback(&callback) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/sjs/simple_stream.rb', line 31 def apply_callback(&callback) parser_callback = proc do |data| callback.call(transform_to_ruby(data)) end parser.setCallback(parser_callback) end |
#flush! ⇒ Object
55 56 57 58 59 |
# File 'lib/sjs/simple_stream.rb', line 55 def flush! transform_to_ruby(parser.flush) rescue => ex raise StreamError.new(ex) end |
#reset! ⇒ Object
49 50 51 52 53 |
# File 'lib/sjs/simple_stream.rb', line 49 def reset! parser.reset rescue => ex raise StreamError.new(ex) end |
#stream(raw_json) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/sjs/simple_stream.rb', line 21 def stream(raw_json) begin entities = parser.stream(raw_json.to_s) transform_to_ruby(entities) rescue => ex raise StreamError.new(ex) end end |
#stream_from_url(url, timeout = 10, &callback) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/sjs/simple_stream.rb', line 42 def stream_from_url(url, timeout = 10, &callback) apply_callback(&callback) parser.streamFromUrl(url, timeout) rescue => ex raise StreamError.new(ex) end |