将动态API响应保存到DB中

时间:2014-05-23 15:38:33

标签: ruby-on-rails ruby api

我的电子商务网站差不多已经完成了我的攻击。在从不同的托运人那里获得费率后,我将它们格式化为选择形式

我正在使用active_shipping gem获取费率

  <select name="shipping option">
        <option>
        </option>
    <% @order_preview.fedex_rates.each do |rate| %>
        <option>
        <%= rate.service_name %>
        <%= number_to_currency(rate.price.to_f / 100) %>
        </option>
    <% end %>
        <option>
        </option>
    <% @order_preview.usps_rates.each do |rate| %>
        <option>
        <%= rate.service_name %>
        <%= number_to_currency(rate.price.to_f / 100) %>
        </option>
    <% end %>
  </select>

这样每次迭代都会创建自己的选项行。对我来说棘手的部分是让选定的选项实际捕获行中的信息。响应包含价格,因此我不能将它们保存到我的数据库中,因为它们是动态的,具体取决于包目的地。

响应哈希看起来像这样

<ActiveMerchant::Shipping::RateEstimate:0x00000101431c58 @service_name="FedEx Standard Overnight", @carrier="FedEx", @destination=4G
Brooklyn, NY, 11249
United States, @origin=Brooklyn, NY, 11249
United States, @service_code="STANDARD_OVERNIGHT", @package_rates=[{:package=>#<ActiveMerchant::Shipping::Package:0x000001049b9b78 @options={:cylinder=>nil, :units=>:imperial}, @dimensions=[#<Quantified::Length: 10.0 inches>, #<Quantified::Length: 10.0 inches>, #<Quantified::Length: 10.0 inches>], @weight_unit_system=:imperial, @dimensions_unit_system=:imperial, @weight=#<Quantified::Mass: 10.0 ounces>, @value=nil, @currency=nil, @cylinder=false, @gift=false, @oversized=false, @unpackaged=false, @inches=[10.0, 10.0, 10.0]>}], @total_price=3378, @negotiated_rate=nil, @currency="USD", @delivery_range=[Tue, 27 May 2014 00:00:00 +0000, Tue, 27 May 2014 00:00:00 +0000], @shipping_date=nil, @delivery_date=Tue, 27 May 2014 00:00:00 +0000, @insurance_price=nil>, #<ActiveMerchant::Shipping::RateEstimate:0x0000010404e2f0 @service_name="FedEx Priority Overnight", @carrier="FedEx", @destination=4G
Brooklyn, NY, 11249
United States, @origin=Brooklyn, NY, 11249
United States, @service_code="PRIORITY_OVERNIGHT", @package_rates=[{:package=>#<ActiveMerchant::Shipping::Package:0x000001049b9b78 @options={:cylinder=>nil, :units=>:imperial}, @dimensions=[#<Quantified::Length: 10.0 inches>, #<Quantified::Length: 10.0 inches>, #<Quantified::Length: 10.0 inches>], @weight_unit_system=:imperial, @dimensions_unit_system=:imperial, @weight=#<Quantified::Mass: 10.0 ounces>, @value=nil, @currency=nil, @cylinder=false, @gift=false, @oversized=false, @unpackaged=false, @inches=[10.0, 10.0, 10.0]>}], @total_price=4418, @negotiated_rate=nil, @currency="USD", @delivery_range=[Tue, 27 May 2014 00:00:00 +0000, Tue, 27 May 2014 00:00:00 +0000], @shipping_date=nil, @delivery_date=Tue, 27 May 2014 00:00:00 +0000, @insurance_price=nil>, #<ActiveMerchant::Shipping::RateEstimate:0x0000010a405e88 @service_name="FedEx Priority Overnight Saturday Delivery", @carrier="FedEx", @destination=4G
Brooklyn, NY, 11249
United States, @origin=Brooklyn, NY, 11249
United States, @service_code="PRIORITY_OVERNIGHT", @package_rates=[{:package=>#<ActiveMerchant::Shipping::Package:0x000001049b9b78 @options={:cylinder=>nil, :units=>:imperial}, @dimensions=[#<Quantified::Length: 10.0 inches>, #<Quantified::Length: 10.0 inches>, #<Quantified::Length: 10.0 inches>], @weight_unit_system=:imperial, @dimensions_unit_system=:imperial, @weight=#<Quantified::Mass: 10.0 ounces>, @value=nil, @currency=nil, @cylinder=false, @gift=false, @oversized=false, @unpackaged=false, @inches=[10.0, 10.0, 10.0]>}], @total_price=6170, @negotiated_rate=nil, @currency="USD", @delivery_range=[Sat, 24 May 2014 00:00:00 +0000, Sat, 24 May 2014 00:00:00 +0000], @shipping_date=nil, @delivery_date=Sat, 24 May 2014 00:00:00 +0000, @insurance_price=nil>

将@service_code标题保存到数据库中,然后将它们与API调用相匹配,只有填充字段(如果它与我的数据库中的字符匹配)(这样我也可以解决我的其他问题)是不错的做法{ {3}}。)

让我知道会有什么建议。这是我的第一个ecom开发工作! 非常感谢!专利

0 个答案:

没有答案