ActiveResource & REST

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    6 Favorites

    ActiveResource & REST - Presentation Transcript

    1. REST & ActiveResource Matthijs Langenberg
    2. Webservices
    3. Wat zijn webservices
    4. ”The W3C defines a Web Service as a software system designed to support interoperable machine to machine interaction over a network.” -- Wikipedia
    5. Bevorder ‘machine to machine interaction’
    6. HTML is moeilijk te parsen
    7. Geef iets anders terug XML?
    8. Just Another View
    9. respond_to
    10. class ArticlesController < ApplicationController def show @article = Article.find(params[:id]) respond_to do |format| format.html format.xml { render :xml => @article } end end end
    11. Soorten Webservices • Remote procedure calls (RPC) • Service-oriented architecture (SOA) • Representational state transfer (REST)
    12. Rails votes REST
    13. Rails votes REST BIG TIME!
    14. Wat is REST?
    15. REpresentional State Transfer
    16. HTTP’s: “convention over configuration”
    17. Schreef geen vervanging voor iets wat HTTP je gratis geeft
    18. HTTP Abuse Wat is er mis met dit request? GET http://myblog.com/articles/destroy/1
    19. HTTP Abuse Wat is er mis met dit request? GET http://myblog.com/articles/destroy/1 Conflict
    20. HTTP Abuse Wat is er mis met dit request? GET http://myblog.com/articles/destroy/1 Conflict • Actie staat in URL • Uitgevoerd actie is in conflict met HTTP methode
    21. The REST-way DELETE http://myblog.com/articles/1
    22. URI’s
    23. URI’s GET /articles/create GET /articles/show/1 GET /articles/update/1 GET /articles/destroy/1
    24. URI’s GET /articles/create GET /articles/show/1 GET /articles/update/1 GET /articles/destroy/1
    25. Mapping
    26. Mapping HTTP GET POST PUT DELETE
    27. Mapping HTTP Controller GET SHOW POST CREATE PUT UPDATE DELETE DESTROY
    28. Resourceful URI’s
    29. Resourceful URI’s GET /articles POST /articles/create GET /articles/show/1 POST /articles/update/1 GET /articles/destroy/1
    30. Resourceful URI’s ➡ GET /articles GET /articles ➡ POST /articles POST /articles/create ➡ GET /articles/1 GET /articles/show/1 ➡ PUT /articles/1 POST /articles/update/1 ➡ DELETE /articles/1 GET /articles/destroy/1
    31. Gratis named routes
    32. ActionController::Routing::Routes.draw do |map| map.resources :articles do |articles| articles.resources :comments end end
    33. ActionController::Routing::Routes.draw do |map| map.resources :articles do |articles| articles.resources :comments end end
    34. ActionController::Routing::Routes.draw do |map| map.resources :articles do |articles| articles.resources :comments end end articles_url article_url new_article_url edit_article_url article_comments_url article_comment_url article_new_comment_url article_edit_comment_url
    35. ActionController::Routing::Routes.draw do |map| map.resources :articles do |articles| articles.resources :comments end end articles_url ➡ /articles article_url ➡ /articles/:id new_article_url ➡ /articles/new edit_article_url ➡ /articles/:id/edit article_comments_url ➡ /articles/:article_id/comments article_comment_url ➡ /articles/:article_id/comments/:id article_new_comment_url ➡ /articles/:article_id/comments/new article_edit_comment_url ➡ /articles/:article_id/comments/:id/edit
    36. link_to article.title, { :controller => ‘article’, :action => ‘show’, :id => article }
    37. link_to article.title, { :controller => ‘article’, :action => ‘show’, :id => article }
    38. link_to article.title, { :controller => ‘article’, :action => ‘show’, :id => article } link_to article.title, article_url(article)
    39. link_to article.title, { :controller => ‘article’, :action => ‘show’, :id => article } link_to article.title, article_url(article) link_to article.title, article
    40. Wat zou je doen? Je wilt comments aan articles toevoegen, ArticlesController is aanwezig.
    41. Wat zou je doen? Je wilt comments aan articles toevoegen, ArticlesController is aanwezig. 1) Voeg een actie ‘add_comment’ aan ArticlesController toe. (POST /articles/1/add_comment)
    42. Wat zou je doen? Je wilt comments aan articles toevoegen, ArticlesController is aanwezig. 1) Voeg een actie ‘add_comment’ aan ArticlesController toe. (POST /articles/1/add_comment) 2) Maak een CommentsController, met een ‘create’ actie. (POST /comments/create?article_id=1)
    43. Mr. RESTful zegt:
    44. Mr. RESTful zegt: Antwoord 2
    45. Mr. RESTful zegt: Antwoord 2 • Een comment is een een aparte resource
    46. Mr. RESTful zegt: Antwoord 2 • Een comment is een een aparte resource • Er bestaat geen ‘add_comment’ methode in HTTP
    47. Mr. RESTful zegt: Antwoord 2 • Een comment is een een aparte resource • Er bestaat geen ‘add_comment’ methode in HTTP • Er bestaat wel een ‘create’ (POST) methode in HTTP
    48. Geen Namespaces! • POST /articles/create • POST /articles/create_comment • GET /articles/destroy • GET /articles/destroy_comment
    49. Teveel vrijheid is niet goed
    50. class ArticlesController < ApplicationController def show @article = Article.find(params[:id]) end def show_rss @article = Article.find(params[:id]) render :rss => @article.to_rss end def show_atom @article = Article.find(params[:id]) render :atom => @article.to_atom end def show_xml @article = Article.find(params[:id]) render :xml => @article.to_xml end def show_ajax @article = Article.find(params[:id]) render :template => show_article.rjs end end
    51. Geen aparte actie voor alternatieve view!
    52. class ArticlesController < ApplicationController def show @article = Article.find(params[:id]) respond_to do |format| format.html format.rss { render :rss => @article.to_rss } format.atom { render :atom => @article.to_atom } format.xml { render :xml => @article.to_xml } format.rjs { render :template => ‘show_article.rjs’ } end end end
    53. Wauw! HTTP method naar controller actie mapping actie klinkt tof!
    54. Maar er zit een adder ...
    55. Maar er zit een adder ...
    56. Browsers ondersteunen PUT en DELETE niet!
    57. Browsers ondersteunen PUT en DELETE niet! <input name=\"_method\" type=\"hidden\" value=\"put\" />
    58. Gelukkig zijn de helpers ook aangepast. ;-)
    59. HTML_options • link_to “delete”, article_path(1), :method => ‘delete’ • link_to_remote, “delete”, article_path(1), :method => ‘delete’ • form_tag(member_path(2), :method => :put)
    60. form_for remote_form_for Bepalen op basis van AR object de method: form_for(Movie.new): <form action=\"/movies\" class=\"new_movie\" id=\"new_movie\" method=\"post\"> form_for(Movie.find(:first)): <form action=\"/movies/1\" class=\"edit_movie\" id=\"edit_movie_1\" method=\"post\"> <input name=\"_method\" type=\"hidden\" value=\"put\" />
    61. Controller Acties
    62. MoviesController#index
    63. MoviesController#index # GET /movies # GET /movies.xml def index @movies = Movie.find(:all) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @movies } end end
    64. MoviesController#index <?xml version=\"1.0\" encoding=\"UTF-8\"?> # GET /movies <movies> # GET /movies.xml <movie> def index <director>Chris Miller</director> @movies = Movie.find(:all) <id type=\"integer\">1</id> <rating type=\"decimal\">7.0</rating> respond_to do |format| <title>Shrek the Third</title> format.html # index.html.erb </movie> <movie> format.xml { render :xml => @movies } <director>Sam Raimi</director> end <id type=\"integer\">2</id> end <rating type=\"decimal\">6.9</rating> <title>Spider-Man 3</title> </movie> <movie> <director>Juan Carlos Fresnadillo</director> <id type=\"integer\">3</id> <rating type=\"decimal\">7.7</rating> <title>28 Weeks Later</title> </movie> </movies>
    65. MoviesController#show
    66. MoviesController#show # GET /movies/1 # GET /movies/1.xml def show @movie = Movie.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @movie } end end
    67. MoviesController#show # GET /movies/1 <?xml version=\"1.0\" encoding=\"UTF-8\"?> <movie> # GET /movies/1.xml <director>Chris Miller</director> def show <id type=\"integer\">1</id> @movie = Movie.find(params[:id]) <rating type=\"decimal\">7.0</rating> <title>Shrek the Third</title> respond_to do |format| </movie> format.html # show.html.erb format.xml { render :xml => @movie } end end
    68. MoviesController#create
    69. MoviesController#create # POST /movies # POST /movies.xml def create @movie = Movie.new(params[:movie]) respond_to do |format| if @movie.save flash[:notice] = 'Movie was successfully created.' format.html { redirect_to(@movie) } format.xml { render :xml => @movie, :status => :created, :location => @movie } else format.html { render :action => \"new\" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
    70. MoviesController#create Status: 201 Created Location: http://localhost:3000/movies/16 # POST /movies <?xml version=\"1.0\" encoding=\"UTF-8\"?> # POST /movies.xml <movie> def create <director>Steven Spielbergh</director> @movie = Movie.new(params[:movie]) <id type=\"integer\">15</id> <rating type=\"decimal\">8.3</rating> respond_to do |format| <title>Letters from Iwo Jima</title> if @movie.save </movie> flash[:notice] = 'Movie was successfully created.' format.html { redirect_to(@movie) } format.xml { render :xml => @movie, :status => :created, :location => @movie } else format.html { render :action => \"new\" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
    71. MoviesController#create Status: 201 Created Location: http://localhost:3000/movies/16 # POST /movies <?xml version=\"1.0\" encoding=\"UTF-8\"?> # POST /movies.xml <movie> def create <director>Steven Spielbergh</director> @movie = Movie.new(params[:movie]) <id type=\"integer\">15</id> <rating type=\"decimal\">8.3</rating> respond_to do |format| <title>Letters from Iwo Jima</title> if @movie.save </movie> flash[:notice] = 'Movie was successfully created.' format.html { redirect_to(@movie) } format.xml { render :xml => @movie, :status => :created, Status: 422 Unprocessable Entity :location => @movie } <?xml version=\"1.0\" encoding=\"UTF-8\"?> else <errors> format.html { render :action => \"new\" } <error>Rating can't be blank</error> format.xml { render :xml => @movie.errors, :status => 422 } <error>Director can't be blank</error> end <error>Title can't be blank</error> end </errors> end
    72. MoviesController#update
    73. MoviesController#update # PUT /movies/1 # PUT /movies/1.xml def update @movie = Movie.find(params[:id]) respond_to do |format| if @movie.update_attributes(params[:movie]) flash[:notice] = 'Movie was successfully updated.' format.html { redirect_to(@movie) } format.xml { head :ok } else format.html { render :action => \"edit\" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
    74. MoviesController#update Status: 200 OK # PUT /movies/1 # PUT /movies/1.xml def update @movie = Movie.find(params[:id]) respond_to do |format| if @movie.update_attributes(params[:movie]) flash[:notice] = 'Movie was successfully updated.' format.html { redirect_to(@movie) } format.xml { head :ok } else format.html { render :action => \"edit\" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
    75. MoviesController#update Status: 200 OK # PUT /movies/1 # PUT /movies/1.xml def update @movie = Movie.find(params[:id]) Status: 422 Unprocessable Entity respond_to do |format| <?xml version=\"1.0\" encoding=\"UTF-8\"?> if @movie.update_attributes(params[:movie]) <errors> flash[:notice] = 'Movie was successfully updated.' <error>Title can't be blank</error> format.html { redirect_to(@movie) } </errors> format.xml { head :ok } else format.html { render :action => \"edit\" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
    76. MoviesController#destroy
    77. MoviesController#destroy # DELETE /movies/1 # DELETE /movies/1.xml def destroy @movie = Movie.find(params[:id]) @movie.destroy respond_to do |format| format.html { redirect_to(movies_url) } format.xml { head :ok } end end
    78. MoviesController#destroy # DELETE /movies/1 Status: 200 OK # DELETE /movies/1.xml def destroy @movie = Movie.find(params[:id]) @movie.destroy respond_to do |format| format.html { redirect_to(movies_url) } format.xml { head :ok } end end
    79. Scaffolding
    80. maar er is meer!
    81. ActiveResource
    82. ActiveResource • Object-oriented REST services • Transparent met een RESTful service (Rails) werken • Net als ActiveRecord, maar dan voor REST
    83. Browser
    84. Browser GET /movies.html Controller (RESTful)
    85. Browser GET /movies.html Controller (RESTful) Movie.find(:all) ActiveRecord
    86. Browser GET /movies.html Controller (RESTful) Movie.find(:all) ActiveRecord SELECT * FROM MOVIES DB
    87. Browser Browser GET /movies.html Controller (RESTful) Movie.find(:all) ActiveRecord SELECT * FROM MOVIES DB
    88. Browser Browser GET /movies.html GET /movies.html Controller Controller (RESTful) Movie.find(:all) ActiveRecord SELECT * FROM MOVIES DB
    89. Browser Browser GET /movies.html GET /movies.html Controller Controller (RESTful) Movie.find(:all) Movie.find(:all) ActiveResource ActiveRecord SELECT * FROM MOVIES DB
    90. Browser Browser GET /movies.html GET /movies.html Controller Controller (RESTful) Movie.find(:all) Movie.find(:all) GET /movies.xml ActiveResource ActiveRecord SELECT * FROM MOVIES DB
    91. Configuratie
    92. Werken met RESTful webservices (ActiveResource)
    93. Create
    94. Create
    95. Read
    96. Read
    97. Update
    98. Update
    99. Destroy
    100. Destroy
    101. Wat doet ARes?
    102. Wat doet ARes? • Genereer URL
    103. Wat doet ARes? • Genereer URL • Request URL (XML)
    104. Wat doet ARes? • Genereer URL • Request URL (XML) • Verwerk request (XML)
    105. Wat doet ARes? • Genereer URL • Request URL (XML) • Verwerk request (XML) • Biedt ActiveRecord-like API
    106. Demo
    107. Demo 1) RESTful Rails applicatie (met curl)
    108. Demo 1) RESTful Rails applicatie (met curl) 2) Rails applicatie met ActiveResource
    109. Demo 1) RESTful Rails applicatie (met curl) 2) Rails applicatie met ActiveResource 3) ActiveResource gem
    110. Vragen? <mlangenberg@gmail.com> #rubyenrails at irc.freenode.org

    + RobbertRobbert, 3 years ago

    custom

    3904 views, 6 favs, 1 embeds more stats

    Matthijs Langenberg's slide's of his presentation a more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 3904
      • 3891 on SlideShare
      • 13 from embeds
    • Comments 0
    • Favorites 6
    • Downloads 215
    Most viewed embeds
    • 13 views on http://2007.rubyenrails.nl

    more

    All embeds
    • 13 views on http://2007.rubyenrails.nl

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories