Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/crepe/crepe
π₯ The thin API stack.
https://github.com/crepe/crepe
api fast lightweight rack ruby
Last synced: 3 months ago
JSON representation
π₯ The thin API stack.
- Host: GitHub
- URL: https://github.com/crepe/crepe
- Owner: crepe
- Created: 2012-08-06T23:53:04.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2017-12-10T05:57:23.000Z (about 7 years ago)
- Last Synced: 2024-07-31T17:23:26.534Z (6 months ago)
- Topics: api, fast, lightweight, rack, ruby
- Language: Ruby
- Homepage:
- Size: 432 KB
- Stars: 128
- Watchers: 10
- Forks: 5
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-ruby - Crepe - The thin API stack. (API Builder and Discovery)
README
# Crepe [![Build Status][1]][2] [![Code Climate][3]][4]
Crepe is a lightweight API framework designed to help you write clean, fast web
services in Ruby. With an elegant and intuitive DSL inspired by RSpec, and with
a nod to Grape, Crepe makes API design simple.[1]: https://img.shields.io/travis/crepe/crepe.svg?style=flat
[2]: https://travis-ci.org/crepe/crepe
[3]: https://img.shields.io/codeclimate/maintainability/crepe/crepe.svg?style=flat&label=code%20climate
[4]: https://codeclimate.com/github/crepe/crepe## Installation
In your application's Gemfile:
```ruby
gem 'crepe', github: 'crepe/crepe'
```If you're coming from Rails and/or you want a Crepe application with a
thought-out file structure, you can use [creperie][creperie] to generate a new
API:```bash
$ gem install creperie --pre
$ crepe new my_api
```[creperie]: https://github.com/crepe/creperie
## Usage
Crepe APIs are, at their core, Rack applications. They can be created by
subclassing `Crepe::API`. To detail Crepe's major features, we'll show how
[GitHub's Gist API][gist-api] could be written using Crepe:[gist-api]: https://developer.github.com/v3/gists/
```ruby
# config.ru
require 'bundler/setup'
require 'crepe'module Gist
class API < Crepe::API
# Like `let` in RSpec, this defines a lazy-loading helper.
let(:current_user) { User.authorize!(request.headers['Authorization']) }namespace :gists do
let(:gist_params) { params.slice(:files, :description, :public) }get do
begin
current_user.gists.limit(30)
rescue User::Unauthorized
Gist.limit(30)
end
endpost { Gist.create(gist_params) }
get(:public) { Gist.limit(30) }
get(:starred) { current_user.starred_gists }
# Specify a parameter as part of the namespace: /gists/:id/...
param :id do
let(:gist) { Gist.find(params[:id]) }get { gist }
put { gist.update_attributes(gist_params) }
patch { gist.update_attributes(gist_params) }
delete do
if gist.user == current_user
gist.destroy and head :no_content
else
error! :unauthorized
end
end# Specify a parameter constraint, e.g. a 40-character hex string
param(sha: /\h{40}/) { gist.revisions.find(params[:sha]) }
get(:commits) { gist.commits.limit(30) }get :star do
if current_user.starred?(gist)
head :no_content
else
head :not_found
end
end
put(:star) { current_user.star(gist) }
delete(:star) { current_user.unstar(gist) }get(:forks) { gist.forks.limit(30) }
post(:forks) { current_user.fork(gist) }
end
endrescue_from ActiveRecord::RecordNotFound do |e|
error! :not_found, e.message
endrescue_from ActiveRecord::InvalidRecord do |e|
error! :unprocessable_entity, e.message, errors: e.record.errors
endrescue_from User::Unauthorized do |e|
unauthorized! realm: 'Gist API'
end
end
endrun Gist::API
```The above example will give you a Rack application that you can run with the
`rackup` command, responding to the following routes:```
GET /gists
POST /gists
GET /gists/public
GET /gists/starred
GET /gists/:id
PUT /gists/:id
PATCH /gists/:id
DELETE /gists/:id
GET /gists/:id/:sha
GET /gists/:id/commits
GET /gists/:id/star
PUT /gists/:id/star
DELETE /gists/:id/star
GET /gists/:id/forks
POST /gists/:id/forks
```## Advanced usage
The above example only skims the surface of what Crepe can do. For more
information, see the [Crepe wiki][wiki].[wiki]: https://github.com/crepe/crepe/wiki
## License
(The MIT License.)
Β© 2013β2015 Stephen Celis , Evan Owen , David Celis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the βSoftwareβ), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED βAS ISβ, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.