How to calculate shipping costs for the marketplace tutorial?

Hello, I have successfully completed the marketplace tutorial, and now I would like to add a feature where the shipping costs is calculated. Recently, I just discovered the "active_shipping" gem, and I think using it will help simplify the task of calculating the shipping. However, I am a bit perplexed as to where to start when building out this feature. I have already added the "active_shipping" gem to my Gemfile, so I just need some ideas on what are some possible ways to calculate shipping. On a side note, I only plan on shipping via USPS, and any and all help on this subject would greatly be appreciated.

Posted about 3 years ago by Chris
Posted about 3 years ago by Alex Yang

Have you already looked over the documentation? The examples they provide seem pretty straightforward. To check shipping cost via USPS, you should use code like the below:

require 'active_shipping'

usps = ActiveShipping::USPS.new(login: 'developer-key') # You will probably need to register to get a developer-key
response = usps.find_rates(ActiveShipping::Location.new(country: 'US',
state: 'CA',
city: 'Beverly Hills',
zip: '90210'),
ActiveShipping::Location.new(country: 'CA',
province: 'ON',
city: 'Ottawa',
postal_code: 'K1P 1J1'),
[ActiveShipping::Package.new(100, [93,10], cylinder: true)])

usps_rates = response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}

2
Posted about 3 years ago by Chris

@Alex, thanks again for the reply. I think I am going to try and accomplish this using JavaScript.

1
Posted about 3 years ago by Alex Yang

Great - let me know how it goes!

1