8. Installation
Grape is a gem, to install it just install the
gem:
gem install grape
If you're using Bundler, add the gem to
Gemfile.
gem 'grape‘
9. CRUD Actions (GET)
class App < Grape::API
get '/files', rabl: 'assets/collection' do
@assets = Asset.all
end
end curl http://localhost:9292/files
[{"asset":{"title":"My first zip
file","id":1,"assetuploader":{}}},{"asset":{"title"
:"My first zip file","id":2,"assetuploader":{}}},
… ]
10. CRUD Actions (GET)
class App < Grape::API
get '/files/:id', rabl: 'assets/item' do
@asset = Asset[params[:id]]
end
end
curl http://localhost:9292/files/1
{"asset":{"title":"My first zip
file","id":1,"url":"/public/uploads/ass
ets/1/zip.zip","user":"N/A"}}
11. CRUD Actions (POST)
class App < Grape::API
post '/files', rabl: 'assets/item' do
@asset = Asset.new params[:file]
@asset.save
end
end
curl -X POST -d 'file[title]=First file'
http://localhost:9292/files
{"asset":{"title":"First file","id":38,
"url":null,"user":"N/A"}}
12. CRUD Actions (PUT)
class App < Grape::API
put '/files/:id', rabl: 'assets/item' do
@asset = Asset[params[:id]]
@asset.set(params[:file]).save
end
end
curl -X PUT -d 'file[title]=Updated
file' http://localhost:9292/files/40
{"asset":{"title":"Updated
file","id":40,"url":null,"user":"N/A"}}
13. CRUD Actions (DELETE)
class App < Grape::API
delete '/files/:id' do
@asset = Asset[params[:id]]
@asset.delete
"File with id #{params[:id]} deleted"
end
end
curl -X DELETE
http://localhost:9292/files/39
"File with id 39 deleted"
14. Namespace
class App < Grape::API
resource :files do
get '/', rabl: 'assets/collection' do
@assets = Asset.all
end
get '/:id', rabl: 'assets/item' do
@asset = Asset[params[:id]]
end
post '/', rabl: 'assets/item' do
@asset = Asset.new params[:file]
@asset.save
end
…
end
end
16. Versioning (Path)
class App < Grape::API
version 'v1', using: :path
get '/files', rabl: 'assets/collection' do
@assets = Asset.all
end
end
curl http://localhost:9292/v1/files
17. Versioning (Header)
class App < Grape::API
version 'v1', using: :header, vendor: 'asset'
get '/files', rabl: 'assets/collection' do
@assets = Asset.all
end
end
curl -H Accept=application/vnd.asset-v1+
json http://localhost:9292/files
18. Versioning (Accept-Version Header)
class App < Grape::API
version 'v1', using: :accept_version_header
get '/files', rabl: 'assets/collection' do
@assets = Asset.all
end
end
curl -H "Accept-Version=v1"
http://localhost:9292/files
19. Versioning (Param)
class App < Grape::API
version 'v1', using: :param
get '/files', rabl: 'assets/collection' do
@assets = Asset.all
end
end
curl http://localhost:9292/files?apiver=v1
20. Versioning (Param)
class App < Grape::API
version 'v1', using: :param, parameter: "version"
get '/files', rabl: 'assets/collection' do
@assets = Asset.all
end
end
curl http://localhost:9292/files?version=v1
21. Parameter Validation
class App < Grape::API
params do
requires :id, type: Integer
end
get '/files/:id', rabl: 'assets/item' do
@asset = Asset[params[:id]]
end
end
curl http://localhost:9292/files/1
{"asset":{"title":"My first zip file","id":1,
"url":"/public/uploads/assets/1/zip.zip“
,"user":"N/A"}}
curl http://localhost:9292/files/wrong
{"error":"id is invalid"}
22. Parameter Validation
class App < Grape::API
params do
requires :id, type: Integer
optional :text, type: String,
regexp: /^[a-z]+$/
end
get '/files/:id', rabl: 'assets/item' do
@asset = Asset[params[:id]]
end
end
curl http://localhost:9292/files/1
{"asset":{"title":"My first zip file"}}
curl http://localhost:9292/files/1?text=example
{"asset":{"title":"My first zip file",}}
curl http://localhost:9292/files/1?text=Example
{"error":"text is invalid"}
23. Basic Authentication
class App < Grape::API
http_basic do |handler, password|
@@user = User.where(handler: handler).first
@@user.authorize? password
end
get '/files/:id', rabl: 'assets/item' do
@asset = Asse[params[:id]]
end
end
24. API Formats
class App < Grape::API
format :json
get '/files', rabl: 'assets/item' do
@asset = Asse.all
end
end
25. API Formats
class App < Grape::API
format :json
default_format :json
get '/files', rabl: 'assets/item' do
@asset = Asse.all
end
end
28. Grape vs Rails
ab (apache-bench) is a tool
for benchmarking your
Hypertext Transfer Protocol
(HTTP) server
29. Ruby Version Grape Rails::API
Ruby 1.9.3-p448 Time taken for tests:
40.799 seconds
Time taken for tests:
89.975 seconds
Requests per second:
2451.07
Requests per second:
1111.42
Time per request:
0.408
Time per request:
0.900
Grape vs Rails
30. Ruby Version Grape Rails::API
Ruby 2.0.0-p247 Time taken for tests:
44.902 seconds
Time taken for tests:
107.761 seconds
Requests per second:
2227.10
Requests per second:
927.98
Time per request:
0.449
Time per request:
1.078
Grape vs Rails
31. Ruby Version Grape Rails::API
JRuby 1.7.4 Time taken for tests:
12.664 seconds
Time taken for tests:
33.278 seconds
Requests per second:
7896.50
Requests per second:
3005.01
Time per request:
0.507
Time per request:
1.331
Grape vs Rails
32. Resources
Google Group - Get Grape help here.
API Documentation - YARD documentation for the
Grape API.
The Grapes of Rapid - RubyConf 2010 presentation
about Grape. Slides
Grape Wiki - A collection of more resources and
gems.