SlideShare a Scribd company logo
Building and Testing Rails 
JSON API with RSpec 
Eddie Lau (3dd13@42la.bs) 
23 Oct 2014
Agenda 
โ€ข 1. Development Process 
โ€ข 2. API Design 
โ€ข 3. Rails, RSpec and Gems 
โ€ข 4. Mini-hackathon
Mini-hackthon 
build your own API
Setup (Mac) 
Ruby 
curl -sSL https://get.rvm.io | bash 
rvm install 2.1.3 
Gems needed 
gem install bundler 
gem install rails 
Sample project code 
git clone http://github.com/3dd13/api_rspec_workshop 
cd api_rspec_workshop 
bundle install
1. Development Process
Workflow 
Initial 
โ€ข UX / UI / Wireframe 
โ€ข Communication / Endpoint needed 
โ€ข API Design 
โ€ข Test cases 
โ€ข GREEN 
Add feature 
โ€ข UX / UI / Wireframe 
โ€ข Communication / Endpoint needed 
โ€ข API Design 
โ€ข Upgrade version (if needed) 
โ€ข Add / Update test cases 
โ€ข GREEN
2. API Design
Architecture 
Frontend Js Client 
HTTP API 
Backend Server
42 Labs Stack 
AngularJS (Browser or Mobile) 
RESTful API - JSON 
Ruby on Rails / Node.js
42 Labs Stack 
Admin Web 
Customer Web 
(AngularJS) 
Customer Mobile 
(AngularJS) 
Ruby on Rails 
HTML 
JSON API 
JSON API
RESTful 
โ€ข GET http://example.com/projects.json 
โ€ข GET http://example.com/projects/:id.json 
โ€ข PATCH http://example.com/projects/:id.json 
body: {project: {name: โ€œโ€, โ€ฆ}} 
โ€ข POST http://example.com/projects.json 
body: {project: {name: โ€œโ€, โ€ฆ}} 
โ€ข DELETE http://example.com/projects/:id.json
Standard / Convention 
Always 
GET, CREATE, UPDATE, DELETE resource
RESTful ? 
1. Search projects by date range 
GET http://example.com/projects/search.json 
2. Create multiple projects 
POST http://example.com/projects.json 
body: [{project: {name: โ€œโ€, โ€ฆ}, {project: {name: โ€œโ€,โ€ฆ}} ] 
3. Update multiple projects (e.g. mark as archived) 
PATCH http://example.com/projects/archive.json 
body: { project_ids: [1, 3, 5]}
Suggestion 
not always true ! 
1. Search projects by date range 
GET http://example.com/projects.json?date_range_filter=last_week 
2. Create multiple projects 
POST http://example.com/project_batches.json 
body: {project_batch: [{ project: {name: โ€œโ€} }, โ€ฆ]} 
3. Update multiple projects (e.g. mark as archived) 
PATCH http://example.com/projects/1,3,5.json
RESTful Authentication 
โ€ข Step 1: User Sign In 
POST http://example.com/users/sessions 
body: {email: โ€œuser@example.comโ€, password: โ€œpasswordโ€} 
Response: 
{session: {auth_token: โ€œ123abc456defxxxxxโ€}} 
โ€ข Step 2: Access Protected Action 
GET http://example.com/users/projects/1.json 
Header: X-AUTH-TOKEN 123abc456defxxxxx
Versioning 
โ€ข GET http://example.com/api/v1/projects.json 
โ€ข GET http://example.com/api/projects.json 
Accept: application/vnd.charityspring.v1
3. Rails & RSpec 
and other gems
Testing
rspec 
expect(status).to eq 201 
expect(response_body).to be_json_eql(expected_json) 
expect{ do_request }.to change{ Project.count }.from(0).to(1)
factory_girl 
FactoryGirl.define do 
factory :user do 
sequence(:email) { |n| "user#{n}@example.com"} 
password "password" 
end 
end 
user = FactoryGirl.create(:user)
rspec_api_documentation 
rake docs:generate
4. Mini-hackthon 
build your own API
Setup (Mac) 
Ruby 
curl -sSL https://get.rvm.io | bash 
rvm install 2.1.3 
Gems needed 
gem install bundler 
gem install rails 
Sample project code 
git clone http://github.com/3dd13/api_rspec_workshop 
cd api_rspec_workshop 
bundle install
Run test cases 
rake db:migrate 
rake 
rake docs:generate
Create seed data 
rake db:seed
Code walk through
Note: 
1. password is plain text 
2. no database index was created 
donโ€™t use it in production, feel free to change the code
Start coding ~ 
1. Visitor wants to see a list of crowd funding projects 
2. Visitor wants to search crowd funding projects by name 
3. Visitor wants to sign up using email and password 
4. Visitor wants to sign in using email and password 
5. Member wants to create a crowd funding project 
6. Member wants to see a list of crowd funding project she created 
7. Member wants to pledge a crowd funding project 
8. Member wants to see a list of crowd funding project she pledged
Advanced 
1. Member wants to pledge and pay via Paypal / Stripe 
2. Add a new API endpoint to v2

More Related Content

PPTX
Basic Git commands
KEY
Matt Gauger - Git & Github web414 December 2010
PDF
Mastering Grails 3 Plugins - GR8Conf EU 2016
PDF
Git real slides
PDF
Git 101: Git and GitHub for Beginners
ย 
PDF
Git and github 101
PDF
Git Started With Git
PPT
Introduction to Git and Github
Basic Git commands
Matt Gauger - Git & Github web414 December 2010
Mastering Grails 3 Plugins - GR8Conf EU 2016
Git real slides
Git 101: Git and GitHub for Beginners
ย 
Git and github 101
Git Started With Git
Introduction to Git and Github

What's hot (19)

PDF
Git - An Introduction
PPTX
Git 101
PDF
Learning git
PDF
Puppet at GitHub
ย 
PPTX
Introduction to git & GitHub
PDF
PuppeteerใฎใŠ่ฉฑ
PPT
Introduction to Git Commands and Concepts
PDF
Docker tips
PDF
Make an Instant Website with Webhooks
PPTX
Git Obstacle Course: Stop BASHing your head and break down the basics
PDF
GitHub Actions in action
PDF
Introduction to Git (part 1)
PPTX
Git 101 for Beginners
PDF
Building an API with Django and Django REST Framework
PDF
Netflix Nebula - Gradle Summit 2014
PDF
Git Tutorial
PDF
The First 10M Pulls: Building The Official Curl Image for Docker Hub
PDF
SF Gradle Meetup - Netflix OSS
PDF
How to Use Mirroring and Caching to Optimize your Container Registry
Git - An Introduction
Git 101
Learning git
Puppet at GitHub
ย 
Introduction to git & GitHub
PuppeteerใฎใŠ่ฉฑ
Introduction to Git Commands and Concepts
Docker tips
Make an Instant Website with Webhooks
Git Obstacle Course: Stop BASHing your head and break down the basics
GitHub Actions in action
Introduction to Git (part 1)
Git 101 for Beginners
Building an API with Django and Django REST Framework
Netflix Nebula - Gradle Summit 2014
Git Tutorial
The First 10M Pulls: Building The Official Curl Image for Docker Hub
SF Gradle Meetup - Netflix OSS
How to Use Mirroring and Caching to Optimize your Container Registry
Ad

Viewers also liked (19)

PPTX
7 data entry
PDF
I'm watir
ย 
PPTX
Automated testing - back to the roots
PPT
Trading Clearing Systems Test Automation
PDF
Web Development with Sinatra
PDF
Effectively Testing Services - Burlington Ruby Conf
PPTX
Metaprogramming ruby
PPTX
Mutation Testing - Ruby Edition
DOCX
Mobile app testing
KEY
Effective Testing with Ruby
PDF
Action Controller Overview, Season 2
ย 
PPTX
Test automation in project management
PPTX
Microsoft Azure Mobile Services
PPT
Metaprogramming With Ruby
PDF
20141024 AgileDC 2014 Conf How much testing is enough for software that can c...
PDF
How to accurately estimate the size and effort of your software testing (1)
PPTX
ยซRuby integration testing toolsยป
PDF
Automated Testing with Selenium and Bamboo - Atlassian Summit 2010 - Lightnin...
PPT
Rails testing: factories or fixtures?
ย 
7 data entry
I'm watir
ย 
Automated testing - back to the roots
Trading Clearing Systems Test Automation
Web Development with Sinatra
Effectively Testing Services - Burlington Ruby Conf
Metaprogramming ruby
Mutation Testing - Ruby Edition
Mobile app testing
Effective Testing with Ruby
Action Controller Overview, Season 2
ย 
Test automation in project management
Microsoft Azure Mobile Services
Metaprogramming With Ruby
20141024 AgileDC 2014 Conf How much testing is enough for software that can c...
How to accurately estimate the size and effort of your software testing (1)
ยซRuby integration testing toolsยป
Automated Testing with Selenium and Bamboo - Atlassian Summit 2010 - Lightnin...
Rails testing: factories or fixtures?
ย 
Ad

Similar to Women Who Code - RSpec JSON API Workshop (20)

PDF
Ruby on Rails Kickstart 101 & 102
KEY
Play Support in Cloud Foundry
ย 
PDF
DevOPS training - Day 2/2
KEY
Supa fast Ruby + Rails
PDF
2.28.17 Introducing DSpace 7 Webinar Slides
ODP
An Overview of Node.js
PPTX
Spring Cloud: API gateway upgrade & configuration in the cloud
PPTX
REST API Best Practices & Implementing in Codeigniter
PDF
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
ย 
PDF
Plugin-based software design with Ruby and RubyGems
PDF
Making a small QA system with Docker
PDF
Building APIs in an easy way using API Platform
PPTX
Untangling - fall2017 - week 9
PDF
Parse cloud code
PDF
Building Beautiful REST APIs with ASP.NET Core
PDF
Everything-as-code - A polyglot adventure
PDF
Everything-as-code. A polyglot adventure. #DevoxxPL
PPT
In Act Developers Platform
PDF
Building applications with Serverless Framework and AWS Lambda - JavaZone 2019
PPTX
Migrating from MongoDB to Neo4j - Lessons Learned
Ruby on Rails Kickstart 101 & 102
Play Support in Cloud Foundry
ย 
DevOPS training - Day 2/2
Supa fast Ruby + Rails
2.28.17 Introducing DSpace 7 Webinar Slides
An Overview of Node.js
Spring Cloud: API gateway upgrade & configuration in the cloud
REST API Best Practices & Implementing in Codeigniter
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
ย 
Plugin-based software design with Ruby and RubyGems
Making a small QA system with Docker
Building APIs in an easy way using API Platform
Untangling - fall2017 - week 9
Parse cloud code
Building Beautiful REST APIs with ASP.NET Core
Everything-as-code - A polyglot adventure
Everything-as-code. A polyglot adventure. #DevoxxPL
In Act Developers Platform
Building applications with Serverless Framework and AWS Lambda - JavaZone 2019
Migrating from MongoDB to Neo4j - Lessons Learned

Recently uploaded (20)

PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
DOCX
Unit-3 cyber security network security of internet system
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
ย 
PDF
Introduction to the IoT system, how the IoT system works
ย 
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PDF
๐Ÿ’ฐ ๐”๐Š๐“๐ˆ ๐Š๐„๐Œ๐„๐๐€๐๐†๐€๐ ๐Š๐ˆ๐๐„๐‘๐Ÿ’๐ƒ ๐‡๐€๐‘๐ˆ ๐ˆ๐๐ˆ ๐Ÿ๐ŸŽ๐Ÿ๐Ÿ“ ๐Ÿ’ฐ
ย 
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PPTX
Funds Management Learning Material for Beg
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
presentation_pfe-universite-molay-seltan.pptx
Design_with_Watersergyerge45hrbgre4top (1).ppt
522797556-Unit-2-Temperature-measurement-1-1.pptx
Unit-3 cyber security network security of internet system
RPKI Status Update, presented by Makito Lay at IDNOG 10
ย 
Introduction to the IoT system, how the IoT system works
ย 
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
๐Ÿ’ฐ ๐”๐Š๐“๐ˆ ๐Š๐„๐Œ๐„๐๐€๐๐†๐€๐ ๐Š๐ˆ๐๐„๐‘๐Ÿ’๐ƒ ๐‡๐€๐‘๐ˆ ๐ˆ๐๐ˆ ๐Ÿ๐ŸŽ๐Ÿ๐Ÿ“ ๐Ÿ’ฐ
ย 
SAP Ariba Sourcing PPT for learning material
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Introuction about ICD -10 and ICD-11 PPT.pptx
Decoding a Decade: 10 Years of Applied CTI Discipline
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Funds Management Learning Material for Beg
Cloud-Scale Log Monitoring _ Datadog.pdf
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Module 1 - Cyber Law and Ethics 101.pptx
presentation_pfe-universite-molay-seltan.pptx

Women Who Code - RSpec JSON API Workshop

  • 1. Building and Testing Rails JSON API with RSpec Eddie Lau (3dd13@42la.bs) 23 Oct 2014
  • 2. Agenda โ€ข 1. Development Process โ€ข 2. API Design โ€ข 3. Rails, RSpec and Gems โ€ข 4. Mini-hackathon
  • 4. Setup (Mac) Ruby curl -sSL https://get.rvm.io | bash rvm install 2.1.3 Gems needed gem install bundler gem install rails Sample project code git clone http://github.com/3dd13/api_rspec_workshop cd api_rspec_workshop bundle install
  • 6. Workflow Initial โ€ข UX / UI / Wireframe โ€ข Communication / Endpoint needed โ€ข API Design โ€ข Test cases โ€ข GREEN Add feature โ€ข UX / UI / Wireframe โ€ข Communication / Endpoint needed โ€ข API Design โ€ข Upgrade version (if needed) โ€ข Add / Update test cases โ€ข GREEN
  • 8. Architecture Frontend Js Client HTTP API Backend Server
  • 9. 42 Labs Stack AngularJS (Browser or Mobile) RESTful API - JSON Ruby on Rails / Node.js
  • 10. 42 Labs Stack Admin Web Customer Web (AngularJS) Customer Mobile (AngularJS) Ruby on Rails HTML JSON API JSON API
  • 11. RESTful โ€ข GET http://example.com/projects.json โ€ข GET http://example.com/projects/:id.json โ€ข PATCH http://example.com/projects/:id.json body: {project: {name: โ€œโ€, โ€ฆ}} โ€ข POST http://example.com/projects.json body: {project: {name: โ€œโ€, โ€ฆ}} โ€ข DELETE http://example.com/projects/:id.json
  • 12. Standard / Convention Always GET, CREATE, UPDATE, DELETE resource
  • 13. RESTful ? 1. Search projects by date range GET http://example.com/projects/search.json 2. Create multiple projects POST http://example.com/projects.json body: [{project: {name: โ€œโ€, โ€ฆ}, {project: {name: โ€œโ€,โ€ฆ}} ] 3. Update multiple projects (e.g. mark as archived) PATCH http://example.com/projects/archive.json body: { project_ids: [1, 3, 5]}
  • 14. Suggestion not always true ! 1. Search projects by date range GET http://example.com/projects.json?date_range_filter=last_week 2. Create multiple projects POST http://example.com/project_batches.json body: {project_batch: [{ project: {name: โ€œโ€} }, โ€ฆ]} 3. Update multiple projects (e.g. mark as archived) PATCH http://example.com/projects/1,3,5.json
  • 15. RESTful Authentication โ€ข Step 1: User Sign In POST http://example.com/users/sessions body: {email: โ€œuser@example.comโ€, password: โ€œpasswordโ€} Response: {session: {auth_token: โ€œ123abc456defxxxxxโ€}} โ€ข Step 2: Access Protected Action GET http://example.com/users/projects/1.json Header: X-AUTH-TOKEN 123abc456defxxxxx
  • 16. Versioning โ€ข GET http://example.com/api/v1/projects.json โ€ข GET http://example.com/api/projects.json Accept: application/vnd.charityspring.v1
  • 17. 3. Rails & RSpec and other gems
  • 19. rspec expect(status).to eq 201 expect(response_body).to be_json_eql(expected_json) expect{ do_request }.to change{ Project.count }.from(0).to(1)
  • 20. factory_girl FactoryGirl.define do factory :user do sequence(:email) { |n| "user#{n}@example.com"} password "password" end end user = FactoryGirl.create(:user)
  • 22. 4. Mini-hackthon build your own API
  • 23. Setup (Mac) Ruby curl -sSL https://get.rvm.io | bash rvm install 2.1.3 Gems needed gem install bundler gem install rails Sample project code git clone http://github.com/3dd13/api_rspec_workshop cd api_rspec_workshop bundle install
  • 24. Run test cases rake db:migrate rake rake docs:generate
  • 25. Create seed data rake db:seed
  • 27. Note: 1. password is plain text 2. no database index was created donโ€™t use it in production, feel free to change the code
  • 28. Start coding ~ 1. Visitor wants to see a list of crowd funding projects 2. Visitor wants to search crowd funding projects by name 3. Visitor wants to sign up using email and password 4. Visitor wants to sign in using email and password 5. Member wants to create a crowd funding project 6. Member wants to see a list of crowd funding project she created 7. Member wants to pledge a crowd funding project 8. Member wants to see a list of crowd funding project she pledged
  • 29. Advanced 1. Member wants to pledge and pay via Paypal / Stripe 2. Add a new API endpoint to v2