SlideShare a Scribd company logo
1 of 29
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

What's hot

SF Gradle Meetup - Netflix OSS
SF Gradle Meetup - Netflix OSSSF Gradle Meetup - Netflix OSS
SF Gradle Meetup - Netflix OSS
Justin Ryan
 

What's hot (20)

Git - An Introduction
Git - An IntroductionGit - An Introduction
Git - An Introduction
 
Introduction to Git and Github
Introduction to Git and GithubIntroduction to Git and Github
Introduction to Git and Github
 
Git 101
Git 101Git 101
Git 101
 
Learning git
Learning gitLearning git
Learning git
 
Puppet at GitHub
Puppet at GitHubPuppet at GitHub
Puppet at GitHub
 
Introduction to git & GitHub
Introduction to git & GitHubIntroduction to git & GitHub
Introduction to git & GitHub
 
Puppeteerのお話
Puppeteerのお話Puppeteerのお話
Puppeteerのお話
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and Concepts
 
Docker tips
Docker tipsDocker tips
Docker tips
 
Make an Instant Website with Webhooks
Make an Instant Website with WebhooksMake an Instant Website with Webhooks
Make an Instant Website with Webhooks
 
Git Obstacle Course: Stop BASHing your head and break down the basics
Git Obstacle Course: Stop BASHing your head and break down the basicsGit Obstacle Course: Stop BASHing your head and break down the basics
Git Obstacle Course: Stop BASHing your head and break down the basics
 
GitHub Actions in action
GitHub Actions in actionGitHub Actions in action
GitHub Actions in action
 
Introduction to Git (part 1)
Introduction to Git (part 1)Introduction to Git (part 1)
Introduction to Git (part 1)
 
Git 101 for Beginners
Git 101 for Beginners Git 101 for Beginners
Git 101 for Beginners
 
Building an API with Django and Django REST Framework
Building an API with Django and Django REST FrameworkBuilding an API with Django and Django REST Framework
Building an API with Django and Django REST Framework
 
Netflix Nebula - Gradle Summit 2014
Netflix Nebula - Gradle Summit 2014Netflix Nebula - Gradle Summit 2014
Netflix Nebula - Gradle Summit 2014
 
Git Tutorial
Git TutorialGit Tutorial
Git Tutorial
 
The First 10M Pulls: Building The Official Curl Image for Docker Hub
The First 10M Pulls: Building The Official Curl Image for Docker HubThe First 10M Pulls: Building The Official Curl Image for Docker Hub
The First 10M Pulls: Building The Official Curl Image for Docker Hub
 
SF Gradle Meetup - Netflix OSS
SF Gradle Meetup - Netflix OSSSF Gradle Meetup - Netflix OSS
SF Gradle Meetup - Netflix OSS
 
How to Use Mirroring and Caching to Optimize your Container Registry
How to Use Mirroring and Caching to Optimize your Container RegistryHow to Use Mirroring and Caching to Optimize your Container Registry
How to Use Mirroring and Caching to Optimize your Container Registry
 

Viewers also liked

Trading Clearing Systems Test Automation
Trading Clearing Systems Test AutomationTrading Clearing Systems Test Automation
Trading Clearing Systems Test Automation
Iosif Itkin
 
Effective Testing with Ruby
Effective Testing with RubyEffective Testing with Ruby
Effective Testing with Ruby
Akira Sosa
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2
RORLAB
 
20141024 AgileDC 2014 Conf How much testing is enough for software that can c...
20141024 AgileDC 2014 Conf How much testing is enough for software that can c...20141024 AgileDC 2014 Conf How much testing is enough for software that can c...
20141024 AgileDC 2014 Conf How much testing is enough for software that can c...
Craeg Strong
 

Viewers also liked (19)

7 data entry
7 data entry7 data entry
7 data entry
 
I'm watir
I'm watirI'm watir
I'm watir
 
Automated testing - back to the roots
Automated testing - back to the rootsAutomated testing - back to the roots
Automated testing - back to the roots
 
Trading Clearing Systems Test Automation
Trading Clearing Systems Test AutomationTrading Clearing Systems Test Automation
Trading Clearing Systems Test Automation
 
Web Development with Sinatra
Web Development with SinatraWeb Development with Sinatra
Web Development with Sinatra
 
Effectively Testing Services - Burlington Ruby Conf
Effectively Testing Services - Burlington Ruby ConfEffectively Testing Services - Burlington Ruby Conf
Effectively Testing Services - Burlington Ruby Conf
 
Metaprogramming ruby
Metaprogramming rubyMetaprogramming ruby
Metaprogramming ruby
 
Mutation Testing - Ruby Edition
Mutation Testing - Ruby EditionMutation Testing - Ruby Edition
Mutation Testing - Ruby Edition
 
Mobile app testing
Mobile app testingMobile app testing
Mobile app testing
 
Effective Testing with Ruby
Effective Testing with RubyEffective Testing with Ruby
Effective Testing with Ruby
 
Action Controller Overview, Season 2
Action Controller Overview, Season 2Action Controller Overview, Season 2
Action Controller Overview, Season 2
 
Test automation in project management
Test automation in project managementTest automation in project management
Test automation in project management
 
Microsoft Azure Mobile Services
Microsoft Azure Mobile ServicesMicrosoft Azure Mobile Services
Microsoft Azure Mobile Services
 
Metaprogramming With Ruby
Metaprogramming With RubyMetaprogramming With Ruby
Metaprogramming With Ruby
 
20141024 AgileDC 2014 Conf How much testing is enough for software that can c...
20141024 AgileDC 2014 Conf How much testing is enough for software that can c...20141024 AgileDC 2014 Conf How much testing is enough for software that can c...
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)
How to accurately estimate the size and effort of your software testing (1)How to accurately estimate the size and effort of your software testing (1)
How to accurately estimate the size and effort of your software testing (1)
 
«Ruby integration testing tools»
«Ruby integration testing tools»«Ruby integration testing tools»
«Ruby integration testing tools»
 
Automated Testing with Selenium and Bamboo - Atlassian Summit 2010 - Lightnin...
Automated Testing with Selenium and Bamboo - Atlassian Summit 2010 - Lightnin...Automated Testing with Selenium and Bamboo - Atlassian Summit 2010 - Lightnin...
Automated Testing with Selenium and Bamboo - Atlassian Summit 2010 - Lightnin...
 
Rails testing: factories or fixtures?
Rails testing: factories or fixtures?Rails testing: factories or fixtures?
Rails testing: factories or fixtures?
 

Similar to Women Who Code - RSpec JSON API Workshop

An Overview of Node.js
An Overview of Node.jsAn Overview of Node.js
An Overview of Node.js
Ayush Mishra
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code
維佋 唐
 
In Act Developers Platform
In Act Developers PlatformIn Act Developers Platform
In Act Developers Platform
Eris Ristemena
 

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

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

Recently uploaded

原版定制(爱大毕业证书)英国爱丁堡大学毕业证原件一模一样
原版定制(爱大毕业证书)英国爱丁堡大学毕业证原件一模一样原版定制(爱大毕业证书)英国爱丁堡大学毕业证原件一模一样
原版定制(爱大毕业证书)英国爱丁堡大学毕业证原件一模一样
gfhdsfr
 
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
Fi
 
一比一原版(Exon毕业证书)英国埃克塞特大学毕业证如何办理
一比一原版(Exon毕业证书)英国埃克塞特大学毕业证如何办理一比一原版(Exon毕业证书)英国埃克塞特大学毕业证如何办理
一比一原版(Exon毕业证书)英国埃克塞特大学毕业证如何办理
gfhdsfr
 
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
Fir
 
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
Fi
 
一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理
A
 
一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书
A
 
一比一定制(OSU毕业证书)美国俄亥俄州立大学毕业证学位证书
一比一定制(OSU毕业证书)美国俄亥俄州立大学毕业证学位证书一比一定制(OSU毕业证书)美国俄亥俄州立大学毕业证学位证书
一比一定制(OSU毕业证书)美国俄亥俄州立大学毕业证学位证书
rgdasda
 
一比一原版(PSU毕业证书)美国宾州州立大学毕业证如何办理
一比一原版(PSU毕业证书)美国宾州州立大学毕业证如何办理一比一原版(PSU毕业证书)美国宾州州立大学毕业证如何办理
一比一原版(PSU毕业证书)美国宾州州立大学毕业证如何办理
Fir
 
一比一原版加拿大多伦多大学毕业证(UofT毕业证书)如何办理
一比一原版加拿大多伦多大学毕业证(UofT毕业证书)如何办理一比一原版加拿大多伦多大学毕业证(UofT毕业证书)如何办理
一比一原版加拿大多伦多大学毕业证(UofT毕业证书)如何办理
egfdgfd
 
原版定制(Management毕业证书)新加坡管理大学毕业证原件一模一样
原版定制(Management毕业证书)新加坡管理大学毕业证原件一模一样原版定制(Management毕业证书)新加坡管理大学毕业证原件一模一样
原版定制(Management毕业证书)新加坡管理大学毕业证原件一模一样
asdafd
 
一比一原版(TRU毕业证书)温哥华社区学院毕业证如何办理
一比一原版(TRU毕业证书)温哥华社区学院毕业证如何办理一比一原版(TRU毕业证书)温哥华社区学院毕业证如何办理
一比一原版(TRU毕业证书)温哥华社区学院毕业证如何办理
Fir
 

Recently uploaded (20)

原版定制(爱大毕业证书)英国爱丁堡大学毕业证原件一模一样
原版定制(爱大毕业证书)英国爱丁堡大学毕业证原件一模一样原版定制(爱大毕业证书)英国爱丁堡大学毕业证原件一模一样
原版定制(爱大毕业证书)英国爱丁堡大学毕业证原件一模一样
 
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
一比一原版(Soton毕业证书)南安普顿大学毕业证原件一模一样
 
一比一原版(Exon毕业证书)英国埃克塞特大学毕业证如何办理
一比一原版(Exon毕业证书)英国埃克塞特大学毕业证如何办理一比一原版(Exon毕业证书)英国埃克塞特大学毕业证如何办理
一比一原版(Exon毕业证书)英国埃克塞特大学毕业证如何办理
 
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
 
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
 
iThome_CYBERSEC2024_Drive_Into_the_DarkWeb
iThome_CYBERSEC2024_Drive_Into_the_DarkWebiThome_CYBERSEC2024_Drive_Into_the_DarkWeb
iThome_CYBERSEC2024_Drive_Into_the_DarkWeb
 
TORTOGEL TELAH MENJADI SALAH SATU PLATFORM PERMAINAN PALING FAVORIT.
TORTOGEL TELAH MENJADI SALAH SATU PLATFORM PERMAINAN PALING FAVORIT.TORTOGEL TELAH MENJADI SALAH SATU PLATFORM PERMAINAN PALING FAVORIT.
TORTOGEL TELAH MENJADI SALAH SATU PLATFORM PERMAINAN PALING FAVORIT.
 
一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理
 
一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书
 
一比一定制(OSU毕业证书)美国俄亥俄州立大学毕业证学位证书
一比一定制(OSU毕业证书)美国俄亥俄州立大学毕业证学位证书一比一定制(OSU毕业证书)美国俄亥俄州立大学毕业证学位证书
一比一定制(OSU毕业证书)美国俄亥俄州立大学毕业证学位证书
 
Statistical Analysis of DNS Latencies.pdf
Statistical Analysis of DNS Latencies.pdfStatistical Analysis of DNS Latencies.pdf
Statistical Analysis of DNS Latencies.pdf
 
Development Lifecycle.pptx for the secure development of apps
Development Lifecycle.pptx for the secure development of appsDevelopment Lifecycle.pptx for the secure development of apps
Development Lifecycle.pptx for the secure development of apps
 
GOOGLE Io 2024 At takes center stage.pdf
GOOGLE Io 2024 At takes center stage.pdfGOOGLE Io 2024 At takes center stage.pdf
GOOGLE Io 2024 At takes center stage.pdf
 
AI Generated 3D Models | AI 3D Model Generator
AI Generated 3D Models | AI 3D Model GeneratorAI Generated 3D Models | AI 3D Model Generator
AI Generated 3D Models | AI 3D Model Generator
 
一比一原版(PSU毕业证书)美国宾州州立大学毕业证如何办理
一比一原版(PSU毕业证书)美国宾州州立大学毕业证如何办理一比一原版(PSU毕业证书)美国宾州州立大学毕业证如何办理
一比一原版(PSU毕业证书)美国宾州州立大学毕业证如何办理
 
一比一原版加拿大多伦多大学毕业证(UofT毕业证书)如何办理
一比一原版加拿大多伦多大学毕业证(UofT毕业证书)如何办理一比一原版加拿大多伦多大学毕业证(UofT毕业证书)如何办理
一比一原版加拿大多伦多大学毕业证(UofT毕业证书)如何办理
 
Thank You Luv I’ll Never Walk Alone Again T shirts
Thank You Luv I’ll Never Walk Alone Again T shirtsThank You Luv I’ll Never Walk Alone Again T shirts
Thank You Luv I’ll Never Walk Alone Again T shirts
 
原版定制(Management毕业证书)新加坡管理大学毕业证原件一模一样
原版定制(Management毕业证书)新加坡管理大学毕业证原件一模一样原版定制(Management毕业证书)新加坡管理大学毕业证原件一模一样
原版定制(Management毕业证书)新加坡管理大学毕业证原件一模一样
 
一比一原版(TRU毕业证书)温哥华社区学院毕业证如何办理
一比一原版(TRU毕业证书)温哥华社区学院毕业证如何办理一比一原版(TRU毕业证书)温哥华社区学院毕业证如何办理
一比一原版(TRU毕业证书)温哥华社区学院毕业证如何办理
 
Premier Mobile App Development Agency in USA.pdf
Premier Mobile App Development Agency in USA.pdfPremier Mobile App Development Agency in USA.pdf
Premier Mobile App Development Agency in USA.pdf
 

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