Skip to content

Instantly share code, notes, and snippets.

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

Restaurant

Restaurant serves RESTful API on Rails.

Features

Controller

Restaurant provides controller-less & RESTful API. All controllers and routings will be auto-defined from your config/roles.yml definition.

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

Authentication

Doorkeeper is supported for authentication by default.

$ rails g doorkeeper:install
$ rails g doorkeeper:migration
$ rake db:migrate
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  doorkeeper_for :all
end

Authorization

  • User with "public" scope token
  • can access /recipes/:id
  • User with "admin" scope token
  • can access /recipes/:id
  • can access /recipes
  • can filter recipes by id and title
  • can sort recipes by id and title
# config/roles.yml
public:
  recipes:
    actions:
      - show
admin:
  recipes:
    actions:
      - index
      - show
    where:
      - id
      - title
    order:
      - id
      - title

SQL-like URI query

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(
       "body"       => nil,
       "created_at" => "2000-01-01T00:00:00Z",
       "id"         => 1,
       "title"      => "title 1",
       "created_at" => "2000-01-01T00:00:00Z",
       "user_id"    => 1
    )
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment