class Application
Represents the main Presently application middleware.
Handles routing for the display view (/), presenter view (/presenter),
and WebSocket connections (/live). Creates a shared class Presently::PresentationController
that keeps all connected clients in sync.
Definitions
def initialize(delegate, slides_root: "slides", templates_roots: [])
Initialize a new Presently application.
Signature
-
parameter
delegateProtocol::HTTP::Middleware The next middleware in the chain.
-
parameter
slides_rootString The directory containing slide files.
-
parameter
templates_rootsArray(String) Additional directories to search for templates.
Implementation
def initialize(delegate, slides_root: "slides", templates_roots: [])
@slides_root = slides_root
@templates_roots = templates_roots
super(delegate)
end
def allowed_views
The view classes that this application allows.
Signature
-
returns
Array(Class) The allowed view classes.
Implementation
def allowed_views
[DisplayView, PresenterView]
end
def state
The shared state passed to all views via the resolver.
Signature
-
returns
Hash The controller as keyword state.
Implementation
def state
{controller: controller}
end
def controller
The shared presentation controller.
Signature
-
returns
PresentationController The controller instance.
Implementation
def controller
@controller ||= begin
templates = Templates.for(@templates_roots)
presentation = Presentation.load(@slides_root, templates: templates)
PresentationController.new(presentation, state: State.new)
end
end
def title
The application title shown in the browser.
Signature
-
returns
String The page title.
Implementation
def title
"Presently"
end
def body(request)
Create the body view for the given request path.
Signature
-
parameter
requestProtocol::HTTP::Request The incoming request.
-
returns
Live::View | Nil The view for the path, or
nilfor unknown paths.
Implementation
def body(request)
case request.path
when "/"
DisplayView.new(controller: controller)
when "/presenter"
PresenterView.new(controller: controller)
end
end
def handle(request)
Handle an HTTP request by rendering the appropriate page.
Signature
-
parameter
requestProtocol::HTTP::Request The incoming request.
-
returns
Protocol::HTTP::Response The HTTP response.
Implementation
def handle(request)
if body = self.body(request)
page = Page.new(title: title, body: body)
return Protocol::HTTP::Response[200, [], [page.call]]
else
return Protocol::HTTP::Response[404, [], ["Not Found"]]
end
end