Class: JsonStreamTrigger

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

Constant Summary collapse

DEBUG =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJsonStreamTrigger

Returns a new instance of JsonStreamTrigger.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/json_stream_trigger.rb', line 9

def initialize()
  @parser = JSON::Stream::Parser.new
  
  @parser.start_object   &method(:start_object)
  @parser.end_object     &method(:end_object)
  @parser.start_array    &method(:start_array)
  @parser.end_array      &method(:end_array)
  @parser.key            &method(:key)
  @parser.value          &method(:value)

  @last_call      = nil
  @key_path       = ''
  @triggers       = {}
  @active_buffers = {}
  @full_buffer    = ''
end

Instance Attribute Details

#full_bufferObject (readonly)

Returns the value of attribute full_buffer


6
7
8
# File 'lib/json_stream_trigger.rb', line 6

def full_buffer
  @full_buffer
end

#key_pathObject (readonly)

Returns the value of attribute key_path


6
7
8
# File 'lib/json_stream_trigger.rb', line 6

def key_path
  @key_path
end

#triggersObject (readonly)

Returns the value of attribute triggers


6
7
8
# File 'lib/json_stream_trigger.rb', line 6

def triggers
  @triggers
end

Instance Method Details

#<<(bytes) ⇒ Object


30
31
32
33
34
# File 'lib/json_stream_trigger.rb', line 30

def <<(bytes)
  debug "bytes: #{bytes.inspect}"
  @parser << bytes
  @full_buffer << bytes if DEBUG
end

#on(pattern, &block) ⇒ Object


26
27
28
# File 'lib/json_stream_trigger.rb', line 26

def on(pattern, &block)
  @triggers[pattern] = block
end

#path_matches?(pattern) ⇒ Boolean

Returns:

  • (Boolean)

36
37
38
# File 'lib/json_stream_trigger.rb', line 36

def path_matches?(pattern)
  JsonPath.matches?(@key_path, pattern)
end