Class: Firehose::Rack::App

Inherits:
Object
  • Object
show all
Defined in:
lib/firehose/rack/app.rb

Overview

Acts as the glue between the HTTP/WebSocket world and the Firehose::Server class, which talks directly to the Redis server. Also dispatches between HTTP and WebSocket transport handlers depending on the clients’ request.

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ App

Returns a new instance of App.

Yields:

  • (_self)

Yield Parameters:



7
8
9
# File 'lib/firehose/rack/app.rb', line 7

def initialize
  yield self if block_given?
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/firehose/rack/app.rb', line 11

def call(env)
  # Cache the parsed request so we don't need to re-parse it when we pass
  # control onto another app.
  req     = env['parsed_request'] ||= ::Rack::Request.new(env)
  method  = req.request_method

  case method
  when 'PUT'
    # Firehose::Client::Publisher PUT's payloads to the server.
    publisher.call(env)
  when 'HEAD' 
    # HEAD requests are used to prevent sockets from timing out
    # from inactivity
    ping.call(env)
  else
    # TODO - 'harden' this up with a GET request and throw a "Bad Request" 
    # HTTP error code. I'd do it now but I'm in a plane and can't think of it.
    consumer.call(env)
  end
end

#consumerObject

The consumer pulls messages off of the backend and passes messages to the connected HTTP or WebSocket client. This can be configured from the initialization method of the rack app.



35
36
37
# File 'lib/firehose/rack/app.rb', line 35

def consumer
  @consumer ||= Consumer.new
end