summaryrefslogtreecommitdiff
path: root/lib/nickserver/reel_server.rb
blob: abda57fe14388193dc2874026d4f121961d5ab4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'celluloid/autostart'
require 'reel'
require 'nickserver/adapters/celluloid_http'
require 'nickserver/request_handler'

module Nickserver
  class ReelServer < Reel::Server::HTTP

    def self.start(options = {})
      new(options[:host], options[:port]).run
    end

    def initialize(host = "127.0.0.1", port = 3000)
      super(host, port, &method(:on_connection))
    end

    def on_connection(connection)
      connection.each_request do |request|
        handler = handler_for(request)
        handler.respond_to params(request), request.headers
      end
    end


    protected

    def handler_for(request)
      RequestHandler.new(request, Nickserver::Adapters::CelluloidHttp.new)
    end

    def params(request)
      if request.query_string
        CGI.parse request.query_string
      else
        CGI.parse request.body.to_s
      end
    end

  end
end