/grape-sample-blog-api

A sample application showing how to use grape to create a simple blog api.

Primary LanguageRubyBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Simple API

Endpoints: | Action | Endpoint | Route |

List GET /quotes /api/:version/quotes(.json) Read GET /quotes/:id /api/:version/quotes/:id(.json) Update PUT /quotes/:id /api/:version/quotes/:id(.json) Delete DELETE /quotes/:id /api/:version/quotes/:id(.json) Create POST /quotes /api/:version/quotes(.json) Favorite PATCH /quotes/:id /api/:version/quotes/:id(.json)

curl -X GET 'http://localhost:3001/api/v1/quotes.json' # list
curl -X GET 'http://localhost:3001/api/v1/quotes/1.json' # show
curl -X POST 'http://localhost:3001/api/v1/quotes.json' -d "quote='my cool quote'" # create
curl -X PUT 'http://localhost:3001/api/v1/quotes/1/update.json' -d 'quote_id=4' # favorite

Get routes: be rake grape:routes

Original README

This is a example application showing how to use grape to create a simple API. This sample show cases how to create a simple API without authentication, caching, custom errors, entities and such other things to build a robust public API.

I wrote the sample because I was unable to find a sample to cover the basics of grape.

Environment

The sample was developed using the following software. If your software is different, the sample may still work, but there is no guarantee.

  1. Rails 3.2.8
  2. ruby 1.9.3p194
  3. OS X 10.8.1 (aka Mountain Lion)
  4. Grape (0.2.1)

Setup

Install the gems

bundle install

Create and migrate the database

rake db:migrate

Seed the database

rake db:seed

Running the sample

You can run the server using the built-in rails server

rails server

Usage

Getting all the weblogs

curl -i http://localhost:3000/weblogs

Creating a weblog

curl -d '{"title": "Dummy"}'  -X POST -H Content-Type:application/json http://localhost:3000/weblogs

Deleting a weblog

curl -X DELETE http://localhost:3000/weblogs/1

Updating a weblog

curl -d '{"title": "Another Weblog"}' -X PUT -H Content-Type:application/json http://localhost:3000/weblogs/2

Get the posts of the weblog #2

curl -i http://localhost:3000/weblogs/2/posts

Create a post in a weblog #2

curl -d '{"title": "Dummy"}'  -X POST -H Content-Type:application/json http://localhost:3000/weblogs/2/posts

Delete all posts in weblog #2

curl -X DELETE http://localhost:3000/weblogs/2/posts

Updating a post

curl -d '{"title": "Dummy"}'  -X PUT -H Content-Type:application/json http://localhost:3000/posts/2

Delete all posts

curl -X DELETE http://localhost:3000/posts

Delete a specific post

curl -X DELETE http://localhost:3000/posts/2

Delete all comments from post #2

curl -X DELETE http://localhost:3000/posts/2/comments

Create a comment in a post #2

curl -d '{"name": "Bob"}'  -X POST -H Content-Type:application/json http://localhost:3000/posts/2/comments

Get a comment #2

curl http://localhost:3000/comments/2

Delete a comment #2

curl -X DELETE http://localhost:3000/comments/2

Delete all comments

curl -X DELETE http://localhost:3000/comments

Updating comment #2

curl -d '{"name": "Sam"}' -X PUT-H Content-Type:application/json http://localhost:3000/comments/2