Rest Ruby On Rails

  • View
    2.478

  • Download
    23

Embed Size (px)

DESCRIPTION

 

Text of Rest Ruby On Rails

  • 1. REST&R ubyo nR ails [email_address]

2. Kude Labs

  • http://kudelabs.com
  • Web Applications
    • Schedule.ph
    • PRDGuide.com
    • FriendsMap.net
  • Web Wednesday http://kudelabs.com/category/web-wednesday-guangzhou

3. R E S T 4.

  • REST
    • Resource
    • CRUD
    • HTTP Verb
  • Ruby on Rails
  • RESTful Login & Search?
  • Twitter & Facebook API
  • Tips
  • Q & A

5. REST

  • Re presentationalS tateT ransfer
    • Client-server
    • Stateless
    • Cacheable
    • Layered system
    • Code on Demand*
    • Uniform interface

6. Just an Architecture, Not a Standard or Protocol Describe Internet Based Remote System 7. R emoteP rocedureC alls

  • SOAP
    • REST
  • Facebook API Flickr API API
    • http://api.flickr.com/services/rest/?method= flickr.photos.search
    • http://gw.api.tbsandbox.com/router/rest?method= taobao.taobaoke.items.get

8. RESTvsRPC

  • RPC
      • getUsers()
      • getNewUsersSince(date)
      • savePurchaseOrder()
    • POST
    • HTTP
      • Authentication
      • Caching
      • Content type
    • SOAPHTTP
  • REST
    • HTTP
      • GET
      • POST
      • PUT
      • DELETE
    • HTTP
    • Stateful Resources MessagesOperations

9. Resource CRUD HTTP Verb 10. Resource

  • Resouce
  • + (representation)
    • Resource
    • ResourceURL
    • generic connector interface (HTTP)
    • ResourceURL
    • Stateless

11. CRUD & HTTP Verb

  • ResourceC reate R ead U pdateD elete
  • HTTP Verb
    • curl -X GET http://example.com/projects
    • curl -X DELETE http://example.com/projects/1

SQL CRUD HTTP DELETE DELETE DELETE UPDATE UPDATE PUT SELECT READ GET INSERT CREATE POST 12. REST Ruby on Rails 13. Ruby on Rails

  • Ruby
  • MVCM odel- V iew- C ontroller
  • Web Application
  • v1.2.x REST
  • v3 beta 1

14. REST Ruby on Rails

  • 7 ControllerAction
    • Index
    • Show
    • New
    • Edit
    • Create
    • Update
    • Destroy
  • 4HTTP Verb
    • GET
    • POST
    • PUT
    • DELETE

15. POST /projects/create create /projects POST POST /projects/update/1 update /projects/1 PUT GET /projects/destroy/1 destroy /projects/1 DELETE GET /projects/show/1 show /projects/1 GET URL without REST Action URL with REST HTTP Verb 16.

  • classUsersController
  • # GET /users/1
  • defshow()
  • end
  • # POST /users
  • defcreate()
  • end
  • # PUT /users/1
  • defupdate()
  • end
  • # DELETE /users/1
  • defdestroy()
  • end
  • end

17.

  • classUsersController
  • # GET /users/new
  • defnew ; end
  • # GET /users/1/edit
  • defedit ; end
  • # GET /users
  • defindex ; end
  • # GET /users/1
  • defshow ; end
  • # POST /users
  • defcreate ; end
  • # PUT /users/1
  • defupdate ; end
  • # DELETE /users/1
  • defdestroy ; end
  • end

18. User + Group = ? 19.

  • UserGroups
  • GroupUsers

Users UsersGroups Groups 1 N N 1 20.

  • classGroup
  • has_many:users_groups
  • has_many:users , :through =>:users_groups
  • end
  • classUser
  • has_many:users_groups
  • has_many:groups , :through =>:users_groups
  • end
  • classUsersGroups
  • belongs_to:groups
  • belongs_to:users
  • end

21. Use Case User join Group User leave Group 22.

  • UsersController
    • join_group()
    • # POST /users/1/join_group?group_id=2
    • leave_group()
    • # POST /users/1/leave_group?group_id=2
  • GroupsController
    • add_user()
    • # POST /groups/1/add_user?user_id=2
    • remove_user()
    • # POST /groups/1/remove_user?user_id=2

23. User+Group=UserGroup Membership 24.

  • class Group < ActiveRecord::Base
  • has_many:memberships
  • has_many :users, :through =>:memberships
  • end
  • class User < ActiveRecord::Base
  • has_many:memberships
  • has_many :groups, :through =>:memberships
  • end
  • classMembership< ActiveRecord::Base
  • belongs_to :groups
  • belongs_to :users
  • end

25.

  • MembershipsController
    • create()
    • # INSERT INTO "memberships" ("group_id", "user_id") VALUES(2, 1)
    • # POST /memberships?user_id=1&group_id=2
    • destroy()
    • # DELETE FROM "memberships" WHERE "id" = 1
    • # DELETE /memberships/3

26. mine types

  • classUsersController
  • # GET /users
  • # GET /users.xml
  • defindex
  • @users = User.all
  • respond_to do |format|
  • format.html # index.html.erb
  • format.js{ render :json => @users }
  • format.xml{ render :xml => @users }
  • fomrat.atom do
  • render :action => atom, :content_type => Mine::ATOM
  • end
  • end
  • end
  • end

27.

  • GET /users=> HTML
  • GET /users.xml=> XML
  • Accept: text/javascript GET /users=> json
  • Accept: text/html GET /users.xml=> XML

28. Nested Resrouce

  • GET/groups
  • GET/groups/1
  • DELETE /groups/1
  • POST/groups
  • GET/users
  • GET/users/2
  • DELETE /users/2
  • POST/users

GroupUsers 29.

  • map.resources:groupsdo |groups|
  • groups.resources:users
  • end
  • GET/groups/1/users
  • GET/groups/1/users/2
  • DELETE /groups/1/users/2
  • POST/groups/1/users

30. RESTAjax 31.

  • HTTP
  • REST

32. RESTful Login & Search? 33.

  • Login
  • POST /login?user=shaokun&password=***
  • POST /signin?user=shaokun&password=***
  • https://reg.163.com/logins.jsp
  • http://ptlogin2.qq.com/login

34.

  • LoginResource
  • LoginHTTP Verb
  • LoginController Action

35.

  • classSessionsController
  • # GET /sessions
  • defnew
  • end
  • # POST /sessions
  • defcreate
  • en