Class: Conjur::EventSource

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

Overview

An EventSource instance is used to parse a stream in the format given by the Server Sent Events standard: www.whatwg.org/specs/web-apps/current-work/#server-sent-events

Defined Under Namespace

Classes: Event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventSource

Returns a new instance of EventSource.



23
24
25
26
27
28
# File 'lib/conjur/event_source.rb', line 23

def initialize
  @json   = true
  @on     = {}
  @all    = []
  @buffer = ""
end

Instance Attribute Details

#jsonBoolean Also known as: json?

Returns (true) Whether to parse event’s data field as JSON.

Returns:

  • (Boolean)

    (true) Whether to parse event’s data field as JSON.



20
21
22
# File 'lib/conjur/event_source.rb', line 20

def json
  @json
end

#last_event_idObject (readonly)

Returns the value of attribute last_event_id.



16
17
18
# File 'lib/conjur/event_source.rb', line 16

def last_event_id
  @last_event_id
end

#retryObject (readonly)

Returns the value of attribute retry.



11
12
13
# File 'lib/conjur/event_source.rb', line 11

def retry
  @retry
end

Instance Method Details

#feed(chunk) ⇒ Object

Feed a chunk of data to the EventSource and dispatch any fully receieved events.

Parameters:

  • chunk (String)

    the data to parse



33
34
35
36
37
38
39
# File 'lib/conjur/event_source.rb', line 33

def feed chunk
  @buffer << chunk

  while i = @buffer.index("\n\n")
    process_event @buffer.slice!(0..i)
  end
end

#message(&block) ⇒ Object

Listens to all messages



47
48
49
# File 'lib/conjur/event_source.rb', line 47

def message &block
  @all << block
end

#on(name, &block) ⇒ Object

Adds a listener for :name:



42
43
44
# File 'lib/conjur/event_source.rb', line 42

def on name, &block
  (@on[name.to_sym] ||= []) << block
end