Skip to content

Commit

Permalink
Documents MessageBus::Rack::Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Nov 28, 2018
1 parent 26d2187 commit e29a42a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/message_bus/rack/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module MessageBus::Rack; end
# delivers existing messages from the backlog and informs a
# `MessageBus::ConnectionManager` of a connection which is remaining open.
class MessageBus::Rack::Middleware
# @param [Array<MessageBus::Message>] backlog a list of messages for delivery
# @return [JSON] a JSON representation of the backlog, compliant with the
# subscriber API specification
def self.backlog_to_json(backlog)
m = backlog.map do |msg|
{
Expand All @@ -21,20 +24,30 @@ def self.backlog_to_json(backlog)
JSON.dump(m)
end

# Sets up the middleware to receive subscriber client requests and begins
# listening for messages published on the bus for re-distribution
#
# @param [Proc] app the rack app
# @param [Hash] config
# @option config [MessageBus::Instance] :message_bus (`MessageBus`) a specific instance of message_bus
def initialize(app, config = {})
@app = app
@bus = config[:message_bus] || MessageBus
@connection_manager = MessageBus::ConnectionManager.new(@bus)
start_listener
end

# Stops listening for messages on the bus
# @return [void]
def stop_listener
if @subscription
@bus.unsubscribe(&@subscription)
@started_listener = false
end
end

# Process an HTTP request from a subscriber client
# @param [Rack::Request::Env] env the request environment
def call(env)
return @app.call(env) unless env['PATH_INFO'] =~ /^\/message-bus\//

Expand Down

0 comments on commit e29a42a

Please sign in to comment.