A Ruby gem for streaming data from the Bluesky/ATProto firehose

added documentation for Stream event handlers

+96 -9
+17
lib/skyfall/events.rb
··· 1 + module Skyfall 2 + 3 + # @private 4 + module Events 5 + protected 6 + 7 + def event_handler(name) 8 + define_method("on_#{name}") do |&block| 9 + @handlers[name.to_sym] = block 10 + end 11 + 12 + define_method("on_#{name}=") do |block| 13 + @handlers[name.to_sym] = block 14 + end 15 + end 16 + end 17 + end
+79 -9
lib/skyfall/stream.rb
··· 2 2 require 'faye/websocket' 3 3 require 'uri' 4 4 5 + require_relative 'events' 5 6 require_relative 'version' 6 7 7 8 module Skyfall ··· 14 15 # custom client for a websocket API that isn't supported yet. 15 16 16 17 class Stream 17 - EVENTS = %w(message raw_message connecting connect disconnect reconnect error timeout) 18 + extend Events 19 + 18 20 MAX_RECONNECT_INTERVAL = 300 19 21 20 22 # If enabled, the client will try to reconnect if the connection is closed unexpectedly. ··· 209 211 end 210 212 end 211 213 212 - EVENTS.each do |event| 213 - define_method "on_#{event}" do |&block| 214 - @handlers[event.to_sym] = block 215 - end 214 + 215 + # @!method on_connecting(block) 216 + # Defines a callback to be run when the client tries to open a connection to the websocket. 217 + # Can be also run as a setter `on_connecting=`. 218 + # @param [Proc] block 219 + # @yieldparam [String] url URL to which the client is connecting 220 + # @return [nil] 221 + 222 + event_handler :connecting 216 223 217 - define_method "on_#{event}=" do |block| 218 - @handlers[event.to_sym] = block 219 - end 220 - end 224 + # @!method on_connect(block) 225 + # Defines a callback to be run after a connection to the websocket is opened. 226 + # Can be also run as a setter `on_connect=`. 227 + # @param [Proc] block 228 + # @return [nil] 229 + 230 + event_handler :connect 231 + 232 + # @!method on_raw_message(block) 233 + # Defines a callback to be run when a message is received, passing a raw data packet as 234 + # received from the websocket (plain text or binary). Can be also run as a setter `on_raw_message=`. 235 + # @param [Proc] block 236 + # @yieldparam [String] data payload of the received message 237 + # @return [nil] 238 + 239 + event_handler :raw_message 240 + 241 + # @!method on_message(block) 242 + # Defines a callback to be run when a message is received, passing the message as a parsed 243 + # object of an appropriate message class. Can be also run as a setter `on_message=`. 244 + # @param [Proc] block 245 + # @yieldparam [Object] message parsed message of an appropriate class 246 + # @return [nil] 247 + 248 + event_handler :message 249 + 250 + # @!method on_disconnect(block) 251 + # Defines a callback to be run after a connection to the websocket is closed (and the client 252 + # does not reconnect). Can be also run as a setter `on_disconnect=`. 253 + # 254 + # This callback is not run when `on_reconnect` fires. 255 + # @param [Proc] block 256 + # @return [nil] 257 + 258 + event_handler :disconnect 259 + 260 + # @!method on_reconnect(block) 261 + # Defines a callback to be run when a connection to the websocket is broken, but the client 262 + # initiates or schedules a reconnect (which may happen after a delay). Can be also run as 263 + # a setter `on_reconnect=`. 264 + # @param [Proc] block 265 + # @return [nil] 266 + 267 + event_handler :reconnect 268 + 269 + # @!method on_timeout(block) 270 + # Defines a callback to be run when the heartbeat timer forces a reconnect. A reconnect is 271 + # triggered after not receiving any messages for a period of time specified in {#heartbeat_timeout} 272 + # (if {#check_heartbeat} is enabled). Can be also run as a setter `on_timeout=`. 273 + # 274 + # This callback is also followed by `on_reconnect`. 275 + # @param [Proc] block 276 + # @return [nil] 277 + 278 + event_handler :timeout 279 + 280 + # @!method on_error(block) 281 + # Defines a callback to be run when the websocket connection returns an error. Can be also 282 + # run as a setter `on_error=`. 283 + # 284 + # Default handler prints the error to stdout. 285 + # 286 + # @param [Proc] block 287 + # @yieldparam [Exception] error the received error 288 + # @return [nil] 289 + 290 + event_handler :error 221 291 222 292 223 293 # Returns a string with a representation of the object for debugging purposes.