Class: Conjur::EventSource
- Inherits:
-
Object
- Object
- Conjur::EventSource
- 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
-
#json ⇒ Boolean
(also: #json?)
(true) Whether to parse event’s data field as JSON.
-
#last_event_id ⇒ Object
readonly
Returns the value of attribute last_event_id.
-
#retry ⇒ Object
readonly
Returns the value of attribute retry.
Instance Method Summary collapse
-
#feed(chunk) ⇒ Object
Feed a chunk of data to the EventSource and dispatch any fully receieved events.
-
#initialize ⇒ EventSource
constructor
A new instance of EventSource.
-
#message(&block) ⇒ Object
Listens to all messages.
-
#on(name, &block) ⇒ Object
Adds a listener for :name:.
Constructor Details
#initialize ⇒ EventSource
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
#json ⇒ Boolean Also known as: json?
Returns (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_id ⇒ Object (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 |
#retry ⇒ Object (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.
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 &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 |