Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Putting the Hype back in Hypermedia

Putting the Hype back in Hypermedia

Practical REST with Roar

Alex Coles

June 06, 2012
Tweet

More Decks by Alex Coles

Other Decks in Programming

Transcript

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

    17 - 19). • Also helping out with Rails Girls. Mittwoch, 6. Juni 12
  2. The Representer module ProjectRepresenter include Roar::Representer::JSON property :id property :name

    property :date_published property :author_name end Mittwoch, 6. Juni 12
  3. 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
  4. 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
  5. Pick your features • Hypertext Application Language (HAL) • Coersion,

    using Virtus • HTTP Support, using Net::HTTP or Faraday Mittwoch, 6. Juni 12
  6. 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
  7. 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
  8. 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
  9. ROAR Rails • gives some nice integration with Responders (https://github.com/

    plataformatec/responders) •respond_with project Mittwoch, 6. Juni 12
  10. 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