Skip to content

Commit

Permalink
deserialize currency amounts as BigDeciaml
Browse files Browse the repository at this point in the history
  • Loading branch information
AGS4NO committed Nov 2, 2023
1 parent a0a2ae1 commit 8a49e84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/dnsimple/struct/charge.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'bigdecimal'

module Dnsimple
module Struct
class Charge < Base
Expand All @@ -25,7 +27,7 @@ class ChargeItem < Base
# @param [String] amount
# @return [void]
def amount=(amount)
@amount = amount.to_f
@amount = BigDecimal(amount)
end
end

Expand Down Expand Up @@ -67,15 +69,15 @@ def items=(charge_items)
# @param [String] balance_amount
# @return [void]
def balance_amount=(balance_amount)
@balance_amount = balance_amount.to_f
@balance_amount = BigDecimal(balance_amount)
end

# Converts total_amount to a Float and sets it.
#
# @param [String] total_amount
# @return [void]
def total_amount=(total_amount)
@total_amount = total_amount.to_f
@total_amount = BigDecimal(total_amount)
end
end

Expand Down
12 changes: 7 additions & 5 deletions spec/dnsimple/client/billing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'spec_helper'

require 'bigdecimal/util'

describe Dnsimple::Client, ".billing" do

subject { described_class.new(base_url: "https://api.dnsimple.test", access_token: "a1b2c3").billing }
Expand Down Expand Up @@ -40,16 +42,16 @@

response.data.each do |result|
expect(result).to be_a(Dnsimple::Struct::Charge)
expect(result.balance_amount).to be_a(Float)
expect(result.balance_amount).to be_a(BigDecimal)
expect(result.reference).to be_a(String)
expect(result.items).to be_a(Array)
expect(result.items[0]).to be_a(Dnsimple::Struct::Charge::ChargeItem)
end

expect(response.data[0].total_amount).to be_a(Float)
expect(response.data[0].total_amount).to equal(14.5)
expect(response.data[0].items[0].amount).to be_a(Float)
expect(response.data[0].items[0].amount).to equal(14.5)
expect(response.data[0].total_amount).to be_a(BigDecimal)
expect(response.data[0].total_amount.to_s("F")).to eq("14.5")
expect(response.data[0].items[0].amount).to be_a(BigDecimal)
expect(response.data[0].items[0].amount.to_s("F")).to eq("14.5")
end

it "supports filters" do
Expand Down

0 comments on commit 8a49e84

Please sign in to comment.