This is an example application showing how to use grape to create a simple API using entities from the newly-extracted grape-entity gem. This sample show cases how to create a simple API without authentication, caching, custom errors 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.
The sample was developed using the following software. If your software is different, the sample may still work, but there is no guarantee.
- Rails 3.2.8
- ruby 1.9.3p194
- OS X 10.8.1 (aka Mountain Lion)
- Grape (0.2.6)
- Grape Entity (0.2.0)
Install the gems
bundle installCreate and migrate the database
rake db:migrateSeed the database
rake db:seedYou can run the server using the built-in rails server
rails serverGetting all the weblogs
curl -i http://localhost:3000/weblogsCreating a weblog
curl -d '{"title": "Dummy"}' -X POST -H Content-Type:application/json http://localhost:3000/weblogsDeleting a weblog
curl -X DELETE http://localhost:3000/weblogs/1Updating a weblog
curl -d '{"title": "Another Weblog"}' -X PUT -H Content-Type:application/json http://localhost:3000/weblogs/2Get the posts of the weblog #2
curl -i http://localhost:3000/weblogs/2/postsCreate a post in a weblog #2
curl -d '{"title": "Dummy"}' -X POST -H Content-Type:application/json http://localhost:3000/weblogs/2/postsDelete all posts in weblog #2
curl -X DELETE http://localhost:3000/weblogs/2/postsUpdating a post
curl -d '{"title": "Dummy"}' -X POST -H Content-Type:application/json http://localhost:3000/posts/2Delete all posts
curl -X DELETE http://localhost:3000/postsDelete a specific post
curl -X DELETE http://localhost:3000/posts/2Delete all comments from post #2
curl -X DELETE http://localhost:3000/posts/2/commentsCreate a comment in a post #2
curl -d '{"name": "Bob"}' -X POST -H Content-Type:application/json http://localhost:3000/posts/2/commentsGet a comment #2
curl http://localhost:3000/comments/2Delete a comment #2
curl -X DELETE http://localhost:3000/comments/2Delete all comments
curl -X DELETE http://localhost:3000/commentsUpdating comment #2
curl -d '{"name": "Sam"}' -X PUT-H Content-Type:application/json http://localhost:3000/comments/2