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

Hypermedia APIs with Webmachine and ROAR

Hypermedia APIs with Webmachine and ROAR

Building hypermedia APIs with Webmachine and ROAR

Larry Staton Jr.

November 15, 2012
Tweet

More Decks by Larry Staton Jr.

Other Decks in Technology

Transcript

  1. Hypermedia APIs Hypermedia APIs Webmachine & ROAR 1 Larry Staton

    Jr. : @statonjr 1 Wednesday, 14 November 12
  2. Hypermedia APIs 4 Constraint Promotes Tradeoff client-server UI Portability Simplified

    server stateless Scalability Reliability Efficiency non-shared caching Reduced latency Efficiency Scalability Reliability uniform interface Independent Evolution Decoupled implementation Efficiency layered system Scalability Simplified clients Higher Latency code-on-demand Extensibility Simplified clients Visibility 4 Wednesday, 14 November 12
  3. Hypermedia APIs 7 Constraint Promotes Tradeoff client-server UI Portability Simplified

    server stateless Scalability Reliability Efficiency non-shared caching Reduced latency Efficiency Scalability Reliability uniform interface Independent Evolution Decoupled implementation Efficiency layered system Scalability Simplified clients Higher Latency code-on-demand Extensibility Simplified clients Visibility 7 Wednesday, 14 November 12
  4. Hypermedia APIs 17 class HomeResource < Webmachine::Resource def allowed_methods %W[GET]

    end def expires Time.now + 2.weeks end def languages_provided [“en-us”] end end 17 Wednesday, 14 November 12
  5. Hypermedia APIs 18 allow_missing_post? allowed_methods base_uri charsets_provided content_types_accepted content_types_provided create_path

    delete_completed? delete_resource encodings_provided expires finish_request forbidden generate_etag is_authorized? is_conflict? known_content_type? known_methods language_chosen languages_provided last_modified malformed_request? moved_permanently? moved_temporarily? multiple_choices? options post_is_create? previously_existed? process_post resource_exists? service_available? uri_too_long? valid_content_headers? valid_entity_length? validate_content_checksum variances Callbacks 18 Wednesday, 14 November 12
  6. Hypermedia APIs 19 Webmachine::Application.new do |app| app.routes do add [],

    HomeResource end end 19 Wednesday, 14 November 12
  7. Hypermedia APIs 22 <!DOCTYPE html> <html lang=”en”> <body> <a href=”...”

    rel=”../vehicles”>Vehicles</a> </body> </html> Example HTML Representation 22 Wednesday, 14 November 12
  8. Hypermedia APIs 23 class HomeResource < Webmachine::Resource … def content_types_provided

    [[‘text/html’, :to_html]] end def to_html template = Slim::Template.new(“home_representer.slim”) template.render end end 23 Wednesday, 14 November 12
  9. Hypermedia APIs 25 { “_links”: { “self”: { “href”: “.../api”

    }, “vehicles”: { “href”: “.../vehicles” } } } Example HAL+JSON Representation 25 Wednesday, 14 November 12
  10. Hypermedia APIs 27 module HomeRepresenter include Roar::Representer::JSON include Roar::Representer::JSON::HAL include

    Roar::Representer::Feature::Hypermedia link :self do "/api" end link :vehicles do ".../vehicles" end end 27 Wednesday, 14 November 12
  11. Hypermedia APIs 28 class HomeResource < Webmachine::Resource … def content_types_provided

    [[‘application/hal+json’, :to_hal]] end def to_hal home = Object.new.extend(HomeRepresenter) home.to_json end end 28 Wednesday, 14 November 12
  12. Hypermedia APIs Additional Resources 32 seancribbs/webmachine-ruby apotonick/roar Jon Moore’s 2010

    Oredev talk Steve Klabnik: “Designing Hypermedia APIs” Mike Amundsen: “Building Hypermedia APIs” Webber/Robinson: “REST in Practice” RESTFest 32 Wednesday, 14 November 12