EasyPost/easypost-ruby

Serializing EasyPost objects fails

Closed this issue · 5 comments

Given a snippet:

shipment = EasyPost::Shipment.create(
  :from_address => source,
  :to_address => destination,
  :parcel => parcel
)

shipment.rates
# => [#<EasyPost::Rate: id=rat.... ]
YAML.load( YAML.dump( shipment )).rates
# => NoMethodError: undefined method `rates' for #<EasyPost::Shipment:0x00000101f55c88>

This seems to be happening because there's some trickery in the initializer that defines instance methods. Could the same goals be achieved using method_missing? The rationale for this would be that it's then straight forward to save EasyPost objects to the database using serialize method for ActiveRecord or any other ORM. This helps with reducing round trips to EasyPost servers which are very important for the user facing applications.

Could you provide the output for source.to_json, destination.to_json, parcel.to_json, and shipment.to_json? I am trying to recreate this locally and it appears to be working on my end, so I need a little help reproducing.

Thanks!
Jon

Sorry about the delay. Here's an example that breaks. Tested on ruby-1.9.3-p286

require "easypost"
require "yaml"

EasyPost.api_key = "..."
shipment = EasyPost::Shipment.create(
  :from_address => EasyPost::Address.create(
    :street_1 => "Pickle rd 2b",
    :zip => "10001",
    :state => "NY"),
  :to_address => EasyPost::Address.create(
    :street_1 => "54 Tofu place",
    :zip => "90201",
    :state => "CA"),
  :parcel => EasyPost::Parcel.create(
    :width => 5,
    :height => 3,
    :length => 10,
    :weight => 8)
  )

shipment2 = YAML.load( YAML.dump( shipment ) )
shipment2.rates   # easypost_test.rb:22:in `<main>': undefined method `rates' for #<EasyPost::Shipment:0x00000100a48e30> (NoMethodError)

Please, let me know, if you need more details like gem versions or if you want me to test something.

Hi Guntars,

Do you receive an error or rates if you run the code you sent up to but not
including the shipment2 lines? I'm running the top portion, and inspecting
shipment after completion and it looks good.

Thanks!
Sawyer

On Wed, Jul 24, 2013 at 1:59 AM, Guntars Ašmanis
notifications@github.comwrote:

Sorry about the delay. Here's an example that breaks. Tested on
ruby-1.9.3-p286

require "easypost"require "yaml"
EasyPost.api_key = "..."shipment = EasyPost::Shipment.create(
:from_address => EasyPost::Address.create(
:street_1 => "Pickle rd 2b",
:zip => "10001",
:state => "NY"),
:to_address => EasyPost::Address.create(
:street_1 => "54 Tofu place",
:zip => "90201",
:state => "CA"),
:parcel => EasyPost::Parcel.create(
:width => 5,
:height => 3,
:length => 10,
:weight => 8)
)
shipment2 = YAML.load( YAML.dump( shipment ) )shipment2.rates # easypost_test.rb:22:in <main>': undefined methodrates' for #EasyPost::Shipment:0x00000100a48e30 (NoMethodError)

Please, let me know, if you need more details like gem versions or if you
want me to test something.


Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-21469256
.

Do you receive an error or rates if you run the code you sent up to but not including the shipment2 lines?

Yes, it works fine. I get rates from shipment.rates

Related to this because I think it has the same root cause, it's impossible to serialize an EasyPost object with Marshal.

For example:

require "easypost"

address = EasyPost::Address.create(
    :street_1 => "Pickle rd 2b",
    :zip => "10001",
    :state => "NY")

Marshal.dump(address)    # TypeError: singleton can't be dumped