class JPShippingRate
This is a singleton class provides interface API calculate EMS shipping rate from Japan. There are several basic functions such as look-up international shipping rate, domestic shipping rate, look-up region of a country, Japan's area of a prefecture.
Usage: shipping_rate = JPShippingRate.instance shipping_rate.international(1500, “US”)
> 8500¶ ↑
or
Note: Just support domestic from Kyoto for now. shipping_rate.domestic(140, “okinawa”)
> 3200¶ ↑
Constants
- VERSION
Public Class Methods
# File lib/jp_shipping_rate.rb, line 29 def initialize @rates = load_shipping_rates @regions = load_regions @jp_areas = load_domestic_areas end
Public Instance Methods
Find domestic area of Japan's states Such as: Hokkaido, Kanto, Okinawa, …
# File lib/jp_shipping_rate.rb, line 74 def area_of_prefecture(state_name = 'kyoto') result = 'okinawa' @jp_areas.each do |area, states| states.each do |state| return result = area.to_s unless state[state_name].nil? end end rescue NoMethodError return 'hokkaido' end
For domestic shipping:
-
default size = 120 for now, parcel size is total of length, width and height
-
state of destination is in lowercase format, e.g. kyoto, tokyo, osaka
-
calculate cost with given size and state
-
plus extra charges for domestic shipping and insurrance charge?
# File lib/jp_shipping_rate.rb, line 52 def domestic(size = 120, to_state) area = area_of_prefecture(to_state) rate = 0 rate += domestic_rate(size, area) if size > 0 rate + domestic_extra_charges end
Calculate an extra charge for domestic shipping with a base extra charge and insurrance condition?
# File lib/jp_shipping_rate.rb, line 95 def domestic_extra_charges(base = 850, insurrance = false) extra = base extra += 1_000 if insurrance extra end
Calculate domestic shipping rate with given params @param size::Integer - size of total of length, width and height @param area::String - Areas of Japan (Okinawa, Hokkaido, Kanto,…)
# File lib/jp_shipping_rate.rb, line 118 def domestic_rate(size = 100, area = 'okinawa') rate = 0 @rates['domestic_parcel'].each do |w, r| next if w.to_i < size r.each do |r_rate| return rate = r_rate[area] unless r_rate[area].nil? end end rate end
For internaltion shipping (EMS):
-
How much weight of the parcel?
-
Identify country region from country code of destination
-
Look up the shipping rate for weight and region
-
plus base extra charges for internaltional shipping and insurrance charge?
# File lib/jp_shipping_rate.rb, line 40 def international(weight, to_country_code) region = region_of_country(to_country_code) rate = 0 rate += international_rate(weight, region) if weight > 0 rate + international_extra_charges end
Calculate an extra charge for internation EMS with a base extra charge and insurrance condition?
# File lib/jp_shipping_rate.rb, line 87 def international_extra_charges(base = 3_500, insurrance = false) extra = base extra += 3_000 if insurrance extra end
Calculate EMS rate with given params @param: weight::Integer - Weight of the parcel. @param: region::String - Regions that is defined by Japan Post service
# File lib/jp_shipping_rate.rb, line 104 def international_rate(weight, region) rate = 0 @rates['international_ems'].each do |w, r| next if w.to_i < weight r.each do |r_rate| return rate = r_rate[region] unless r_rate[region].nil? end end rate end
Find region of the country with given country code from the order info.
# File lib/jp_shipping_rate.rb, line 61 def region_of_country(country_code = 'JP') result = @regions['asia'].to_s @regions.each do |region, countries| countries.each do |country| return result = region.to_s unless country[country_code].nil? end end rescue NoMethodError return 'asia' end
Private Instance Methods
Load Japan areas
# File lib/jp_shipping_rate.rb, line 146 def load_domestic_areas YAML.load_file("#{root_path}/config/japan_areas.yml") end
Load data of region of countries
# File lib/jp_shipping_rate.rb, line 141 def load_regions YAML.load_file("#{root_path}/config/countries.yml") end
Load data of EMS shipping rates
# File lib/jp_shipping_rate.rb, line 136 def load_shipping_rates YAML.load_file("#{root_path}/config/rates.yml") end
# File lib/jp_shipping_rate.rb, line 131 def root_path File.expand_path '../..', __FILE__ end