summaryrefslogtreecommitdiff
path: root/lib/super_good
diff options
context:
space:
mode:
authorJared Norman <jared@super.gd>2020-07-08 19:25:48 -0700
committerJared Norman <jared@super.gd>2020-07-14 13:07:55 -0700
commitc348c0be29f772b4a0ca32ee3061f74a602e2bd9 (patch)
tree688c4a54220a52204f9e6cb13571da4d986d5507 /lib/super_good
parentbec2e97f97c5abafaa7704d562cde2e21ddb6fb3 (diff)
Add convenient class for normalizing addresses
Diffstat (limited to 'lib/super_good')
-rw-r--r--lib/super_good/solidus_taxjar.rb1
-rw-r--r--lib/super_good/solidus_taxjar/addresses.rb41
2 files changed, 42 insertions, 0 deletions
diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb
index f45db80..7bdeb03 100644
--- a/lib/super_good/solidus_taxjar.rb
+++ b/lib/super_good/solidus_taxjar.rb
@@ -9,6 +9,7 @@ require "super_good/solidus_taxjar/calculator_helper"
require "super_good/solidus_taxjar/tax_calculator"
require "super_good/solidus_taxjar/tax_rate_calculator"
require "super_good/solidus_taxjar/discount_calculator"
+require "super_good/solidus_taxjar/addresses"
module SuperGood
module SolidusTaxJar
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