Skip to content

Instantly share code, notes, and snippets.

@cobbr2
Created February 7, 2017 18:21
Show Gist options
  • Save cobbr2/4dfa52d2e31c4c0add271f17a6d5b0f2 to your computer and use it in GitHub Desktop.
Save cobbr2/4dfa52d2e31c4c0add271f17a6d5b0f2 to your computer and use it in GitHub Desktop.
Trying to understand how Roar parses hypermedia....
rcobb@rcobb-x250: ~/try_tb$ ruby roar_examples.rb
SENT: {"title":"with links","_links":{"self":{"href":"http://junk.example.com/and/nothing/else"}}}
PARSED: #<Song title="with links">
SELF LINK:
rcobb@rcobb-x250: ~/try_tb$
require 'ostruct'
Song = Class.new(OpenStruct)
require 'roar/decorator'
require 'roar/json/hal'
class SlinkRepresenter < Roar::Decorator
include Roar::JSON::HAL
link :self do |opts|
"http://junk.example.com/and/nothing/else"
end
property :title
end
with_self = Song.new(title: 'with links')
represented = SlinkRepresenter.new(with_self)
represented_json = represented.to_json
slink_rep = SlinkRepresenter.new(parsed_song = Song.new())
puts "SENT: #{represented_json}"
puts "PARSED: #{slink_rep.from_json(represented_json)}"
puts "SELF LINK: #{slink_rep.links[:self]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment