Created
February 7, 2017 18:21
-
-
Save cobbr2/4dfa52d2e31c4c0add271f17a6d5b0f2 to your computer and use it in GitHub Desktop.
Trying to understand how Roar parses hypermedia....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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