我正在使用Shippinglogic从各个运营商处提取数据。
我想要做的是保存拉到我桌子上的数据。
所以我试图这样做:
ups = Shippinglogic::UPS.new
self.attributes = ups.track(:tracking_number => number)
self.save
以下是ups.track
调用返回的内容:
>> ups.track(:tracking_number => '1ZX2988X0386964132')
=> #<Shippinglogic::UPS::Track::Details:0x1093ba7e0 @origin_country="US", @service_type="GROUND", @destination_state="AL", @origin_state="KY", @signature_name=nil, @destination_city="BIRMINGHAM", @events=[#<Shippinglogic::UPS::Track::Details::Event:0x109383010 @country="US", @name="DELIVERED", @postal_code="35242", @city="BIRMINGHAM", @type="D", @state="AL", @occurred_at=Tue Aug 16 13:44:00 -0500 2011>, #<Shippinglogic::UPS::Track::Details::Event:0x109382d90 @country="US", @name="OUT FOR DELIVERY", @postal_code=nil, @city="BIRMINGHAM", @type="I", @state="AL", @occurred_at=Tue Aug 16 07:46:00 -0500 2011>, #<Shippinglogic::UPS::Track::Details::Event:0x1093b1dc0 @country="US", @name="ARRIVAL SCAN", @postal_code=nil, @city="BIRMINGHAM", @type="I", @state="AL", @occurred_at=Tue Aug 16 07:00:00 -0500 2011>, #<Shippinglogic::UPS::Track::Details::Event:0x109382c50 @country="US", @name="DEPARTURE SCAN", @postal_code=nil, @city="NASHVILLE", @type="I", @state="TN", @occurred_at=Tue Aug 16 03:45:00 -0500 2011>, #<Shippinglogic::UPS::Track::Details::Event:0x109382098 @country="US", @name="ARRIVAL SCAN", @postal_code=nil, @city="NASHVILLE", @type="I", @state="TN", @occurred_at=Tue Aug 16 00:19:00 -0500 2011>, #<Shippinglogic::UPS::Track::Details::Event:0x10937d0c0 @country="US", @name="DEPARTURE SCAN", @postal_code=nil, @city="LEXINGTON", @type="I", @state="KY", @occurred_at=Mon Aug 15 21:36:00 -0500 2011>, #<Shippinglogic::UPS::Track::Details::Event:0x109375e38 @country="US", @name="ORIGIN SCAN", @postal_code=nil, @city="LEXINGTON", @type="I", @state="KY", @occurred_at=Mon Aug 15 16:08:00 -0500 2011>, #<Shippinglogic::UPS::Track::Details::Event:0x109370bb8 @country="US", @name="BILLING INFORMATION RECEIVED", @postal_code=nil, @city=nil, @type="M", @state=nil, @occurred_at=Mon Aug 15 12:18:56 -0500 2011>], @origin_city="CAMPBELLSVILLE", @delivery_at=Tue Aug 16 13:44:00 -0500 2011, @status="DELIVERED", @destination_country="US">
但Shippinglogic返回的对象不会保存到attributes
。
我怎样才能实现这一目标?
答案 0 :(得分:2)
您的问题是track
方法返回一个对象。你不能轻易存储它。因此,您需要使用其方法来存储您真正感兴趣的内容。
EG。存储状态和签名名称
ups = Shippinglogic::UPS.new
track = ups.track(:tracking_number => number) # save the results of the lookup
# since the lookup is expensive
self.status = track.status
self.signature_name = track.signature_name
save # no need to say "self.save"
<强>加强>
根据#track对象的定义方式,将整个对象序列化到数据记录中可能是合理的。然后在需要打印时重新创建#track对象。 Article
另一个问题是,最终,您可能希望将跟踪信息作为字符串呈现给人类。如果是这样,那么只需提前转换为strings / html等,并将结果字符串存储在您的记录中。
答案 1 :(得分:1)
我刚检查了Shippinglogic github自述文件。假设用量不是:
ups = Shippinglogic::UPS.new
tracking = ups.track(:tracking_number => number)
self.attributes = tracking.attributes
save
检查其README的灵活性部分,您将使用tracking.attributes方法。
更新
如果您需要快速获取origin_ *,destination_ *,signature_name,service_type,status,delivery_at和tracking_number,您可以执行以下操作:
ups = Shippinglogic::UPS.new
tracking = ups.track(:tracking_number => number)
self.attributes = [:origin_city, ...].inject({}) do |attrs, attr_name|
attrs[attr_name] = tracking.try(attr_name)
attrs
end
save
但这是一种快速而肮脏的方式。我甚至不确定你是否保存了很多代码(也许)。但它并不像逐个明确地分配它那样可读。