class Ritm::Dispatcher
Keeps a list of subscribers and notifies them when requests/responses are intercepted
Public Class Methods
new()
click to toggle source
# File lib/ritm/dispatcher.rb, line 4 def initialize @handlers = { on_request: [], on_response: [] } end
Public Instance Methods
add_handler(handler)
click to toggle source
# File lib/ritm/dispatcher.rb, line 8 def add_handler(handler) on_request { |*args| handler.on_request(*args) } if handler.respond_to? :on_request on_response { |*args| handler.on_response(*args) } if handler.respond_to? :on_response end
notify_request(request)
click to toggle source
# File lib/ritm/dispatcher.rb, line 21 def notify_request(request) notify(:on_request, request) end
notify_response(request, response)
click to toggle source
# File lib/ritm/dispatcher.rb, line 25 def notify_response(request, response) notify(:on_response, request, response) end
on_request(&block)
click to toggle source
# File lib/ritm/dispatcher.rb, line 13 def on_request(&block) @handlers[:on_request] << block end
on_response(&block)
click to toggle source
# File lib/ritm/dispatcher.rb, line 17 def on_response(&block) @handlers[:on_response] << block end
Private Instance Methods
notify(event, *args)
click to toggle source
# File lib/ritm/dispatcher.rb, line 31 def notify(event, *args) @handlers[event].each do |handler| handler.call(*args) end end