Class: Driq::EventSource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driq = nil, timeout = 60) ⇒ EventSource

Returns a new instance of EventSource.



103
104
105
106
# File 'lib/driq.rb', line 103

def initialize(driq = nil, timeout = 60)
  @driq = driq || Driq.new
  @timeout = timeout
end

Instance Attribute Details

#driqObject (readonly)

Returns the value of attribute driq.



107
108
109
# File 'lib/driq.rb', line 107

def driq
  @driq
end

#timeoutObject

Returns the value of attribute timeout.



108
109
110
# File 'lib/driq.rb', line 108

def timeout
  @timeout
end

Instance Method Details

#build_packet(cursor, ev, value) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/driq.rb', line 130

def build_packet(cursor, ev, value)
  case ev
  when :ping
    ": #{value}\n\n"
  when nil
    "id: #{cursor}\ndata: #{value.to_json}\n\n"
  else
    "id: #{cursor}\nevent: #{ev}\ndata: #{value.to_json}\n\n"
  end
end

#comment(str = "ping") ⇒ Object



126
127
128
# File 'lib/driq.rb', line 126

def comment(str="ping")
  @driq.write([:ping, first_line(str)])
end

#event(ev, value) ⇒ Object



118
119
120
# File 'lib/driq.rb', line 118

def event(ev, value)
  @driq.write([first_line(ev), value])
end

#first_line(str) ⇒ Object



114
115
116
# File 'lib/driq.rb', line 114

def first_line(str)
  str.to_s.lines.first.chomp
end

#pingObject



122
123
124
# File 'lib/driq.rb', line 122

def ping
  comment
end

#read(cursor) ⇒ Object



141
142
143
144
# File 'lib/driq.rb', line 141

def read(cursor)
  cursor, value = @driq.read(cursor, @timeout) || [cursor, [:ping, 'keep']]
  return cursor, build_packet(cursor, value[0], value[1])
end

#write(value) ⇒ Object



110
111
112
# File 'lib/driq.rb', line 110

def write(value)
  @driq.write([nil, value])
end