Recipe

Sinatra Primer

A fast-start guide to building Ruby web services with Sinatra and routing them through the Meridian gateway. This recipe walks through a minimal app, request handling, and wiring an LLM call into a route handler so your endpoint can answer in natural language.

1.Install and boot

Sinatra is a single-file framework. Install the gem, write a route block, and run the file. No generators, no scaffold, no config tree to learn before you ship.

# Gemfile
source 'https://rubygems.org'
gem 'sinatra'
gem 'puma'
gem 'faraday'

# app.rb
require 'sinatra'
get '/' do
  'Meridian + Sinatra ready'
end

2.Route to the gateway

Point Faraday at the Meridian endpoint and forward a prompt from query string into a chat completion. The gateway speaks OpenAI-compatible JSON, so any HTTP client works without an SDK.

require 'faraday'
require 'json'

GATEWAY = 'https://llm.getnimbus.net/v1/chat/completions'

get '/ask' do
  body = {
    model: 'azure/model-router',
    messages: [{ role: 'user', content: params[:q] }]
  }
  res = Faraday.post(GATEWAY) do |r|
    r.headers['Authorization'] = "Bearer #{ENV['MERIDIAN_KEY']}"
    r.headers['Content-Type'] = 'application/json'
    r.body = body.to_json
  end
  content_type :json
  res.body
end

3.Deploy and harden

Run under Puma for concurrency, read the API key from the environment, and add a tiny rescue block so a gateway timeout returns a clean 502 instead of a stack trace. Ship behind a reverse proxy with TLS termination and you have a production-shaped service in under fifty lines.

  • Use rackup -s puma for multi-worker boot.
  • Read secrets from ENV, never commit a key into the gem file.
  • Wrap Faraday calls in begin/rescue for upstream errors.