Women Who Code - RSpec JSON API Workshop
description
Transcript of Women Who Code - RSpec JSON API Workshop
![Page 2: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/2.jpg)
Agenda
• 1. Development Process
• 2. API Design
• 3. Rails, RSpec and Gems
• 4. Mini-hackathon
![Page 3: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/3.jpg)
Mini-hackthonbuild your own API
![Page 4: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/4.jpg)
Setup (Mac)Ruby \curl -sSL https://get.rvm.io | bash rvm install 2.1.3
Gems neededgem install bundlergem install rails
Sample project codegit clone http://github.com/3dd13/api_rspec_workshopcd api_rspec_workshopbundle install
![Page 5: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/5.jpg)
1. Development Process
![Page 6: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/6.jpg)
WorkflowInitial• 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
![Page 7: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/7.jpg)
2. API Design
![Page 8: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/8.jpg)
Architecture
Frontend Js Client
Backend Server
HTTP API
![Page 9: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/9.jpg)
42 Labs Stack
AngularJS (Browser or Mobile)
Ruby on Rails / Node.js
RESTful API - JSON
![Page 10: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/10.jpg)
42 Labs Stack
Customer Web(AngularJS)
Customer Mobile(AngularJS)
Admin Web
Ruby on Rails
HTML
JSON API
JSON API
![Page 11: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/11.jpg)
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
![Page 12: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/12.jpg)
Standard / Convention
Always
GET, CREATE, UPDATE, DELETE resource
![Page 13: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/13.jpg)
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]}
![Page 14: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/14.jpg)
Suggestionnot 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
![Page 15: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/15.jpg)
RESTful Authentication
• Step 1: User Sign In
POST http://example.com/users/sessions
body: {email: “[email protected]”, 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
![Page 16: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/16.jpg)
Versioning
• GET http://example.com/api/v1/projects.json
• GET http://example.com/api/projects.jsonAccept: application/vnd.charityspring.v1
![Page 17: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/17.jpg)
3. Rails & RSpecand other gems
![Page 18: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/18.jpg)
Testing
![Page 19: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/19.jpg)
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)
![Page 20: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/20.jpg)
factory_girl
FactoryGirl.define do factory :user do sequence(:email) { |n| "user#{n}@example.com"} password "password" endend
user = FactoryGirl.create(:user)
![Page 21: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/21.jpg)
rspec_api_documentation
rake docs:generate
![Page 22: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/22.jpg)
4. Mini-hackthonbuild your own API
![Page 23: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/23.jpg)
Setup (Mac)Ruby \curl -sSL https://get.rvm.io | bash rvm install 2.1.3
Gems neededgem install bundlergem install rails
Sample project codegit clone http://github.com/3dd13/api_rspec_workshopcd api_rspec_workshopbundle install
![Page 24: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/24.jpg)
Run test cases
rake db:migrate
rake
rake docs:generate
![Page 25: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/25.jpg)
Create seed data
rake db:seed
![Page 26: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/26.jpg)
Code walk through
![Page 27: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/27.jpg)
Note:
1. password is plain text
2. no database index was created
don’t use it in production, feel free to change the code
![Page 28: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/28.jpg)
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
![Page 29: Women Who Code - RSpec JSON API Workshop](https://reader036.fdocuments.us/reader036/viewer/2022062319/55843f89d8b42a84368b4d1f/html5/thumbnails/29.jpg)
Advanced
1. Member wants to pledge and pay via Paypal / Stripe
2. Add a new API endpoint to v2