Alf::Rack
A collection of Rack middlewares for using Alf in web applications.
Links
Example: a RESTful-like interface
See the examples folder for more advanced examples.
require 'sinatra'
require 'alf-rack'
# include some helpers (see later)
include Alf::Rack::Helpers
# This middleware will open a database connection on every request.
# That connection and query methods are available through the helpers.
use Alf::Rack::Connect do |cfg|
cfg.database = ::Sequel.connect("postgres://...")
end
# Let send all suppliers, automatically use HTTP_ACCEPT to encode in
# requested format (csv, json, etc.)
get '/suppliers' do |id|
Alf::Rack::Response.new{|r|
r.body = relvar{ suppliers }
end
end
# Similar for a single supplier tuple
get '/suppliers/:id' do |id|
# Find the supplier
Alf::Rack::Response.new{|r|
r.body = tuple_extract{ restrict(suppliers, sid: id) }
end
end
Example: arbitrary queries
# As before
use Alf::Rack::Connect do |cfg|
cfg.database = ::Sequel.connect("postgres://...")
end
# Answers arbitrary queries on POST /
run Alf::Rack::Query.new