Class: Sjs::SimpleStream

Inherits:
Object
  • Object
show all
Defined in:
lib/sjs/simple_stream.rb

Constant Summary collapse

StreamError =
Class.new(::StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer = 8092) ⇒ SimpleStream

Returns a new instance of SimpleStream.

Parameters:

  • buffer (Integer) (defaults to: 8092)

    buffer amount until json is parsed



16
17
18
# File 'lib/sjs/simple_stream.rb', line 16

def initialize(buffer = 8092)
  @parser = ::SimpleStream.new(buffer)
end

Instance Attribute Details

#parserObject (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

Parameters:

  • callback (Block)

    Block that will yield the parsed entities



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

Parameters:

  • raw_json (String)

    raw_json



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

Parameters:

  • url (String)
  • timeout (Integer) (defaults to: 10)

    seconds of inactivitiy until connection times out, defaults to 10.

  • callback (Block)

    Block that will yield the parsed entities



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