diff options
author | Jared Norman <jared@super.gd> | 2020-07-08 19:25:48 -0700 |
---|---|---|
committer | Jared Norman <jared@super.gd> | 2020-07-14 13:07:55 -0700 |
commit | c348c0be29f772b4a0ca32ee3061f74a602e2bd9 (patch) | |
tree | 688c4a54220a52204f9e6cb13571da4d986d5507 /lib/super_good/solidus_taxjar | |
parent | bec2e97f97c5abafaa7704d562cde2e21ddb6fb3 (diff) |
Add convenient class for normalizing addresses
Diffstat (limited to 'lib/super_good/solidus_taxjar')
-rw-r--r-- | lib/super_good/solidus_taxjar/addresses.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/super_good/solidus_taxjar/addresses.rb b/lib/super_good/solidus_taxjar/addresses.rb new file mode 100644 index 0000000..8103269 --- /dev/null +++ b/lib/super_good/solidus_taxjar/addresses.rb @@ -0,0 +1,41 @@ +module SuperGood + module SolidusTaxJar + class Addresses + class << self + def normalize(spree_address) + new.normalize(spree_address) + end + end + + def initialize(api: ::SuperGood::SolidusTaxJar.api) + @api = api + end + + def normalize(spree_address) + taxjar_address = api.validate_spree_address(spree_address).first + + return if taxjar_address.nil? + + Spree::Address.immutable_merge(spree_address, { + country: us, # TaxJar only supports the US currently. + state: state(taxjar_address.state), + zipcode: taxjar_address.zip, + city: taxjar_address.city, + address1: taxjar_address.street + }) + end + + private + + attr_reader :api + + def us + Spree::Country.find_by iso: "US" + end + + def state(abbr) + us.states.find_by_abbr abbr + end + end + end +end |