|
1 | 1 | require 'digest/md5' |
2 | | -require 'tempfile' |
3 | 2 | require 'iconv' |
| 3 | +require 'nokogiri' |
4 | 4 |
|
5 | 5 | module MWS |
6 | 6 | module API |
7 | 7 | class Feed < Base |
8 | 8 | ORDER_ACK = '_POST_ORDER_ACKNOWLEDGEMENT_DATA_' |
9 | 9 | SHIP_ACK = '_POST_ORDER_FULFILLMENT_DATA_' |
10 | | - |
11 | | - # TODO: think about whether we should make this more general |
12 | | - # for submission back to the author's fork |
| 10 | + |
| 11 | + # POSTs a request to the submit feed action of the feeds api |
| 12 | + # |
| 13 | + # @param type [String] either MWS::API::Feed::ORDER_ACK or SHIP_ACK |
| 14 | + # @param content_params [Hash{Symbol => String,Hash,Integer}] |
| 15 | + # @return [MWS::API::Response] The response from Amazon |
| 16 | + # @todo Think about whether we should make this more general |
| 17 | + # for submission back to the author's fork |
13 | 18 | def submit_feed(type=nil, content_params={}) |
14 | 19 | name = :submit_feed |
15 | 20 | body = case type |
@@ -38,85 +43,83 @@ def submit_feed(type=nil, content_params={}) |
38 | 43 | end |
39 | 44 |
|
40 | 45 | private |
41 | | - # opts require amazon_order_item_code and amazon_order_id |
| 46 | + # Returns a string containing the order acknowledgement xml |
| 47 | + # |
| 48 | + # @param opts [Hash{Symbol => String}] contains |
| 49 | + # @option opts [String] :amazon_order_item_code ID of the specific item in the order |
| 50 | + # @option opts [String] :amazon_order_id ID of the order on amazon's side |
| 51 | + # @option opts [String] :merchant_order_id Internal order id |
42 | 52 | def content_for_ack_with(opts={}) |
43 | | - body = <<-BODY |
44 | | -<?xml version="1.0"?> |
45 | | -<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd"> |
46 | | -<Header> |
47 | | -<DocumentVersion>1.01</DocumentVersion> |
48 | | -<MerchantIdentifier>#{@connection.seller_id}</MerchantIdentifier> |
49 | | -</Header> |
50 | | -<MessageType>OrderAcknowledgment</MessageType> |
51 | | -<Message> |
52 | | -<MessageID>1</MessageID> |
53 | | -<OrderAcknowledgement> |
54 | | -<AmazonOrderID>#{opts[:amazon_order_id]}</AmazonOrderID> |
55 | | -<StatusCode>Success</StatusCode> |
56 | | -<Item> |
57 | | -<AmazonOrderItemCode>#{opts[:amazon_order_item_code]}</AmazonOrderItemCode> |
58 | | -</Item> |
59 | | -</OrderAcknowledgment> |
60 | | -</Message> |
61 | | -</AmazonEnvelope> |
62 | | -BODY |
| 53 | + Nokogiri::XML::Builder.new do |xml| |
| 54 | + xml.root { |
| 55 | + xml.AmazonEnvelope("xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", |
| 56 | + "xsi:noNamespaceSchemaLocation" => "amzn-envelope.xsd") { # add attrs here |
| 57 | + xml.Header { |
| 58 | + xml.DocumentVersion "1.01" |
| 59 | + xml.MerchantIdentifier @connection.seller_id |
| 60 | + } |
| 61 | + xml.MessageType "OrderAcknowledgement" |
| 62 | + xml.Message { |
| 63 | + xml.MessageID "1" |
| 64 | + xml.OrderAchnowledgement { |
| 65 | + xml.AmazonOrderID opts[:amazon_order_id] |
| 66 | + xml.StatusCode "Success" |
| 67 | + xml.Item { |
| 68 | + xml.AmazonOrderItemCode opts[:amazon_order_item_code] |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + end.to_xml |
63 | 75 | end |
64 | 76 |
|
65 | 77 | # Returns a string containing the shipping achnowledgement xml |
66 | 78 | # |
67 | | - # opts is a hash containing |
68 | | - # :merchant_order_id - the internal order id |
69 | | - # :carrier_code - string representing a shipper code |
70 | | - # possible codes are USPS, UPS, FedEx, DHL, |
71 | | - # Fastway, GLS, GO!, NipponExpress, YamatoTransport |
72 | | - # Hermes Logistik Gruppe, Royal Mail, Parcelforce, |
73 | | - # City Link, TNT, Target, SagawaExpress, |
74 | | - # |
75 | | - # :shipping_method - string describing method (i.e. 2nd Day) |
76 | | - # :items - an array of hashes with :amazon_order_item_code and |
77 | | - # :quantity keys |
78 | | - # :tracking_number - (optional) shipper tracking number |
79 | | - # :fulfillment_date - (optional) DateTime the order was fulfilled |
80 | | - # defaults to the current time |
| 79 | + # @param [Hash{Symbol => String,Array,DateTime}] opts contains: |
| 80 | + # @option opts [String] :merchant_order_id The internal order id |
| 81 | + # @option opts [String] :carrier_code Represents a shipper code |
| 82 | + # possible codes are USPS, UPS, FedEx, DHL, Fastway, GLS, GO!, |
| 83 | + # NipponExpress, YamatoTransport, Hermes Logistik Gruppe, |
| 84 | + # Royal Mail, Parcelforce, City Link, TNT, Target, SagawaExpress |
| 85 | + # @option opts [String] :shipping_method string describing method (i.e. 2nd Day) |
| 86 | + # @option opts [Array<Hash>] :items item specifics including :amazon_order_item_code |
| 87 | + # and :quantity keys |
| 88 | + # @option opts [String] :tracking_number (optional) shipper tracking number |
| 89 | + # @option opts [String] :fulfillment_date (optional) DateTime the order was fulfilled |
| 90 | + # defaults to the current time |
81 | 91 | def content_for_ship_with(opts={}) |
82 | 92 | fulfillment_date = opts[:fulfillment_date] || DateTime.now |
83 | | - tracking_el = opts[:tracking_number] ? "<ShipperTrackingNumber>#{opts[:tracking_number]}</ShipperTrackingNumber>" : "" |
84 | | - items = opts[:items].map do |item_hash| |
85 | | - content_for_item(item_hash) |
86 | | - end.join("\n") |
87 | | - |
88 | | - body = <<-BODY |
89 | | -<?xml version="1.0" encoding="UTF-8"?> |
90 | | -<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amznenvelope.xsd"> |
91 | | -<Header> |
92 | | -<DocumentVersion>1.01</DocumentVersion> |
93 | | -<MerchantIdentifier>#{@connection.seller_id}</MerchantIdentifier> |
94 | | -</Header> |
95 | | -<MessageType>OrderFulfillment</MessageType> |
96 | | -<Message> |
97 | | -<MessageID>1</MessageID> |
98 | | -<OrderFulfillment> |
99 | | -<MerchantOrderID>#{opts[:merchant_order_id]}</MerchantOrderID> |
100 | | -<MerchantFulfillmentID>#{opts[:merchant_order_id]}</MerchantFulfillmentID> |
101 | | -<FulfillmentDate>#{fulfillment_date}</FulfillmentDate> |
102 | | -<FulfillmentData> |
103 | | -<CarrierCode>#{opts[:carrier_code]}</CarrierCode> |
104 | | -<ShippingMethod>#{opts[:shipping_method]}</ShippingMethod> |
105 | | -#{tracking_el} |
106 | | -</FulfillmentData> |
107 | | -#{items} |
108 | | -</OrderFulfillment> |
109 | | -</Message> |
110 | | -</AmazonEnvelope> |
111 | | -BODY |
112 | | - end |
113 | 93 |
|
114 | | - def content_for_item(opts={}) |
115 | | - str = '<Item>' |
116 | | - str += "<AmazonOrderItemCode>#{opts[:amazon_order_item_code]}</AmazonOrderItemCode>" |
117 | | - str += "<Quantity>#{opts[:quantity]}</Quantity>" if opts[:quantity] |
118 | | - |
119 | | - str |
| 94 | + Nokogiri::XML::Builder.new do |xml| |
| 95 | + xml.AmazonEnvelope('xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', |
| 96 | + 'xsi:noNamespaceSchemaLocation' => 'amznenvelope.xsd') { |
| 97 | + xml.Header { |
| 98 | + xml.DocumentVersion "1.01" |
| 99 | + xml.MerchantIdentifier @connection.seller_id |
| 100 | + } |
| 101 | + xml.MessageType "OrderFulfillment" |
| 102 | + xml.Message { |
| 103 | + xml.MessageID "1" |
| 104 | + xml.OrderFulfillment { |
| 105 | + xml.MerghantOrderID opts[:merchant_order_id] |
| 106 | + xml.MerchantFulfillmentID opts[:merchant_order_id] |
| 107 | + xml.FulfillmentDate fulfillment_date |
| 108 | + xml.FulfillmentData { |
| 109 | + xml.CarrierCode opts[:carrier_code] |
| 110 | + xml.ShippingMethod opts[:shipping_method] |
| 111 | + xml.ShipperTrackingNumber opts[:tracking_number] if opts[:tracking_number] |
| 112 | + opts[:items].each do |item_hash| |
| 113 | + xml.Item { |
| 114 | + xml.AmazonOrderItemCode opts[:amazon_order_item_code] |
| 115 | + xml.Quantity opts[:quantity] |
| 116 | + } |
| 117 | + end |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + end.to_xml |
120 | 123 | end |
121 | 124 | end |
122 | 125 | end |
|
0 commit comments