The Ruby guide makes the same honest move as the other language guides: there is no nRouter Ruby gem to learn. You add the standard ruby-openai gem — the one Ruby and Rails teams already reach for — and point it at the gateway.
Disclosure: I work on nRouter. The guide is here: https://nrouter.ai/docs/sdks/ruby
Two lines of configuration
Add the gem:
gem "ruby-openai"
Then build the client with the gateway as its uri_base and your virtual key from the environment:
client = OpenAI::Client.new(
access_token: ENV.fetch("NROUTER_API_KEY"),
uri_base: "https://api.nrouter.ai/v1",
)
Calls are plain ruby-openai: client.chat(parameters: { model: ..., messages: [...] }), and the response is a plain hash you read with response.dig("choices", 0, "message", "content"). No wrapper objects, no new API to memorise — the gem you know, a different front door.
Streaming is just a proc
Ruby's block culture shows up nicely here. Streaming is not a separate client or a callback registration — it is a stream: parameter holding a proc that receives each chunk:
client.chat(
parameters: {
model: "claude-haiku-4-5-20251001",
messages: [{ role: "user", content: "Write a short poem." }],
stream: proc { |chunk| print chunk.dig("choices", 0, "delta", "content") }
}
)
Each chunk is the same hash shape as the full response, so the dig you use for a completed reply works for deltas too.
Errors are Faraday errors
Because ruby-openai rides on Faraday, gateway refusals surface as Faraday exceptions, and the guide rescues them separately: Faraday::ClientError carries the response status and body (e.response[:status], e.response[:body]) for 4xx refusals — a rejected key, an exhausted budget, a rate limit — while Faraday::ServerError covers the 5xx side. If your Rails app already has Faraday error handling, gateway errors slot into the machinery you already have.
Why this shape matters
A gateway earns its place in the routing, the budget ceilings, the server-side guardrails, and the receipts — the guide inspects the gateway's x-nr-* response headers for cost and latency. None of that needs a proprietary gem in your Gemfile. The integration is the configuration: key in the environment, uri_base at the gateway, everything else unchanged.
Top comments (0)