Class: Ritm::Dispatcher

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

Overview

Keeps a list of subscribers and notifies them when requests/responses are intercepted

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

Returns a new instance of Dispatcher.



4
5
6
# File 'lib/ritm/dispatcher.rb', line 4

def initialize
  @handlers = { on_request: [], on_response: [] }
end

Instance Method Details

#add_handler(handler) ⇒ Object



8
9
10
11
# 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) ⇒ Object



21
22
23
# File 'lib/ritm/dispatcher.rb', line 21

def notify_request(request)
  notify(:on_request, request)
end

#notify_response(request, response) ⇒ Object



25
26
27
# File 'lib/ritm/dispatcher.rb', line 25

def notify_response(request, response)
  notify(:on_response, request, response)
end

#on_request(&block) ⇒ Object



13
14
15
# File 'lib/ritm/dispatcher.rb', line 13

def on_request(&block)
  @handlers[:on_request] << block
end

#on_response(&block) ⇒ Object



17
18
19
# File 'lib/ritm/dispatcher.rb', line 17

def on_response(&block)
  @handlers[:on_response] << block
end