Nodes
Table of contents
- Observing routes
- Responding
- Observing + Responding
- Return Types
- Empty
- String
- JSON
- Response
- RBX
- RBX + Antlers
- Redirect
- Two ways to respond
- Cookies
- Calling other code
- Calling a class
- Injecting a dependency
- Arguments
- Route level args
- Render level args
- Intervals
- Parallelism [UNRELEASED]
- Siblings
- Children
- CRUD
- Single Responsibility
- Classic Controller
- Unit Testing
Nodes are the flexible building blocks of your application. They can respond to a route request, or they can be called by another node. They can render a return value, or they can create an event. They are designed to be specific enough to observe events and return values, but generic enough to be split up to represent a complex application with its own patterns and structure.
Nodes can render HTML/JSON directly from the Ruby class (via RBX, similar to JSX) and render other nodes into the output using the syntax; <html><{ ChildNode }></html>.
Observing routes
After setting up a route, observe it to render a response:
class UserNode < LowNode
observe '/:user_id'
def render(event: RouteEvent)
event.params[:user_id] # => '123'
end
end
The observe '/path' syntax is the simplest way to respond to a request. It observes a route and calls the render method... or the receive method if a body was sent in the original request. Constrain the HTTP Verbs via the route type.
The actions are split up this way so that you can have both receiving and responding methods in the same file, and... it just feels rightâ„¢... to send and receive. To be, or not to be, that is the question: Whether 'tis nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles.
For all you HTTP nerds, you can have more flexibility with the syntax:
observe '/path' => :action
The HTTP request/verb becomes the corresponding event/action:
| HTTP Verb | Event | Action |
|---|---|---|
GET |
RouteEvent |
render/get |
QUERY |
ReceiveEvent |
receive/query |
POST |
ReceiveEvent |
receive/post |
PUT |
ReceiveEvent |
receive/put |
PATCH |
ReceiveEvent |
receive/patch |
DELETE |
RouteEvent |
render/delete |
The event will call render/receive first before falling back to an action matching the HTTP verb, when the node does include Controller. Requests with a body are given a ReceiveEvent and call receive, requests without one are given a RouteEvent and call render.
Tip
A ReceiveEvent is just like a RouteEvent except it also has a body attribute. Form and JSON bodies become a hash with symbol keys, other bodies are a string. raw_body is always the string.
For example, a POST request to the '/feedback' route will call the post action/method:
class FormNode < LowNode
observe '/feedback' => :post
include Controller
def post(event: ReceiveEvent)
submit(event.body)
Status[200]
end
end
Note
If no method matches the event's action then nothing happens, the observer returns nil. You get nothing! You lose! Good day, sir! You stole Fizzy Lifting Drinks! You bumped into the ceiling which now has to be washed and sterilized. Raindeer will move on to the next observer.
See also: Observers
Responding
Every node supports an initialize and a render method. The class is initialized first before then being rendered. You can access any instance variables or methods from the render method:
class UserNode < LowNode
observe '/:id'
# Business logic.
def initialize(event: RouteEvent)
@id = event.params[:id]
end
# Templating.
def render
<strong>ID:</strong> {@id}
end
end
Nodes rendered inside another node, like <{ UserCard }>, are given a RenderEvent that has the same params as the route:
class UserCard < LowNode
def initialize(event: RenderEvent)
@id = event.params[:id]
end
end
Observing + Responding
With the "on" syntax and the Route we can observe and respond in one fell swoop:
on Route[GET => '/'] do |request_event|
"Response"
end
Return Types
Empty
As hinted to by the elaborate note above, if our node method returns nil or '' and there are no other observers then the router will default to a 404 response.
class MyNode < LowNode
def render
nil
end
end
String
class MyNode < LowNode
def render
"Hello"
end
end
JSON
A returned Hash or Array is sent as JSON, with the application/json content type.
class MyNode < LowNode
def render
{
error: "This framework is too awesome, burns my eyes."
}
end
end
Response
Return a Protocol::HTTP::Response you made yourself and it's sent as it is, with its status, headers and body. Response[] takes its headers as a hash:
class FeedNode < LowNode
def render
Protocol::HTTP::Response[200, { 'content-type' => 'application/rss+xml', 'cache-control' => 'public, max-age=3600' }, [Feed.xml]]
end
end
RBX
RBX is just HTML inside Ruby. Use .rbx as your file extension and now you can place HTML inside of render:
class MyNode < LowNode
def render
<strong>Hello</strong>
end
end
RBX + Antlers
Antlers allows you to render nodes within nodes:
class ParentNode < LowNode
def render
<html><{ ChildNode }></html>
end
end
class ChildNode < LowNode
def render
<strong>Hello</strong>
end
end
Which outputs:
<html><strong>Hello</strong></html>
See also: Templating
Redirect
Return redirect to send the browser to another address, with a 303 so it gets the address after a form:
class FeedbackNode < LowNode
include Controller
def post(event: ReceiveEvent)
submit(event.body)
redirect('/thanks')
end
end
Two ways to respond
A node can respond in two styles, and you can use whichever suits it:
- Functional: call
add_headerandset_cookieas you go, then return a literal, like a string, a hash or an array. The node sends the headers with it. - Object oriented: build a
Protocol::HTTP::Responsewith its status, headers and body, and return that. Headers you added withadd_headerorset_cookieare added to it too.
# Functional: add the header, then return a hash, which is sent as JSON.
class SearchIndexNode < LowNode
def render
add_header('cache-control', 'public, max-age=3600')
{ recipes: Recipe.titles }
end
end
# Object oriented: return the whole response, with its headers given when it's made.
class SearchIndexNode < LowNode
def render
Protocol::HTTP::Response[200, { 'content-type' => 'application/json', 'cache-control' => 'public, max-age=3600' }, [JSON.generate(recipes: Recipe.titles)]]
end
end
The functional style keeps a node short, as Raindeer turns what it returns into a response. The object oriented style gives full control, like a status or content type of its own.
Cookies
Read the cookies a request sent with event.cookies, and set one with set_cookie, which is HttpOnly, Secure and SameSite=Lax unless you say otherwise:
class ThemeNode < LowNode
def initialize(event:)
@theme = event.cookies['theme'] || 'light'
set_cookie('theme', @theme, max_age: 365 * 24 * 60 * 60, http_only: false)
end
end
Expire a cookie with max_age: 0, and add any other header with add_header('cache-control', 'no-store').
Calling other code
Nodes are designed for intercepting the request-response layer and then handing off control to your domain-specific models, presenters and business logic.
Tip
When to use a node? If you're dealing with a route or responding with HTML then use a node. If not then you don't have to use one.
Calling a class
All classes in /app are autoloaded so you can call any class from any node:
# /app/business_directory/business_logic.rb
class BusinessLogic
def self.metrics(company_id:)
# Calculate a bunch of business metrics.
end
end
# /app/business_directory/business_directory.rbx
class BusinessDirectory < LowNode
observe '/:company_id'
def initialize(event: RouteEvent)
company_id = event.params[:company_id]
@metrics = BusinessLogic.metrics(company_id:)
end
def render
<{ Metrics metrics=@metrics }>
end
end
Injecting a dependency
See: Dependencies
Arguments
Note
Node actions/methods called via observers/events have an omittable event: argument
Route level args
An event keyword argument is optionally available to all initialize and render arguments. A node that observes a route is given the RouteEvent:
class UserNode < LowNode
observe '/:user_id'
def initialize(event: RouteEvent)
event.params[:user_id] # => '123'
end
def render(event: RouteEvent)
event.params[:user_id] # => '123'
end
end
event.path is the address the request came to, like /123, without its query, and event.query its query as a hash. event.route.path is the route it matched, like /:user_id.
Render level args
If the node has been rendered by another node then any props passed to that node are available as keyword arguments in the node's initialize or render methods.
Passing props:
<{ MyNode my_var='Yes' }>
Receiving args:
class MyNode < LowNode
def render(my_var:)
<strong>{my_var}</strong>
end
end
Outputs:
<strong>Yes</strong>
Intervals
A node can run code on a schedule. Observe an Interval and define tick:
class CleanupNode < LowNode
observe Interval[every_minute: 5]
def tick
Sessions.delete_expired
end
end
Count in every_second, every_minute, every_hour or every_day. Add at_hour: to only run during one hour of the day:
observe Interval[every_minute: 3, at_hour: 2] # Every 3 minutes from 2am to 3am.
Ticks run on a thread of their own. So a slow job never holds up a request. Jobs run one at a time. A job that takes longer than its interval skips the turns it missed. It doesn't run twice at once.
What tick returns is ignored. Nothing responds to an interval.
Note
A job runs on a different thread from requests. Anything it shares with requests must be safe to use from two threads at once. A Thread::Queue is a good way to hand work over.
For example a node can draw images ahead of time and keep them ready for requests:
class ImagePool < LowNode
observe Interval[every_second: 1]
READY = Thread::Queue.new
# A request takes a ready image or nil when there are none.
def self.take
READY.pop(true)
rescue ThreadError
nil
end
def tick
(5 - READY.size).times { READY << Image.draw }
end
end
Parallelism [UNRELEASED]
Speed up CPU-bound work with parallelism. Thanks to the immutable nature of nodes we can process them in parallel using the <{ parallelize: }> syntax. Your data must be immutable or be able to be copied by Raindeer. This means reducing your reliance on global state, such as global dependency injection via Providers, in favour of local dependency injection via Plugs.
Note
IO-bound work like database queries are essentially parallel already, as multiple asynchronous requests will simultaneously wait for database results. However the CPU-bound processing of these results is not parallelized, so you'll still see a speed up... depending on what you're doing.
Siblings
def render
<{ parallelize: }>
# For Loop executed at the same time as UserNode.
<{ for: user in: @users }>
<{ UserNode user=user }>
<{ :for }>
# PostsNode executed at the same time as For Loop.
<{ PostsNode }>
<{ :parallelize }>
end
Children
def render
# Each item in the loop is executed at the same time.
<{ for: user in: @users :parallelize }>
<{ UserNode user=user }>
<{ :for }>
end
CRUD
CRUD (Create, Read, Update, Delete) is pretty standard in web applications. Raindeer replaces controllers and actions with events and actions. Instead of a central controller for disparate create, read, update and delete actions, you observe the appropriate route with a standalone node for each event/action that happens to that route.
At first this may seem like more boilerplate for each action... but consider that in a controller each action usually becomes bloated over time, with either fat controllers or fat models. This way you are forced to focus on "single-responsiblity" from the start. Over time your node will be able to handle increasingly complexity gracefully.
Tip
In Raindeer the node is the controller, the service object and the template all-in-one.
Single Responsibility
Route:
Raindeer.router do
put '/:user_id/update'
end
Node:
class UserUpdater < LowNode
observe '/:user_id/update'
def receive(event: ReceiveEvent)
UsersData.update(event.params[:user_id], event.body[:attributes])
Status[200]
end
end
Data:
class UsersData < LowData
def update(id, attributes) = Users(id:)[] = attributes
end
Classic Controller
Here's how to replicate a controller one-to-one in Raindeer:
Route:
Raindeer.router do
route [GET, POST, PUT, DELETE] => '/:user_id'
end
Node:
class UserController < LowNode
observe '/:user_id'
include Controller
def get(event: RouteEvent)
UserNode.render(event:, user: UsersData.find(event.params[:user_id]))
end
def post(event: ReceiveEvent)
UsersData.create(event.params[:user_id], event.body[:attributes])
Status[200]
end
def put(event: ReceiveEvent)
UsersData.update(event.params[:user_id], event.body[:attributes])
Status[200]
end
def delete(event: RouteEvent)
UsersData.delete(event.params[:user_id])
Status[200]
end
end
Data:
class UsersData < LowData
def find(id) = Users(id:)[:username, :email].to_a.first
def create(id, attributes) = Users << { **attributes, id: }
def update(id, attributes) = Users(id:)[] = attributes
def delete(id) = Users(id:).delete
end
Unit Testing
Note
Nodes use the Method Factory pattern. You call the class method to call the instance method. This works for every action: render, receive and side_effect, plus the HTTP verbs get, query, post, put, patch and delete when the node does include Controller.
Instead of calling new on a node class directly, first you call a class method which instantiates the class on your behalf, then calls the corresponding instance method.
Say your class looks like this:
class ListNode < LowNode
def render
<ul>...</ul>
end
end
To call the #render instance method you first call the .render class method:
RSpec.describe ListNode do
describe '#render' do
it 'renders a list' do
expect(subject.render).to eq('<ul>...</ul>')
end
end
end