Agenda
• 1. Development Process
• 2. API Design
• 3. Rails, RSpec and Gems
• 4. Mini-hackathon
Mini-hackthonbuild your own API
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
1. Development Process
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
2. API Design
Architecture
Frontend Js Client
Backend Server
HTTP API
42 Labs Stack
AngularJS (Browser or Mobile)
Ruby on Rails / Node.js
RESTful API - JSON
42 Labs Stack
Customer Web(AngularJS)
Customer Mobile(AngularJS)
Admin Web
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]}
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
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
Versioning
• GET http://example.com/api/v1/projects.json
• GET http://example.com/api/projects.jsonAccept: application/vnd.charityspring.v1
3. Rails & RSpecand 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" endend
user = FactoryGirl.create(:user)
rspec_api_documentation
rake docs:generate
4. Mini-hackthonbuild your own API
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
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