Skip to content

Instantly share code, notes, and snippets.

@r7kamura
Created May 14, 2013 13:53
Show Gist options
  • Save r7kamura/5576038 to your computer and use it in GitHub Desktop.
Save r7kamura/5576038 to your computer and use it in GitHub Desktop.

Restaurant

Restaurant serves your data via auto-defined RESTful API on your rails application.

Install

# Gemfile
gem "restaurant"

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  include Restaurant::ControllerHelper
end

# config/routes.rb
Rails.application.routes.draw do
  Restaurant::Router.route(self)
end
$ bundle install
$ bundle exec rails g doorkeeper:install
$ bundle exec rails g doorkeeper:migration
$ bundle exec rake db:migrate

restaurant.yml

Controllers and routes are auto-defined from your config/restaurant.yml. You can restrict requests by scopes, actions, attributes, and queries.

# config/restaurant.yml
public:         # User with "public" scope token
  recipes:      #
    actions:    #
      - show    # can access to /recipes/:id
    attributes: #
      - title   # can read recipe.title

admin:          # User with "admin" scope token
  recipes:      #
    actions:    #
      - index   # can access to /recipes
      - show    # can access to /recipes/:id
    where:      #
      - id      # can filter recipes by id
      - title   # can filter recipes by title
    order:      #
      - id      # can sort recipes by id
      - title   # can sort recipes by title
    attributes: #
      - id      # can read recipe.id
      - title   # can read recipe.title

SQL-like URI query

SQL-like URI query system is supported.

context "with where params" do
  it "returns recipes filtered by given query" do
    get "/recipes", { where: { title: { eq: recipe.title } } }, env
    response.should be_ok
    response.body.should be_json(
       "id"         => 1,
       "user_id"    => 1
       "body"       => "body 1",
       "title"      => "title 1",
       "updated_at" => "2000-01-01T00:00:00Z",
       "created_at" => "2000-01-01T00:00:00Z",
    )
  end
end

More

See the example application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment