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

railsconf2013-tech-demo

> 编程语言
开源

Seahorse 是一种用来描述您的 API 的方式

165 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Seahorse 是一种用来描述您的 API 的方式

Seahorse

Seahorse is a way to describe your service APIs as first-class citizens with a declarative DSL. The library also provides Ruby on Rails integration so that take advantage of your API model in controller actions.

Features

Seahorse provides the ability to define an API model, but also has functionality to support parameter validation and serialization of inputs and outputs to API calls. With Rails integration, this is automatic, namely, parameters in params are automatically type-converted and validated, outputs are automatically serialized from your API model to JSON or XML. You can also hook this up to a Sinatra app with a little amount of work.

Installing

bash
gem install seahorse

Usage

Using Seahorse in a Rails app is pretty easy!

First, define your API model and operations by creating a class like Api::Post in app/models/api/post.rb and including Seahorse::Model:

…

You can generate a JSON description of this API model by calling:

ruby
puts Api::Post.to_json

Rails Integration

In Rails, you can hook this API model up to routing by adding the following line in your config/routes.rb:

ruby
Seahorse::Model.add_all_routes(self)

This finds all API models defined and hooks them up to your Rails app. Note that in Rails, the Post API model will route to your PostsController.

Then you just write Rails code as normal. Here is what the index controller action might look like on PostsController:

ruby
class PostsController  (offset + page_size)

    respond_with(output)
  end
end

Note that params[:page] can be used without calling .to_i because Seahorse already typecasted it to an integer, since your model's input defined it as one. You no longer have to worry about typecasting values in and out. You simply take input params and call respond_with on the data you want to serialize out the data you want to display (also defined in your API model).

Here's what a create action might look like:

ruby
def create
  user = User.where(username: params[:username]).first
  respond_with user.posts.create(params[:post])
end

Note that all your parameters are automatically validated and type-converted according to the inputs you defined in your API model. You don't have to white list attributes in your model, and you don't need to define strong parameters either; Seahorse does this all for you.

Contributing

Feel free to open issues or submit pull requests with any ideas you can think of to make integrating the Seahorse model into your application an easier process. This project was initially created as a tech demo for RailsConf 2013 to illustrate some of the principles used to design the client-side SDKs at Amazon Web Services, so the current breadth of features is fairly minimal. Feature additions and extra work on the project is welcome!

License

Seahorse uses the Apache 2.0 License. See LICENSE for details.

Issues· 10 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Ruby

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

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言