class Application
Represents the main Lively application middleware.
This class serves as the entry point for Lively applications, handling both standard HTTP requests for the initial page load and WebSocket connections for live updates. It integrates with the Live framework to provide real-time interactive web applications.
Definitions
def self.[](tag)
Create a new application class configured for a specific Live view tag.
Signature
-
parameter
tag
Class
The Live view class to use as the application body.
-
returns
Class
A new application class configured for the specified tag.
Implementation
def self.[](tag)
klass = Class.new(self)
klass.define_singleton_method(:resolver) do
Live::Resolver.allow(tag)
end
klass.define_method(:body) do
tag.new
end
return klass
end
def self.resolver
Get the default resolver for this application.
Signature
-
returns
Live::Resolver
A resolver configured to allow HelloWorld components.
Implementation
def self.resolver
Live::Resolver.allow(HelloWorld)
end
attr :resolver
Signature
-
attribute
Live::Resolver
The resolver for live components.
def initialize(delegate, resolver: self.class.resolver)
Initialize a new Lively application.
Signature
-
parameter
delegate
Protocol::HTTP::Middleware
The next middleware in the chain.
-
parameter
resolver
Live::Resolver
The resolver for Live components.
Implementation
def initialize(delegate, resolver: self.class.resolver)
super(delegate)
@resolver = resolver
end
attr :delegate
Signature
-
attribute
Protocol::HTTP::Middleware
The delegate middleware for request handling.
def live(connection)
Handle a WebSocket connection for live updates.
Signature
-
parameter
connection
Async::WebSocket::Connection
The WebSocket connection.
Implementation
def live(connection)
Live::Page.new(@resolver).run(connection)
end
def title
Get the title for this application.
Signature
-
returns
String
The class name of this application.
Implementation
def title
self.class.name
end
def body(...)
Create the body content for this application.
Signature
-
parameter
**options
Hash
Additional options to pass to the body constructor.
-
returns
HelloWorld
A new HelloWorld instance.
Implementation
def body(...)
HelloWorld.new(...)
end
def index(...)
Create the index page for this application.
Signature
-
parameter
**options
Hash
Additional options to pass to the index constructor.
-
returns
Pages::Index
A new index page instance.
Implementation
def index(...)
Pages::Index.new(title: self.title, body: self.body(...))
end
def handle(request, ...)
Handle a standard HTTP request.
Signature
-
parameter
request
Protocol::HTTP::Request
The incoming HTTP request.
-
parameter
**options
Hash
Additional options.
-
returns
Protocol::HTTP::Response
The HTTP response with the rendered page.
Implementation
def handle(request, ...)
return Protocol::HTTP::Response[200, [], [self.index(...).call]]
end
def call(request)
Process an incoming HTTP request.
Signature
-
parameter
request
Protocol::HTTP::Request
The incoming HTTP request.
-
returns
Protocol::HTTP::Response
The appropriate response for the request.
Implementation
def call(request)
if request.path == "/live"
return Async::WebSocket::Adapters::HTTP.open(request, &self.method(:live)) || Protocol::HTTP::Response[400]
else
return handle(request)
end
end