Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

railsconf2013-tech-demo

> 编程语言
Open source

Seahorse is a way to describe your API

165 stars0 likes2 views
WebsiteGitHub

About

Seahorse is a way to describe your 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 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

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