lostisland / sawyer

Secret User Agent of HTTP
MIT License
247 stars 75 forks source link

Failing to restore resource.rels[:x].get after marshal_load #53

Open dazza-codes opened 6 years ago

dazza-codes commented 6 years ago

Finding it difficult to restore functionality after serializing/deserialzing a Sawyer::Resource.

For example, starting with the github Octokit gem

repo = Octokit.repo('kubernetes/kubernetes');
repo.class #=> Sawyer::Resource

users = repo.rels[:contributors].get.data;
users.class #=> Array

dumped = Marshal.dump( repo.marshal_dump );
dumped.class #=> String

agent = Sawyer::Agent.new('https://api.github.com/', links_parser: Sawyer::LinkParsers::Simple.new)
resource = Sawyer::Resource.new(agent)
resource.marshal_load(Marshal.restore(dumped))
resource.class #=> Sawyer::Resource
resource.rels.class #=> Sawyer::Relation::Map
resource.rels[:contributors]
#=> #<Sawyer::Relation: contributors: get #<Addressable::Template:0x000055be59bf7030>>

resource.rels[:contributors].get
NoMethodError: undefined method `get' for nil:NilClass
Did you mean?  gets
               gem
from {HOME}/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/sawyer-0.8.1/lib/sawyer/agent.rb:94:in `call'
technoweenie commented 5 years ago

That specific error is because the Sawyer::Agent has no Faraday::Connection in @conn. I haven't done anything to make sure Ruby can problem marshal and unmarshal Sawyer objects.

Have you considered using a custom Faraday middleware like VCR to cache requests?