Skip to content

Commit

Permalink
from_json to parse json money data
Browse files Browse the repository at this point in the history
  • Loading branch information
elfassy committed Dec 14, 2017
1 parent 5d98e41 commit 01db5dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/money/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def parse(*args)
parser.parse(*args)
end

def from_json(json)
data = JSON.parse(json)
Money.new(data['value'], data['currency'])
end

def from_cents(cents, currency = nil)
new(cents.round.to_f / 100, currency)
end
Expand Down
4 changes: 4 additions & 0 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@
expect(Money.new(1.50).to_f.to_s).to eq("1.5")
end

it "is creatable from a json string" do
expect(Money.from_json('{"value":"1.0","currency":"CAD"}')).to eq(Money.new(1, 'CAD'))
end

it "is creatable from an integer value in cents" do
expect(Money.from_cents(1950)).to eq(Money.new(19.50))
end
Expand Down

0 comments on commit 01db5dd

Please sign in to comment.