Sha256: 56bb5c2166b489c9849086847c894a024cfd4e127578051bf77a0c46608df3ff

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

module Firehose
  module Rack
    # 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.
    class App
      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

      private
      def publisher
        @publisher ||= Publisher.new
      end

      def consumer
        @consumer ||= Consumer.new
      end

      def ping
        @ping ||= Ping.new
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
firehose-1.2.8 lib/firehose/rack/app.rb
firehose-1.2.7 lib/firehose/rack/app.rb
firehose-1.2.6 lib/firehose/rack/app.rb
firehose-1.2.5 lib/firehose/rack/app.rb
firehose-1.2.4 lib/firehose/rack/app.rb
firehose-1.2.3 lib/firehose/rack/app.rb
firehose-1.2.2 lib/firehose/rack/app.rb
firehose-1.2.1 lib/firehose/rack/app.rb
firehose-1.2.0 lib/firehose/rack/app.rb