百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
H

hanami-api

> 数据库
开源

HTTP APIs的最小,轻量级,最快的Ruby框架.

367 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

HTTP APIs的最小,轻量级,最快的Ruby框架.

Hanami::API

Minimal, extremely fast, lightweight Ruby framework for HTTP APIs.

Status

Contact

  • Home page: http://hanamirb.org
  • Mailing List: http://hanamirb.org/mailing-list
  • API Doc: http://rubydoc.info/gems/hanami-api
  • Bugs/Issues: https://github.com/hanami/api/issues
  • Chat: http://chat.hanamirb.org

Installation

Hanami::API supports Ruby (MRI) 3.2+

Add these lines to your application's Gemfile:

ruby
gem "hanami-api"
gem "puma" # or "webrick", or "thin", "falcon"

And then execute:

bash
$ bundle install

Or install it yourself as:

bash
$ gem install hanami-api
  • Performance
    • Runtime
    • Memory
    • Requests per second
  • Usage
    • Routes
    • HTTP methods
    • Endpoints
      • Rack endpoint
      • Block endpoint
        • String (body)
        • Enumerator (body)
        • Integer (status code)
        • Integer, String (status code, body)
        • Integer, Enumerator (status code, body)
        • Integer, Hash, String (status code, headers, body)
        • Integer, Hash, Enumerator (status code, headers, body)
    • Block context
      • env
      • status
      • headers
      • body
      • params
      • halt
      • redirect
      • back
      • json
    • Scope
    • Helpers
    • Rack Middleware
    • Streamed Responses
    • Body Parsers
  • Testing
  • Development
  • Contributing

Performance

Benchmark against an app with 10,000 routes, hitting the 10,000th to measure the worst case scenario. Based on jeremyevans/r10k, Hanami::API scores first for speed, and second for memory footprint.

Runtime

Runtime to complete 20,000 requests (lower is better).

Framework Seconds to complete
hanami-api 0.116
watts 0.235
roda 0.348
syro 0.356
rack-app 0.623
cuba 1.291
rails 17.047
sinatra 197.477

Memory

Memory footprint for 10,000 routes app (lower is better).

Framework Bytes
roda 47252
hanami-api 53988
cuba 55420
syro 60256
rack-app 82976
watts 84956
sinatra 124980
rails 143048

Requests per second

For this benchmark there are two apps for each framework: one with the root route, and one with 10,000 routes. Requests per second hitting the 1st (and only route) and the 10,000th route to measure the best and worst case scenario (higher is better).

Framework 1st route 10,000th route
hanami-api 14719.95 14290.20
watts 13912.31 12609.68
roda 13965.20 11051.27
syro 13079.12 10689.51
rack-app 10274.01 10306.46
cuba 13061.82 7084.33
rails 1345.27 303.06
sinatra 5038.74 28.14

Usage

Create config.ru at the root of your project:

ruby
# frozen_string_literal: true

require "bundler/setup"
require "hanami/api"

class App  "foo"}, "You shall not pass"]
end

It will return [401, {"X-Custom-Header" => "foo"}, ["You shall not pass"]]

Integer, Hash, Enumerator (status code, headers, body)
ruby
get "/" do
  [401, {"X-Custom-Header" => "foo"}, Enumerator.new { ... }]
end

It will return [401, {"X-Custom-Header" => "foo"}, Enumerator], see Streamed Responses

Block context

When using the block syntax there is a rich API to use.

env

The #env method exposes the Rack environment for the current request

status

Get HTTP status

ruby
get "/" do
  puts status
    # => 200
end

Set HTTP status

ruby
get "/" do
  status(201)
end

headers

Get HTTP response headers

ruby
get "/" do
  puts headers
    # => {}
end

Set HTTP status

ruby
get "/" do
  headers["X-My-Header"] = "OK"
end

body

Get HTTP response body

ruby
get "/" do
  puts body
    # => nil
end

Set HTTP response body

ruby
get "/" do
  body "Hello, world"
end

Set HTTP response body using a Streamed Response

ruby
get "/" do
  body Enumerator.new { ... }
end

params

Access params for current request

ruby
get "/" do
  id = params[:id]
  # ...
end

halt

Halts the flow of the block and immediately returns with the current HTTP status

ruby
get "/authenticate" do
  halt(401)

  # this code will never be reached
end

It sets a Rack response: [401, {}, ["Unauthorized"]]

ruby
get "/authenticate" do
  halt(401, "You shall not pass")

  # this code will never be reached
end

It sets a Rack response: [401, {}, ["You shall not pass"]]

You can also use a Streamed Response here

ruby
get "/authenticate" do
  halt(401, Enumerator.new { ... })
end

redirect

Redirects request and immediately halts it

ruby
get "/legacy" do
  redirect "/dashboard"

  # this code will never be reached
end

It sets a Rack response: [301, {"Location" => "/new"}, ["Moved Permanently"]]

ruby
get "/legacy" do
  redirect "/dashboard", 302

  # this code will never be reached
end

It sets a Rack response: [302, {"Location" => "/new"}, ["Moved"]]

back

Utility for redirect back using HTTP request header HTTP_REFERER

ruby
get "/authenticate" do
  if authenticate(env)
    redirect back
  else
    # ...
  end
end

json

Sets a JSON response for the given object

ruby
get "/user/:id" do
  user = UserRepository.new.find(params[:id])
  json(user)
end
ruby
get "/user/:id" do
  user = UserRepository.new.find(params[:id])
  json(user, "application/vnd.api+json")
end

If you want a Streamed Response

ruby
get "/users" do
  users = Enumerator.new { ... }
  json(users)
end

Scope

Prefixing routes is possible with routing scopes:

ruby
scope "api" do
  scope "v1" do
    get "/users", to: Actions::V1::Users::Index.new
  end
end

It will generate a route with "/api/v1/users" as path.

Helpers

Define helper methods available within the block context. Helper methods have access to default utilities available in block context (e.g. #halt).

Helpers can be defined inline by passing a block to the .helpers method:

ruby
require "hanami/api"

class MyAPI  "/", "REQUEST_METHOD" => "GET"})
      expect(response).to eq([200, {}, ["Hello, world"]])
    end
  end
end

Integration testing

Add this line to your application's Gemfile:

ruby
gem "rack-test", group: :test

In a test, load Rack::Test:

ruby
require "rack/test"

and then, inside your spec/test, include its helper methods:

ruby
include Rack::Test::Methods

Then you can use its methods like get and last_response, e.g.:

ruby
it "returns the status 200" do
  get "/"
  expect(last_response.status).to eq 200
end

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hanami/api.

Copyright

Copyright © 2014–2024 Hanami Team – Released under MIT License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyapibody-parsershanamihttp

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库