Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Rest in Rails

1 723 vues

Publié le

Publié dans : Technologie
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

Rest in Rails

  1. 1. REST in Rails Chhorn Chamnap
  2. 2. What is REST? <ul><li>The REST style characterizes communication between system components (where a component is, say, a web browser or a server) as a series of requests to which the responses are representations of resources. </li></ul><ul><li>What you actually do get hold of is never the resource itself, but a representation of it. </li></ul><ul><li>A resource may, at any given point, be available in any number of representations. </li></ul>
  3. 3. Resources as URLs <ul><li>A resource is something like a &quot;business entity&quot; in modeling lingo. It's an entity you wish to expose as part of an API. Almost always, the entity is a noun, e.g. a person, a car, or a football match. </li></ul><ul><li>Each resource is represented as a unique URL. </li></ul>
  4. 4. Operations as HTTP methods
  5. 6. Services should be stateless <ul><li>An objective of REST is to be able to switch clients at any time and receive the same result. </li></ul><ul><li>So are cookies used at all? Yes, cookies can be used, but mainly for authentication. </li></ul><ul><li>They should only affect if the user can make a call, but not how the server responds. </li></ul>
  6. 7. Services should be idempotent <ul><li>&quot;Idempotent&quot; means that once you pass a message to service, there's no additional effect of passing the same message again. </li></ul>
  7. 8. Benefits of Rails’REST <ul><li>Convenience and automatic best practices for you </li></ul><ul><li>A REST interface to your application’s services, for everyone else </li></ul>
  8. 9. A Short Notice <ul><li>There’s a temptation to call your actions add_item and replace_email_address and things like that. But things get simpler when you name your actions after CRUD operations, or as close to the names of those operations as you can get. </li></ul>
  9. 10. map.resources :books <ul><li>You will have created four named routes and allow you to connect to seven controller actions. </li></ul><ul><li>In most cases, is that they have created a Book model, a book controller with a set of CRUD actions, and some named routes pertaining to that controller (courtesy of map.resources :books ). </li></ul>
  10. 11. :only, :except <ul><li>Each of routes generated by map.resources takes up memory in your application, and causes Rails to generate additional routing logic. </li></ul><ul><li>Luckily, since Rails 2.2 you can use the :only and :except options to fine-tune the routes that Rails will generate for resources. You can supply a single action, an array of actions, or the special :all or :none options. </li></ul>
  11. 12. named route
  12. 13. PUT and DELETE <ul><li>A PUT or DELETE request, in the context of REST in Rails, is actually a POST request with a hidden field called _method set to either “put” or “delete”. The Rails application processing the request will pick up on this, and route the request appropriately to the update or destroy action. </li></ul>
  13. 14. <ul><li>Allows you to introspect all of the routes your application recognizes. </li></ul>rake routes command
  14. 15. map.resource <ul><li>Imagine, for instance, that you have a resource that could exist only one, a blog. You don’t need an index action for the blog resource, since there is only (and can only ever be) one blog in the application. </li></ul><ul><li>map.resource is designed for just this situation. You use it in just the same way as its plural relative. </li></ul>
  15. 16. map.resource <ul><li>map.resource :blog </li></ul>
  16. 17. map.namespace <ul><li>In some cases—administration interfaces, for instance—you may need to namespace a resource without actually nesting it under another.​ For example: /admin/posts </li></ul><ul><li>map.namespace :admin do |admin| </li></ul><ul><li> map.resources :posts </li></ul><ul><li>end </li></ul>
  17. 18. Nested Resources <ul><li>/auctions/1/bids/5 </li></ul><ul><ul><li>map.resources :auctions do |auction| </li></ul></ul><ul><ul><ul><li>auction.resources :bids </li></ul></ul></ul><ul><ul><li>end </li></ul></ul><ul><li>auction_bids_url, auction_bids_path, new_auction_bid_url, edit_auction_bid_url </li></ul><ul><li>Whenever you use the user named routes, you will provide a group, resource in which they can be nested. </li></ul>
  18. 19. Nested Resources <ul><li><%= link_to “See all bids”, auction_bids_path(@auction) %> </li></ul><ul><li><%= link_to “Delete this bid”, auction_bid_path(@auction, @bid), :method => :delete %> </li></ul><ul><li>You can nest to any depth. Each level of nesting adds one to the number of arguments you have to supply to the nested routes. </li></ul>
  19. 20. Deep Nesting? <ul><li>Resources should never be nested more than one level deep. </li></ul><ul><li>map.resources :auctions do |auctions| </li></ul><ul><li>auctions.resources :bids do |bids| </li></ul><ul><li>bids.resources :comments </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
  20. 21. Deep Nesting? (cont.)‏ <ul><li>map.resources :auctions do |auctions| </li></ul><ul><li>auctions.resources :bids </li></ul><ul><li>end </li></ul><ul><li>map.resources :bids do |bids| </li></ul><ul><li>bids.resources :comments </li></ul><ul><li>end </li></ul><ul><li>map.resources :comments </li></ul><ul><li>auctions_path # /auctions </li></ul><ul><li>auctions_path(1) # /auctions/1 </li></ul><ul><li>auction_bids_path(1) # /auctions/1/bids </li></ul><ul><li>bid_path(2) # /bids/2 </li></ul><ul><li>bid_comments_path(3) # /bids/3/comments </li></ul><ul><li>comment_path(4) # /comments/4 </li></ul>
  21. 22. :path_prefix <ul><li>map.resources :auctions </li></ul><ul><li>map.resources :bids, :path_prefix => “auctions/:auction_id” </li></ul><ul><li>What you’re saying here is that you want all of the bids URLs to include the static string “auctions” and a value for auction_id . </li></ul><ul><li>The main difference has to do with the naming of the helper methods that are generated. Nested resources automatically get a name prefix corresponding to their parent resource. </li></ul>
  22. 23. : name_prefix <ul><li>/auctions/2/bids/5 </li></ul><ul><li>/auctions/5 </li></ul><ul><li>map.resources :auctions do |auction| </li></ul><ul><li>auction.resources :bids, :name_prefix => nil </li></ul><ul><li>end </li></ul><ul><li>auction_path(@auction, @bid)‏ </li></ul><ul><li>auction_path(@auction)‏ </li></ul>
  23. 24. RESTful Controllers <ul><li>It was just presented as something that happens automatically, which in fact it does, based on the name of the resource. </li></ul><ul><li>map.resources :auctions do |auction| </li></ul><ul><ul><ul><li>auction.resources :bids </li></ul></ul></ul><ul><ul><li>end </li></ul></ul><ul><li>Having the option means you can name the (userfacing) resource whatever you want, and keep the name of your controller aligned with different naming standards </li></ul><ul><li>map.resources :my_auctions, :controller => :auctions do |auction| </li></ul><ul><li>auction.resources :my_bids, :controller => :bids </li></ul><ul><li>end </li></ul>
  24. 25. Extra Member Routes <ul><li>For example, let’s say we want to make it possible to retract a bid. </li></ul><ul><li>map.resources :auctions do |auction| </li></ul><ul><li>auction.resources :bids </li></ul><ul><li>end </li></ul><ul><li>retract_bid_url </li></ul><ul><li>map.resources :auctions do |a| </li></ul><ul><ul><li>a.resources :bids, :member => {:retract => :any} </li></ul></ul><ul><li>end </li></ul>
  25. 26. Extra Collection Routes <ul><li>You can also use this routing technique to add routes that conceptually apply to an entire collection of resources </li></ul><ul><li>map.resources :auctions, :collection => { :terminate => :any } </li></ul>
  26. 27. Doing it yourself <ul><li>There are two main circumstances when you might want to hand-code your RESTful routes instead of using the macros: </li></ul><ul><ul><li>when your application does not expose the full set of actions, and </li></ul></ul><ul><ul><li>when your URIs follow a nonstandard pattern. </li></ul></ul>
  27. 28. Doing it yourself