Skip to content

Commit 3a8b170

Browse files
author
Pierre Larochelle
committed
Merge pull request #7 from plarochelle/proper-xml-builder
Proper xml builder
2 parents 0a84f0b + 4d65c85 commit 3a8b170

File tree

1 file changed

+78
-75
lines changed

1 file changed

+78
-75
lines changed

lib/ruby-mws/api/feed.rb

Lines changed: 78 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
require 'digest/md5'
2-
require 'tempfile'
32
require 'iconv'
3+
require 'nokogiri'
44

55
module MWS
66
module API
77
class Feed < Base
88
ORDER_ACK = '_POST_ORDER_ACKNOWLEDGEMENT_DATA_'
99
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
1318
def submit_feed(type=nil, content_params={})
1419
name = :submit_feed
1520
body = case type
@@ -38,85 +43,83 @@ def submit_feed(type=nil, content_params={})
3843
end
3944

4045
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
4252
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
6375
end
6476

6577
# Returns a string containing the shipping achnowledgement xml
6678
#
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
8191
def content_for_ship_with(opts={})
8292
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
11393

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
120123
end
121124
end
122125
end

0 commit comments

Comments
 (0)