Rest and Rails
Upcoming SlideShare
Loading in...5
×
 

Rest and Rails

on

  • 3,757 views

 

Statistics

Views

Total Views
3,757
Views on SlideShare
2,995
Embed Views
762

Actions

Likes
2
Downloads
33
Comments
0

36 Embeds 762

http://chamnapchhorn.blogspot.com 416
http://chamnap.github.com 99
http://chamnapchhorn.blogspot.in 70
http://chamnapchhorn.blogspot.de 37
http://chamnapchhorn.blogspot.co.uk 18
http://chamnapchhorn.blogspot.fr 17
http://localhost:4000 15
http://chamnapchhorn.blogspot.ca 11
http://chamnapchhorn.blogspot.dk 10
http://chamnapchhorn.blogspot.jp 8
http://chamnapchhorn.blogspot.ru 6
http://chamnapchhorn.blogspot.com.es 4
http://chamnapchhorn.blogspot.com.br 4
http://feeds2.feedburner.com 4
http://chamnapchhorn.blogspot.co.il 4
http://chamnapchhorn.blogspot.nl 4
http://chamnap.github.io 3
http://www.linkedin.com 3
http://chamnapchhorn.blogspot.sg 3
http://www.chamnapchhorn.blogspot.com 3
http://chamnapchhorn.blogspot.com.tr 3
http://chamnapchhorn.blogspot.hk 2
http://chamnapchhorn.blogspot.ie 2
http://chamnapchhorn.blogspot.co.at 2
http://chamnapchhorn.blogspot.ch 2
http://www.chamnapchhorn.blogspot.de 2
http://chamnapchhorn.blogspot.co.nz 1
http://webcache.googleusercontent.com 1
http://chamnapchhorn.blogspot.it 1
http://chamnapchhorn.blogspot.ro 1
http://feeds.feedburner.com 1
http://chamnapchhorn.blogspot.kr 1
http://chamnapchhorn.blogspot.tw 1
http://chamnapchhorn.blogspot.sk 1
http://chamnapchhorn.blogspot.gr 1
http://chamnapchhorn.blogspot.se 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Rest and Rails Rest and Rails Presentation Transcript

    • REST And rails
      Chhorn Chamnap
      YoolkMango
      15 - July - 2010
    • Agenda
      REST Theory
      RESTful Rails
      Case Study
      Authentication
      References
    • REST Theory
    • REST Introduction
      REST is a unifying theory for how “distributed hypermedia” systems are best organized and structured.
      Lesson learnt from developers:
      CRUD operations correspond to HTTP POST, GET, PUT, and DELETE.
      Consistent, robust, and understandable.
      Names identifies resources
    • Resources
      A resource is something with identity.
      a row in adatabase, a physical object, an abstract concept, or a real-world event in progress
      A resource has a URI.
      Possible to have more than one???
      Different representations of a resource vary based on their content types.
      How does the server know which one to send?
      URI extensions (/users/1.html,/users/1.xml)
      Content negotiation (Accept-Language, Accept-Charset, Accept-Encoding, or Accept)
    • Resources (example)
      GET /orders/124 HTTP/1.1
      Host: www.example.com
      Accept: text/html, application/xhtml+xml, text/*, image/png, image/*, */*
    • Embrace hyperlinks
      Use hyperlinks to related resources.
      Provide a reasonable quantity of information and link to further details.
    • Statelessness
      REST is stateless.
      It presents scalibility.
      Each request carries no state at lower or higher levels.
      Resource state
      the internal state that all non trivial resources carry, and it is essential to a web application.
      Application state (session state)
      the state of the cli-ent’s interaction with the server
      keeping this state on the server violates REST principles as it breaks addressability.
    • HTTP Verbs (HTTP Methods)
      Verbs correspond to actions on resources.
      GET
      HEAD
      POST
      PUT
      DELETE
    • Safe Methods
      Safe methods are used for retrieval.
      never be to perform an update
      All safe methods are idempotent.
    • Idempotent Methods
      GET, HEAD, PUT, and DELETE are idempotent methods.
      The response (and resource state) is the same, no matter how many times thataction is performed.
    • HTTP Status Codes
      Success and failure should be inferred from the HTTP response status
      not from an error message within the payload.
      1xx: Informational
      2xx: Success
      3xx: Redirection
      4xx: Client Error
      5xx: Server Error
    • GET Method
      Transfers a representation of a resource to the client.
      Read-only access to a resource.
      The server must decide to perform an update based on a safe request.
    • PUT Method
      Updates a resource with the representation provided in the body.
      If not exist before, the request creates a new one.
    • DELETE Method
      Deletes the resource identified by its URI.
      Subsequent GET queries to the same URI should return a status code of 410 (Gone) or 404 (Not Found).
    • POST Method
      Neither safe nor idempotent
      Two primary uses:
      creation of new objects
      annotation of existing objects
      The URI of the POST is that of the object’s container or parent.
      The Location header should point to the URI of the created resource
    • RESTful Rails
    • Resource-Based Named Routes
      Encapsulates all of the Rails CRUD actions into one routing statement
      map.resources :users
    • Custom resource routes
      create custom named routes either to the collection (the parent resource) or the members of the collection (the children).
      map.resources :people, :collection => { :search => :get }, :member => { :deactivate => :post }
    • Nested routes
      map.resources :people do |person|
      person.resources :friends
      end
      /people/1/friends
      /people/1/friends/2
      map.resources :people do |person|
      person.resources :friends, :name_prefix => 'person_'
      end
      The name _prefix option adds a prefix to the generated routes.
      person_friends_path and person_friend_path
    • Nested routes (cont.)
      map.resources :people
      map.resources :friends,
      :name_prefix => 'person_',
      :path_prefix => '/people/:person_id‘
      path_prefix option will add a prefix to the URIs that the route will recognize and generate.
    • Singleton resource routes
      Sometimes, there will be an entity that exists as a singleton.
      map.resources :users do |user|
      user.resource :account
      end
      The resource name is still singular, but the inferred controller name is plural.
    • ActionView Support
      The link_to family of helpers can take a :method parameter to define the HTTP method.
      generate hidden form field for the _method parameter for PUT and DELETE.
      <%= link_to 'Delete', person_path(@person), :method => :delete %>
    • Content Types
      Rails has introduced rich support for rendering different responses based on the content type the client wants, via the respond_to method.
      respond_to do |format|
      format.html #format.html { render }
      format.xml { render :xml => @product }
      end
      respond_to :html, :xml
      In config/initializers/mime_types.rb
      Mime::Type.register "image/jpeg", :jpg, [], %w(jpeg)
    • Content Types (cont.)
    • Content Types (cont.)
    • Resourceful session state
      Alternative to holding session state on the server?
      Nearly any problem REST developers face, the solution is to model it as a resource.
    • Case Study
    • Example
    • Refactor
    • Refactor (example)
    • Refactor (example)
    • Authentication
    • Authentication
      Can we used cookies?
      Yes, cookies can be used, but mainly for authentication.
      How to authenticate users in a RESTful way via the browser and other clients?
    • Authentication (cont.)
      Use cookies/sessions to store information just for authentication.
      Use HTTP Basic authentication for other server side clients.
      For more secure, use secure http.
    • Authentication (cont.)
    • Authentication (cont.)
    • References
      Advanced Rails Recipes
      OReilly Advanced Rails
      Oreilly RESTful Web Services
      http://ajaxpatterns.org/RESTful_Service