Class: Supabase::Realtime::TestSocket
- Inherits:
-
Object
- Object
- Supabase::Realtime::TestSocket
- Includes:
- Socket
- Defined in:
- lib/supabase/realtime/test_socket.rb
Overview
In-memory Socket implementation for specs and local prototyping. Captures every frame the client sends in ‘sent_frames`, and exposes `inject(frame)` so a test can simulate a server response.
Not intended for production use — bring a real WebSocket adapter for that.
Instance Attribute Summary collapse
-
#sent_frames ⇒ Object
readonly
Returns the value of attribute sent_frames.
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize ⇒ TestSocket
constructor
A new instance of TestSocket.
-
#inject(frame) ⇒ Object
Push a JSON frame as if it came from the server.
-
#last_sent_frame ⇒ Object
Convenience: the last frame the client pushed, parsed back to a Hash.
- #reset_sent_frames ⇒ Object
- #send(payload) ⇒ Object
- #sent_events ⇒ Object
Methods included from Socket
#close_callbacks, #error_callbacks, #message_callbacks, #on_close, #on_error, #on_message, #on_open, #open_callbacks
Constructor Details
#initialize ⇒ TestSocket
Returns a new instance of TestSocket.
17 18 19 20 |
# File 'lib/supabase/realtime/test_socket.rb', line 17 def initialize @connected = false @sent_frames = [] end |
Instance Attribute Details
#sent_frames ⇒ Object (readonly)
Returns the value of attribute sent_frames.
15 16 17 |
# File 'lib/supabase/realtime/test_socket.rb', line 15 def sent_frames @sent_frames end |
Instance Method Details
#close ⇒ Object
27 28 29 30 |
# File 'lib/supabase/realtime/test_socket.rb', line 27 def close @connected = false close_callbacks.each(&:call) end |
#connect ⇒ Object
22 23 24 25 |
# File 'lib/supabase/realtime/test_socket.rb', line 22 def connect @connected = true open_callbacks.each(&:call) end |
#connected? ⇒ Boolean
36 37 38 |
# File 'lib/supabase/realtime/test_socket.rb', line 36 def connected? @connected end |
#inject(frame) ⇒ Object
Push a JSON frame as if it came from the server. Accepts a JSON String or a Hash (which gets JSON-encoded for you).
44 45 46 47 |
# File 'lib/supabase/realtime/test_socket.rb', line 44 def inject(frame) raw = frame.is_a?(String) ? frame : JSON.generate(frame) .each { |cb| cb.call(raw) } end |
#last_sent_frame ⇒ Object
Convenience: the last frame the client pushed, parsed back to a Hash.
50 51 52 53 54 |
# File 'lib/supabase/realtime/test_socket.rb', line 50 def last_sent_frame return nil if @sent_frames.empty? JSON.parse(@sent_frames.last) end |
#reset_sent_frames ⇒ Object
60 61 62 |
# File 'lib/supabase/realtime/test_socket.rb', line 60 def reset_sent_frames @sent_frames = [] end |
#send(payload) ⇒ Object
32 33 34 |
# File 'lib/supabase/realtime/test_socket.rb', line 32 def send(payload) @sent_frames << payload end |
#sent_events ⇒ Object
56 57 58 |
# File 'lib/supabase/realtime/test_socket.rb', line 56 def sent_events @sent_frames.map { |f| JSON.parse(f)["event"] } end |