这个库与各种运输承运商的 Web 服务对接。目标是将最常用的功能抽象成一个令人愉悦且一致的 Ruby API。
Active Shipping 目前正在 Shopify 的生产环境中被使用和改进。开发工作由 Shopify 集成团队完成(integrations-team@shopify.com)。欢迎在 Active Merchant Google Group 中进行讨论。
gem install active_shipping
...或者将其添加到你的项目的 Gemfile 中。
require 'active_shipping'
# Package up a poster and a Wii for your nephew.
packages = [
ActiveShipping::Package.new( 100, # 100 grams
[93,10], # 93 cm long, 10 cm diameter
:cylinder => true), # cylinders have different volume calculations
ActiveShipping::Package.new( 7.5 * 16, # 7.5 lbs, times 16 oz/lb.
[15, 10, 4.5], # 15x10x4.5 inches
:units => :imperial) # not grams, not centimetres
]
# You live in Beverly Hills, he lives in Ottawa
origin = ActiveShipping::Location.new( :country => 'US',
:state => 'CA',
:city => 'Beverly Hills',
:zip => '90210')
destination = ActiveShipping::Location.new( :country => 'CA',
:province => 'ON',
:city => 'Ottawa',
:postal_code => 'K1P 1J1')
# Find out how much it'll be.
ups = ActiveShipping::UPS.new(:login => 'auntjudy', :password => 'secret', :key => 'xml-access-key')
response = ups.find_rates(origin, destination, packages)
ups_rates = response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
# => [["UPS Standard", 3936],
# ["UPS Worldwide Expedited", 8682],
# ["UPS Saver", 9348],
# ["UPS Express", 9702],
# ["UPS Worldwide Express Plus", 14502]]
# Check out USPS for comparison...
usps = ActiveShipping::USPS.new(:login => 'developer-key')
response = usps.find_rates(origin, destination, packages)
usps_rates = response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
# => [["USPS Priority Mail International", 4110],
# ["USPS Express Mail International (EMS)", 5750],
# ["USPS Global Express Guaranteed Non-Document Non-Rectangular", 9400],
# ["USPS GXG Envelopes", 9400],
# ["USPS Global Express Guaranteed Non-Document Rectangular", 9400],
# ["USPS Global Express Guaranteed", 9400]]
fedex = ActiveShipping::FedEx.new(login: '999999999', password: '7777777', key: '1BXXXXXXXXXxrcB', account: '51XXXXX20')
tracking_info = fedex.find_tracking_info('tracking-number', :carrier_code => 'fedex_ground') # Ground package
tracking_info.shipment_events.each do |event|
puts "#{event.name} at #{event.location.city}, #{event.location.state} on #{event.time}. #{event.message}"
end
# => Package information transmitted to FedEx at NASHVILLE LOCAL, TN on Thu Oct 23 00:00:00 UTC 2008.
# Picked up by FedEx at NASHVILLE LOCAL, TN on Thu Oct 23 17:30:00 UTC 2008.
# Scanned at FedEx sort facility at NASHVILLE, TN on Thu Oct 23 18:50:00 UTC 2008.
# Departed FedEx sort facility at NASHVILLE, TN on Thu Oct 23 22:33:00 UTC 2008.
# Arrived at FedEx sort facility at KNOXVILLE, TN on Fri Oct 24 02:45:00 UTC 2008.
# Scanned at FedEx sort facility at KNOXVILLE, TN on Fri Oct 24 05:56:00 UTC 2008.
# Delivered at Knoxville, TN on Fri Oct 24 16:45:00 UTC 2008. Signed for by: T.BAKER
在使用 bundle install
安装依赖项后,你可以使用 rake test:units
运行单元测试,使用 rake test:remote
运行远程测试。单元测试模拟请求和响应,以便一切都在本地运行,而远程测试实际上会访问承运商的服务器。对于远程测试,你需要为要运行的任何承运商测试提供有效的测试凭据。凭据应放在 ~/.active_shipping/credentials.yml 中,该文件的格式可以在包含的 credentials.yml 中看到。对于某些承运商,我们有可用于测试的公共凭据:请参阅 .travis.yml
。
是的,请!查看测试和 Carrier 类的实现,了解基本原理。在不久的将来,会有一个类似于 Active Merchant 中包含的网关生成器的承运商模板生成器,但 carrier.rb 概述了大部分必要的内容。其他需要熟悉的主要类是 Location、Package 和 Response。
对于你添加的功能,你应该同时进行单元测试和远程测试。最好从远程测试开始,然后记录这些请求和响应,并将它们用作单元测试的模拟。你可以看到 USPS 测试现在是如何工作的
https://github.com/Shopify/active_shipping/blob/master/test/remote/usps_test.rb https://github.com/Shopify/active_shipping/blob/master/test/unit/carriers/usps_test.rb https://github.com/Shopify/active_shipping/tree/master/test/fixtures/xml/usps
要记录请求和响应,只需将你的承运商类上的 logger
设置为某种 Logger
对象
ActiveShipping::USPS.logger = Logger.new($stdout)
(此日志记录功能由 active_utils
依赖项中的 PostsData
模块提供。)
在你将经过良好测试的更改推送到你的 github fork 后,创建一个 pull request,我们会从那里开始处理!有关更多信息,请参阅 CONTRIBUTING.md。
除非在特定文件中另有说明,否则 ActiveShipping 项目中的所有代码均受随附的 MIT-LICENSE 文件中描述的版权和许可约束。