Putting the Hype back in Hypermedia

Putting the Hype back in Hypermedia

Practical REST with Roar

9b1a71682de14fc6fc2b944a9c4814a0?s=128

Alex Coles

June 06, 2012
Tweet

Transcript

  1. Alex Coles Putting the Hype back in Hypermedia – Practical

    REST with Roar Mittwoch, 6. Juni 12
  2. About me Mittwoch, 6. Juni 12

  3. @myabc • @myabc • github.com/myabc • alexbcoles.com Mittwoch, 6. Juni

    12
  4. Where I live • Berlin Mittwoch, 6. Juni 12

  5. Where I work • Payango GmbH Mittwoch, 6. Juni 12

  6. ‘Spare’ Time • Open-source. Check github.com/myabc • Organise eurucamp.org (August

    17 - 19). • Also helping out with Rails Girls. Mittwoch, 6. Juni 12
  7. Standing in for @apotonick Mittwoch, 6. Juni 12

  8. What’s ROAR? Mittwoch, 6. Juni 12

  9. Resource-Oriented Architectures in Ruby Mittwoch, 6. Juni 12

  10. Representations Mittwoch, 6. Juni 12

  11. Let’s detour… REST Mittwoch, 6. Juni 12

  12. REpresentational State Transfer Mittwoch, 6. Juni 12

  13. Look at academic research for REST Mittwoch, 6. Juni 12

  14. Roy Fielding Mittwoch, 6. Juni 12

  15. REST vs SOAP Mittwoch, 6. Juni 12

  16. Mittwoch, 6. Juni 12

  17. ‘You’re doing it wrong’ Mittwoch, 6. Juni 12

  18. REST vs Rails REST Mittwoch, 6. Juni 12

  19. One thing you shouldn’t be too concerned about. Mittwoch, 6.

    Juni 12
  20. URLs Mittwoch, 6. Juni 12

  21. GET http://mysite.com/ search/digital-books Mittwoch, 6. Juni 12

  22. GET http://mysite.com/ search/digital-books RESTful! Mittwoch, 6. Juni 12

  23. GET http://mysite.com/ search.aspx?query=digital- books&parameter=2 Mittwoch, 6. Juni 12

  24. GET http://mysite.com/ search.aspx?query=digital- books&parameter=2 RESTful! Mittwoch, 6. Juni 12

  25. Not XML-RPC Mittwoch, 6. Juni 12

  26. Read more: http://blog.steveklabnik.com/ posts/2011-07-03-nobody- understands-rest-or-http Mittwoch, 6. Juni 12

  27. The ROAR library Mittwoch, 6. Juni 12

  28. ROAR consists of the following roar-rails roar representers Mittwoch, 6.

    Juni 12
  29. Bi-directional Serialization - Deserialization #to_json #from_json #to_xml #from_xml Mittwoch, 6.

    Juni 12
  30. Client Side Bi-directional Serialization - Deserialization #to_json #from_json #to_xml #from_xml

    Mittwoch, 6. Juni 12
  31. Client Side Server Side Bi-directional Serialization - Deserialization #to_json #from_json

    #to_xml #from_xml Mittwoch, 6. Juni 12
  32. Allows multiple representations Mittwoch, 6. Juni 12

  33. Hypertext Application Language (HAL) Mittwoch, 6. Juni 12

  34. Application as state machine orders orders/1 orders/1/ update Mittwoch, 6.

    Juni 12
  35. class Project < ActiveRecord::Base; end Representing models Mittwoch, 6. Juni

    12
  36. The Representer module ProjectRepresenter include Roar::Representer::JSON property :id property :name

    property :date_published property :author_name end Mittwoch, 6. Juni 12
  37. The Representer module ProjectRepresenter include Roar::Representer::JSON property :id property :name

    property :date_published property :author, class: User, extend: UserRepresenter, embedded: true end Mittwoch, 6. Juni 12
  38. The Representer module ProjectRepresenter include Roar::Representer::JSON property :id property :name

    property :date_published property :author, class: User, extend: UserRepresenter, embedded: true collection :tickets, class: Ticket, extend: TicketRepresenter, embedded: true end Mittwoch, 6. Juni 12
  39. Pick your features • Hypertext Application Language (HAL) • Coersion,

    using Virtus • HTTP Support, using Net::HTTP or Faraday Mittwoch, 6. Juni 12
  40. Roar::Representer::JSON:: HAL module ProjectRepresenter include Roar::Representer::JSON include Roar::Representer::JSON::HAL property :id

    property :title property :date_published property :author collection :tickets, class: Ticket, extend: TicketRepresenter, embedded: true link :self do "http://projects/#{id}" end end Mittwoch, 6. Juni 12
  41. Roar::Representer::Feature:: Coersion module ProjectRepresenter include Roar::Representer::JSON include Roar::Representer::Feature::Coersion property :id,

    type: Numeric property :name, type: String, default: ‘Untitled Project’ property :date_published, type: DateTime, default: ->{ DateTime.now } property :author_name, type: String, default: default_author_name def default_author_name “An unknown author from #{DateTime.now.year}” end end Mittwoch, 6. Juni 12
  42. Roar::Representer::Feature:: HttpVerbs module ProjectRepresenter include Roar::Representer::JSON include Roar::Representer::Feature::HttpVerbs property :id

    end Project.get(‘http://projects/1/’) #=> <Project:> Project.new.extend(ProjectRepresenter).post(‘’) #=> <Project:> Mittwoch, 6. Juni 12
  43. Roar::Representer::Feature:: HttpVerbs require ‘faraday’ Roar::Representer::Feature::HttpVerbs.transport_engine = ::Roar::Representer::Transport::NetHTTP ::Roar::Representer::Transport::Faraday Mittwoch, 6.

    Juni 12
  44. ROAR Rails • gives some nice integration with Responders (https://github.com/

    plataformatec/responders) •respond_with project Mittwoch, 6. Juni 12
  45. How I’ve been using ROAR • An architecture consisting of

    multiple projects. • One project is an API gem • defines Representers on both sides – client and server-side, as an API ‘contract’. Mittwoch, 6. Juni 12
  46. What’s to come… • Better HTTP Support: • Building support

    using Faraday. Mittwoch, 6. Juni 12
  47. More information • https://github.com/apotonick/roar • https://github.com/solnic/virtus • anything by Steve

    Klabnik: http://blog.steveklabnik.com/ Mittwoch, 6. Juni 12
  48. @myabc • @myabc • github.com/myabc • alexbcoles.com Mittwoch, 6. Juni

    12