<div dir="ltr"><div>On Mon, Apr 14, 2014 at 10:07 AM, Stephen Nelson-Smith <span dir="ltr"><<a href="mailto:sanelson@gmail.com" target="_blank">sanelson@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div dir="ltr">What's the best way to make a struct available as canned data?  I can view the data I want as a struct using pry... or I can dump the API response using VCR, but neither really helps.  The API is actually just XML, and I can't work out how to serialise the struct so I can get it back again as a struct... Marshal doesn't support dumping from a Struct, JSON gives me a string representation, and YAML gives me a Psych::Nodes::Document object... <br>
</div><div><br></div></blockquote><div> </div></div><div>I think this will do what you need, if I understood your question correctly:</div><div><br></div><div><div>#!/usr/bin/env ruby</div><div>require 'json'</div>
<div>require 'json/add/struct'</div><div>require 'awesome_print'</div><div><br></div><div>Serial = Struct.new(:foo, :bar, :baz)</div><div><br></div><div>s = Serial.new('this','that','the other')</div>
<div>puts 'The Struct instance'</div><div>ap s</div><div># {</div><div>#     :foo => "this",</div><div>#     :bar => "that",</div><div>#     :baz => "the other"</div><div># }</div>
<div><br></div><div>serialised = s.as_json</div><div>puts 'Serialised'</div><div>ap serialised</div><div># {</div><div>#     "json_class" => "Serial",</div><div>#              "v" => [</div>
<div>#         [0] "this",</div><div>#         [1] "that",</div><div>#         [2] "the other"</div><div>#     ]</div><div># }</div><div><br></div><div>json_string = serialised.to_json</div><div>
puts 'Stringified'</div><div>ap json_string</div><div># "{\"json_class\":\"Serial\",\"v\":[\"this\",\"that\",\"the other\"]}"</div><div><br></div>
<div>deserialised = Serial.json_create( JSON.parse(json_string) )</div><div>puts 'Deserialised'</div><div>ap deserialised</div><div># {</div><div>#     :foo => "this",</div><div>#     :bar => "that",</div>
<div>#     :baz => "the other"</div><div># }</div></div><div><br></div><div><br></div><div>Warm Regards</div><div class="gmail_extra"><div><br></div>-- <br><div dir="ltr"><div><br></div><div>Damon Davison</div>
<div><div><font color="#000000"><a href="http://allolex.net/" target="_blank">http://allolex.net</a></font></div></div><div>@allolex</div><div><br></div><div>You can hire me:</div><div><a href="http://curatur.com">http://curatur.com</a></div>
<div>@curatur</div></div>
</div></div>