Pro Yearly is on sale from $80 to $50! »

Designing Hypermedia APIs - by Sean Hagstrom

B044a0f039af800f4df09bf3b2465f18?s=47 Las Vegas Ruby Group
November 20, 2013
25

Designing Hypermedia APIs - by Sean Hagstrom

B044a0f039af800f4df09bf3b2465f18?s=128

Las Vegas Ruby Group

November 20, 2013
Tweet

Transcript

  1. Designing Hypermedia APIs and Implementing one in Rails by Sean

    Hagstrom
  2. [ { "id": "1", "author": "This Is My Post!", "title":

    "I'm a Title!", "content": “This be the content..." }, { "id": "2", "author": “Stephen King", "title": “Stand By Me", "content": "..." }, { "id": "3", "author": “Sean Hagstrom", "title": “Designing Hypermedia APIs", "content": “You’re doing it all wrong.." } ]
  3. What are
 Hypermedia APIs

  4. Hypermedia APIs
 are RESTFUL Hypermedia APIs embody the REST architecture.

    Hypermedia APIs are a re- branding approach of the important principles of REST.
  5. Hypermedia APIs
 are Scalable Hypermedia APIs try to eliminate the

    need for versioning. Encapsulation between the client and server is increased. Hypermedia APIs are naturally extendable, and easy to change.
  6. Hypermedia APIs
 are Discoverable Resource Associated Resource Resource Associated Action

    Action
  7. How do they work?

  8. Media Type Specifications Documentation on how to lay out your

    service request object, for example JSON. Documentation on how to interact with service request object through the client app. <maze version=“1.0"> <cell href="/cells/M" rel=“current"> ! <title>Entrance Hallway</title> <link rel="east" href=“/cells/N"/> ! <link rel="west" href=“/cells/L"/> ! </cell> </maze> Example Of: application/maze+xml
  9. Meaningful Descriptions POST /posts HTTP/1.1 Host: www.mikrobrug.com Accepted: application/hal+son HTTP/1.1

    201 CREATED Location: http://www.mikrobrug.com/posts/4 Content-Type: application/hal+json Server Client
  10. { “post”: { "id": "4", "title": "I'm not wrong your

    wrong”, "author": “snjedi", "content": “This be the content…”, "links": [ { “rel": “self", "href": "/posts/4" }, { “rel": “author", "href": "/authors/snjedi" }, { “rel": “posts", "href": "/posts" } ] } }
  11. An implementation?

  12. Gemfile gem ‘roar’ gem ‘roar-rails’

  13. Command Line rails g representer Post

  14. PostRepresenter module PostRepresenter include Roar::Representer::JSON ! property :id property :title

    property :author property :content ! link :self do post_url(self) end ! link :author do author_url(self.author_id) end ! link :posts do posts_url end end
  15. PostsController class PostsController < ApplicationController include Roar::Rails::ControllerAdditions respond_to :json !

    def show post = Post.find_by_id(params[:id]) respond_with post end end
  16. { “post”: { "id": "4", "title": "I'm not wrong your

    wrong”, "author": “snjedi", "content": “This be the content…”, "links": [ { “rel": “self", "href": "/posts/4" }, { “rel": “author", "href": "/authors/snjedi" }, { “rel": “posts", "href": "/posts" } ] } } Results
  17. Any Questions?