KEMBAR78
Building an API using Grape | PPTX
Building an API using 
Presented By 
Visnupriya. J. R
©2014 
Agenda 
•What is Grape 
•Basic Usage 
•Versioning 
•Format 
•Error Handling 
•Restful-Model-Presentation 
•Testing 
•Documentation
•Powerful API framework 
•Designed to run on Rack Application 
•To fit into Sinatra and Rails Application 
•Grape framework mean "Generalized 
Rapid API Erector" 
©2014 
What is Grape?
©2014 
Basic Usage 
module API 
class Events < Grape::API 
version 'v1‘ 
get :hello do 
{ hello: "Spritle" } 
end 
end 
end
©2014 
Versioning 
•Path 
•Header 
•Accept-Version Header 
•Parameter
content_type :xml, ‘application/xml’ 
content_type :json, 'application/json‘ 
default_format :json ##Override default 
format 
format :json ## for single format 
©2014 
API Format 
•Support XML, JSON, BINARY, 
and TXT content-types 
•Default format is :txt
error! ‘Access Denied’, 401 
### return JSON format 
error!({error: “unexpected error”, detail: 
“missing widget”}, 500) 
### rescue all exceptions and return in 
API format 
rescue_from :all 
©2014 
Error Handling
©2014 
Restful-Model-Presentation 
•To represent data using 
‘present’ method 
–Grape Entities 
–Hypermedia 
–Rabl 
–Active Model Serializers
•Can test API using RSpec 
•RSpec supports API testing framework 
with airborne gem 
•https://github.com/brooklynDev/airbor 
ne 
©2014 
Write Testing
•Easily can document API using Swagger 
•What is Swagger? 
•It’s provide web interface using 
Swagger UI 
•Run doc at 
http://localhost:3000/apidoc 
©2014 
Documentation
©2014 
More…. 
•Helper method 
•Description 
•Pagination with kaminari 
•Cache 
•Content Negotiation 
•OAuth 1.0/2.0
References 
•https://github.com/intridea/grape/wiki 
•https://github.com/intridea/grape#wh 
at-is-grape 
•http://confreaks.com/videos/475- 
rubyconf2010-the-grapes-of-rapid 
•http://funonrails.com/2014/03/building-restful- 
©2014 
api-using-grape-in-rails/ 
•http://intridea.github.io/grape/docs/
©2014 
Q&A?
©2014 
WE 
BUILD 
APPS 
THAT 
PEOPLE 
LOVE TO 
USE 
Cross-Platform Business Mobile 
Apps Development 
Enterprise Web Applications 
Development 
Social Media Integrated 
Applications Development 
Product Development Services

Building an API using Grape

  • 1.
    Building an APIusing Presented By Visnupriya. J. R
  • 2.
    ©2014 Agenda •Whatis Grape •Basic Usage •Versioning •Format •Error Handling •Restful-Model-Presentation •Testing •Documentation
  • 3.
    •Powerful API framework •Designed to run on Rack Application •To fit into Sinatra and Rails Application •Grape framework mean "Generalized Rapid API Erector" ©2014 What is Grape?
  • 4.
    ©2014 Basic Usage module API class Events < Grape::API version 'v1‘ get :hello do { hello: "Spritle" } end end end
  • 5.
    ©2014 Versioning •Path •Header •Accept-Version Header •Parameter
  • 6.
    content_type :xml, ‘application/xml’ content_type :json, 'application/json‘ default_format :json ##Override default format format :json ## for single format ©2014 API Format •Support XML, JSON, BINARY, and TXT content-types •Default format is :txt
  • 7.
    error! ‘Access Denied’,401 ### return JSON format error!({error: “unexpected error”, detail: “missing widget”}, 500) ### rescue all exceptions and return in API format rescue_from :all ©2014 Error Handling
  • 8.
    ©2014 Restful-Model-Presentation •Torepresent data using ‘present’ method –Grape Entities –Hypermedia –Rabl –Active Model Serializers
  • 9.
    •Can test APIusing RSpec •RSpec supports API testing framework with airborne gem •https://github.com/brooklynDev/airbor ne ©2014 Write Testing
  • 10.
    •Easily can documentAPI using Swagger •What is Swagger? •It’s provide web interface using Swagger UI •Run doc at http://localhost:3000/apidoc ©2014 Documentation
  • 11.
    ©2014 More…. •Helpermethod •Description •Pagination with kaminari •Cache •Content Negotiation •OAuth 1.0/2.0
  • 12.
    References •https://github.com/intridea/grape/wiki •https://github.com/intridea/grape#wh at-is-grape •http://confreaks.com/videos/475- rubyconf2010-the-grapes-of-rapid •http://funonrails.com/2014/03/building-restful- ©2014 api-using-grape-in-rails/ •http://intridea.github.io/grape/docs/
  • 13.
  • 14.
    ©2014 WE BUILD APPS THAT PEOPLE LOVE TO USE Cross-Platform Business Mobile Apps Development Enterprise Web Applications Development Social Media Integrated Applications Development Product Development Services