Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Map country names to ISO codes
Browse files Browse the repository at this point in the history
Apple doesn't guarantee that the ISO code will be present and
recommended to match country names against common spellings instead.
This will use the country code if it's provided, but otherwise attempt
to map the country name to the corresponding ISO code.
  • Loading branch information
Luuk Veenis committed Nov 18, 2016
1 parent f37a5c7 commit 070b4fd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ window.SolidusPaypalBraintree = {

buildAddress: function(shippingContact) {
var addressHash = {
country_name: shippingContact.country,
country_code: shippingContact.countryCode,
first_name: shippingContact.givenName,
last_name: shippingContact.familyName,
Expand Down
10 changes: 10 additions & 0 deletions app/models/solidus_paypal_braintree/transaction_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SolidusPaypalBraintree
class TransactionAddress
include ActiveModel::Model
include ActiveModel::Validations::Callbacks
include SolidusPaypalBraintree::CountryMapper

attr_accessor :country_code, :last_name, :first_name,
:city, :zip, :state_code, :address_line_1, :address_line_2
Expand All @@ -18,6 +19,15 @@ class TransactionAddress
validates :spree_country, presence: true
validates :spree_state, presence: true, if: :should_match_state_model?

def initialize(attributes = {})
country_name = attributes.delete(:country_name) || ""
if attributes[:country_code].blank?
attributes[:country_code] = iso_from_name(country_name)
end

super(attributes)
end

def spree_country
country_code && (@country ||= Spree::Country.find_by(iso: country_code.upcase))
end
Expand Down
1 change: 1 addition & 0 deletions lib/solidus_paypal_braintree.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'solidus_core'
require 'solidus_paypal_braintree/engine'
require 'solidus_paypal_braintree/country_mapper'

module SolidusPaypalBraintree
def self.table_name_prefix
Expand Down
35 changes: 35 additions & 0 deletions lib/solidus_paypal_braintree/country_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module SolidusPaypalBraintree
module CountryMapper
extend ActiveSupport::Concern

USA_VARIANTS = [
"the united states of america",
"united states of america",
"the united states",
"united states",
"us of a",
"u.s.a.",
"usa",
"u.s.",
"us",
]

CANADA_VARIANTS = [
"canada",
"ca",
]

# Generates a hash mapping each variant of the country name to the same ISO
# ie: { "usa" => "US", "united states" => "US", "canada" => "CA", ... }
COUNTRY_MAP = {
USA_VARIANTS => "US",
CANADA_VARIANTS => "CA"
}.flat_map { |variants, iso| variants.map { |v| [v, iso] } }.to_h

included do
def iso_from_name country_name
COUNTRY_MAP[country_name.downcase.strip]
end
end
end
end
31 changes: 31 additions & 0 deletions spec/models/solidus_paypal_braintree/transaction_address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@
end
end

describe "#attributes=" do
subject { described_class.new(attrs) }

context "when an ISO code is provided" do
let(:attrs) { { country_code: "US" } }

it "uses the ISO code provided" do
expect(subject.country_code).to eq "US"
end
end

context "when the ISO code is blank" do
context "and a valid country name is provided" do
let!(:canada) { create :country, name: "Canada", iso: "CA" }
let(:attrs) { { country_name: "Canada" } }

it "looks up the ISO code by the country name" do
expect(subject.country_code).to eq "CA"
end
end

context "and no valid country name is provided" do
let(:attrs) { { country_name: "Neverland" } }

it "leaves the country code blank" do
expect(subject.country_code).to be_nil
end
end
end
end

describe '#spree_country' do
subject { described_class.new(country_code: country_code).spree_country }

Expand Down

0 comments on commit 070b4fd

Please sign in to comment.