Tina4 Ruby – Quick Reference
🔥 Hot Tips
- Routes go in
routes/, templates intemplates/, static files inpublic/ - GET routes are public by default; use
secure_get,secure_postetc. to require auth - Return a
HashorArrayfrom a route block and it auto-detects as JSON - Run
tina4 startto launch the dev server on port 7145
Installation
gem install tina4
tina4 init my-project
cd my-project
bundle install
tina4 startMore details around project setup and customizations.
Static Websites
Put .twig files in ./templates • assets in ./public
<!-- templates/index.twig -->
<h1>Hello Static World</h1>More details on static website routing.
Basic Routing
require "tina4"
Tina4.get "/" do |request, response|
response.html "<h1>Hello Tina4 Ruby</h1>"
end
# POST requires Bearer auth by default
Tina4.post "/api" do |request, response|
response.json({ data: request.params })
end
# Redirect after post
Tina4.post "/register" do |request, response|
response.redirect "/welcome"
endFollow the links for basic routing and dynamic routing with variables.
Middleware
Tina4.before "/api" do |request, response|
# Runs before any route matching /api*
Tina4::Debug.info("API request: #{request.path}")
end
Tina4.after do |request, response|
# Runs after every route
response.headers["X-Powered-By"] = "Tina4 Ruby"
endFollow the links for more on Middleware Declaration and Pattern Matching.
Template Rendering
Put .twig files in ./templates • assets in ./public
<!-- templates/index.twig -->
<h1>Hello {{ name }}</h1>Tina4.get "/" do |request, response|
response.render("index.twig", { name: "World!" })
endSessions
The default session handler is FileHandler. Override SESSION_HANDLER in .env.
| Handler | Backend | Required gem |
|---|---|---|
:file (default) | File system | — |
:redis | Redis | redis |
:mongo | MongoDB | mongo |
Tina4.get "/session/set" do |request, response|
request.session["name"] = "Joe"
request.session["info"] = { list: ["one", "two", "three"] }
response.text "Session Set!"
end
Tina4.get "/session/get" do |request, response|
name = request.session["name"]
info = request.session["info"]
response.json({ name: name, info: info })
endSCSS Stylesheets
Drop in ./src/scss → auto-compiled to ./public/css
// src/scss/main.scss
$primary: #2c3e50;
body {
background: $primary;
color: white;
}More details on CSS and SCSS.
Environments
Default development environment can be found in .env
PROJECT_NAME="My Project"
VERSION=1.0.0
TINA4_DEBUG_LEVEL=ALL
API_KEY=ABC1234
DATABASE_URL=sqlite3:data.dbapi_key = ENV["API_KEY"] || "ABC1234"Authentication
Pass Authorization: Bearer <token> to secured routes. JWT RS256 keys auto-generated in .keys/.
# Public route
Tina4.get "/login" do |request, response|
token = Tina4::Auth.generate_token({ "user_id" => 1 })
response.json({ token: token })
end
# Secured route (requires Bearer token)
Tina4.secure_get "/profile" do |request, response|
response.json({ message: "Welcome!" })
endHTML Forms and Tokens
<form method="POST" action="/register">
<input name="email">
<button>Save</button>
</form>More details on posting form data.
AJAX and tina4helper.js
Tina4 ships with a small javascript library to assist with AJAX calls.
More details on available features.
OpenAPI and Swagger UI
Visit http://localhost:7145/swagger
Tina4.get "/users", swagger_meta: { description: "Get all users" } do |request, response|
response.json({ users: [] })
endFollow the links for more on Configuration, Usage and Metadata.
Databases
# db = Tina4::Database.new("<connection_string>")
db = Tina4::Database.new("sqlite3:data.db")Follow the links for more on Available Connections, Core Methods, Usage and Transactions.
Database Results
result = db.fetch("SELECT * FROM users", [], limit: 3, skip: 1)
array = result.to_a # Array of hashes
json = result.to_json # JSON string
csv = result.to_csv # CSV stringLooking at detailed Usage will improve deeper understanding.
Migrations
tina4 migrate --create create_users_table-- migrations/20260313120000_create_users_table.sql
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);tina4 migrateMigrations have rollback support and status tracking.
ORM
class User < Tina4::ORM
integer_field :id, primary_key: true, auto_increment: true
string_field :name
string_field :email
end
User.create(name: "Alice", email: "alice@example.com")
user = User.find(1)
user.name = "Alice Wonder"
user.saveORM functionality is extensive — see the Advanced Detail for the full picture.
CRUD
Tina4.get "/users/dashboard" do |request, response|
users = User.all
response.render("users/dashboard.twig", { users: users })
endMore details on how CRUD works.
Consuming REST APIs
api = Tina4::API.new("https://api.example.com", auth_header: "Bearer xyz")
result = api.get("/users/42")
puts result.bodyMore details are available on POST bodies, authorization headers, and API responses.
Inline Testing
Tina4.describe "Math operations" do
it "adds numbers" do
assert_equal 4, 2 + 2
end
endRun: tina4 test
Websockets
Tina4.get "/ws/chat" do |request, response|
ws = Tina4::Websocket.new(request)
ws.on_message do |data|
ws.send("Echo: #{data}")
end
ws.start
endQueues
Supports litequeue (default/SQLite), RabbitMQ, and Kafka backends.
queue = Tina4::Queue.new(topic: "emails")
Tina4::Producer.new(queue).produce({ to: "alice@example.com", subject: "Welcome" })
consumer = Tina4::Consumer.new(queue)
consumer.each do |msg|
puts msg.data
endWSDL
class Calculator < Tina4::WSDL
service_url "http://localhost:7145/calculator"
def add(a, b)
{ result: a + b }
end
endLocalization (i18n)
Set TINA4_LANGUAGE in .env to change framework language.
puts Tina4.t("server_stopped") # "Server stopped." (en)