Class: Pytty::Daemon::Api::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/pytty/daemon/api/router.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/pytty/daemon/api/router.rb', line 10

def call(env)
  req = Rack::Request.new(env)
  resp = case req.path_info
  when "/stream"
    if env["HTTP_UPGRADE"] == "websocket"
      handler = Pytty::Daemon::Components::WebSocketHandler.new(env)
      handler.handle
    end

    [404, {"Content-Type" => "text/html"}, ["websocket only"]]
  when "/run"
    handler = Pytty::Daemon::Components::WebHandler.new(env)
    output = handler.handle

    [200, {"Content-Type" => "text/html"}, [output]]
  when "/ws"
    if env["HTTP_UPGRADE"] == "websocket"
      ws = WebSockets.new env
      ws.handle
    end

    [200, {"Content-Type" => "text/html"}, ["ws"]]
  else
    [404, {"Content-Type" => "text/html"}, ["unknown: #{req.path_info}"]]
  end

  resp
end