Skip to content

Instantly share code, notes, and snippets.

@kimadactyl
Created October 26, 2016 15:24
Show Gist options
  • Save kimadactyl/231ab6484d18d1c15a9a0bf3cb106a85 to your computer and use it in GitHub Desktop.
Save kimadactyl/231ab6484d18d1c15a9a0bf3cb106a85 to your computer and use it in GitHub Desktop.
Create Royal Mail postal regions for Spree
# A couple of helper functions
# This one converts country strings to Spree::Country objects.
def convert_countries(countries)
log = ""
output = []
countries.each do |country|
spree_country = Spree::Country.find_by(name: country)
if spree_country.nil?
spree_country = Spree::Country.find_by("name like ?", "%#{country}%")
if spree_country.nil?
log += "#{country} not found!\n"
else
log += "#{country} resolved to #{spree_country.name}\n"
end
end
output << spree_country if spree_country
end
puts log
output
end
# This one creates the zone in Spree
def create_zone(countries, zone)
countries.each do |country|
zone.zone_members.create!(zoneable: country)
end
end
# Using this as the basis for regions.
# http://www.royalmail.com/international-zones
# EUROPE
europe = [
"Albania",
"Denmark",
"Kyrgyzstan",
"Russia",
"Andorra",
"Estonia",
"Latvia",
"San Marino",
"Armenia",
"Faroe Islands",
"Liechtenstein",
"Serbia",
"Austria",
"Finland",
"Lithuania",
"Slovakia",
"Azerbaijan",
"France",
"Luxembourg",
"Slovenia",
"Azores",
"Georgia",
"Macedonia",
"Spain",
"Balearic Islands",
"Germany",
"Madeira",
"Sweden",
"Belarus",
"Gibraltar",
"Malta",
"Switzerland",
"Belgium",
"Greece",
"Moldova",
"Tajikistan",
"Bosnia Herzegovina",
"Greenland",
"Monaco",
"Turkey",
"Bulgaria",
"Hungary",
"Montenegro",
"Turkmenistan",
"Canary Islands",
"Iceland",
"Netherlands",
"Ukraine",
"Corsica",
"Ireland", # "Irish Republic" on Royal Mail
"Norway",
"Uzbekistan",
"Croatia",
"Italy",
"Poland",
"Vatican City State",
"Cyprus",
"Kazakhstan",
"Portugal",
"Czech Republic",
"Kosovo",
"Romania"
]
# WORLD ZONE 2
# Note there are 2 Christmas Islands (Indian and Pacific),
# but they have the same postal cost. Phew!
zone_2 = [
"Australia",
"Belau",
"British Indian Ocean Territory",
"Christmas Island",
"Cocos Islands",
"Cook Island",
"Coral Sea Island",
"Fiji",
"French Polynesia",
"French South Antarctic Territory",
"Keeling",
"Kiribati",
"Macao",
"Nauru Island",
"New Caledonia",
"New Zealand",
"New Zealand Antarctic Territory",
"Niue Island",
"Norfolk Island",
"Norwegian Antarctic Territory",
"Papua New Guinea",
"People's Democratic Republic of Laos",
"Pitcairn Island",
"Republic of Singapore",
"Solomon Islands",
"Tahiti",
"Tokelau Islands",
"Tonga",
"Tuvalu",
"US Samoa",
"Western Samoa"
]
# Create shipping zones
royalmail_europe = Spree::Zone.create!(name: "Europe", description: "Royal Mail's 'Europe' shipping region", kind: 'country')
royalmail_zone_2 = Spree::Zone.create!(name: "World Zone 2", description: "Royal Mail's 'World Zone 2' shipping", kind: 'country')
royalmail_zone_1 = Spree::Zone.create!(name: "World Zone 1", description: "Royal Mail's 'World Zone 1' shipping", kind: 'country')
europe_entities = convert_countries(europe)
zone_2_entities = convert_countries(zone_2)
# Europe and Zone 2 defined on Royal Mail website
create_zone(europe_entities, royalmail_europe)
create_zone(zone_2_entities, royalmail_zone_2)
# Zone 1 is defined as... everything else. thanks Royal Mail.
# "Covers all countries, not defined as being in Europe or World Zone 2.
# World Zone 1 generally covers countries in North America, South America,
# Africa, the Middle East, the Far East and South East Asia."
#
# We're just going to get all countries Spree knows about, and delete
# the ones in our other 2 arrays.
all_countries = Spree::Country.all.to_a
europe_entities.each do |entity|
all_countries.delete(entity)
end
zone_2_entities.each do |entity|
all_countries.delete(entity)
end
all_countries.delete(Spree::Country.find_by(name: "United Kingdom"))
create_zone(all_countries, royalmail_zone_1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment