Skip to content
This repository has been archived by the owner on Jun 13, 2018. It is now read-only.

Fix money parsing #458

Open
kmcphillips opened this issue Jan 9, 2017 · 1 comment
Open

Fix money parsing #458

kmcphillips opened this issue Jan 9, 2017 · 1 comment

Comments

@kmcphillips
Copy link
Member

Package.cents_from is a problem.

def self.cents_from(money)
return nil if money.nil?
if money.respond_to?(:cents)
return money.cents
else
case money
when Float
(money * 100).round
when String
money =~ /\./ ? (money.to_f * 100).round : money.to_i
else
money.to_i
end
end
end

It makes a bunch of guesses of how it thinks money is being represented, then parses with switch cases and some weird defaults. Plus assumes anything that responds to cents is money.

We could make each Carrier implement its own money parser, based on the types coming back for a given provider. Or something cleaner. Plus it shouldn't really be in the Package class anyway.

See #457 #455 for examples.

@dustMason
Copy link
Contributor

dustMason commented Jan 13, 2017

I started working on this the other day. It's a big refactor, made a little harder by the fact that the expected money format of each Carrier is not codified anywhere in this library. We will have to check the docs for each API to make sure we get it right.

Pulling in something like RubyMoney would be overkill, but an ActiveShipping::Money class would save us from some money arithmetic pitfalls in the future. Here's what it could look like:

https://gist.github.com/dustMason/433437f0746ba54a07f84f2f8d1dc76a

Then, as mentioned above, each Carrier class would create instances of ActiveShipping::Money as needed, taking care to parse values in API responses as amounts in dollars, cents or whatever.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants