Giter Site home page Giter Site logo

Crepe Build Status Code Climate

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.

Installation

In your application's Gemfile:

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 to generate a new API:

$ gem install creperie --pre
$ crepe new my_api

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 could be written using Crepe:

# 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
      end

      post { 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
    end

    rescue_from ActiveRecord::RecordNotFound do |e|
      error! :not_found, e.message
    end

    rescue_from ActiveRecord::InvalidRecord do |e|
      error! :unprocessable_entity, e.message, errors: e.record.errors
    end

    rescue_from User::Unauthorized do |e|
      unauthorized! realm: 'Gist API'
    end
  end
end

run 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.

License

(The MIT License.)

© 2013–2015 Stephen Celis [email protected], Evan Owen [email protected], David Celis [email protected]

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.

Crêpe's Projects

creperie icon creperie

Create and manage your Crêpe applications.

jsonite icon jsonite

A tiny, HAL-compliant JSON presenter for your Ruby APIs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.