Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified bin/ruby-mws
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions lib/ruby-mws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ def camelize(first_letter_in_uppercase = true)
require 'ruby-mws/api/reports'
require 'ruby-mws/api/response'
require 'ruby-mws/api/sellers'
require 'ruby-mws/api/fulfillment'
12 changes: 9 additions & 3 deletions lib/ruby-mws/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Base
include HTTParty
# debug_output $stderr # only in development
format :xml
headers "User-Agent" => "ruby-mws/#{MWS::VERSION} (Language=Ruby/1.9.3-p0)"
headers "User-Agent" => "ruby-mws/#{MWS::VERSION} (Language=Ruby/#{RUBY_VERSION})"
headers "Content-Type" => "text/xml"

attr_accessor :response
Expand Down Expand Up @@ -44,17 +44,23 @@ def send_request(name, params, options={})
params[:signature_method] ||= 'HmacSHA256'
params[:signature_version] ||= '2'
params[:timestamp] ||= Time.now.iso8601
params[:version] ||= '2009-01-01'
# params[:version] ||= '2009-01-01'
params[:uri] ||= '/'

params[:lists] ||= {}
#params[:lists][:marketplace_id] = "MarketplaceId.Id"
params[:lists][:marketplace_id] = "MarketplaceId.Id"

query = Query.new params
if $VERBOSE
puts "ruby-mws: Sending #{params[:verb].upcase} request to #{query.request_uri}. Options: #{query.http_options.inspect}"
end
resp = self.class.send(params[:verb], query.request_uri, query.http_options)
if $VERBOSE
puts "ruby-mws: response: #{resp}"
puts "response body:"
puts resp.body.inspect
puts "\n"
end

content_type = resp.headers['content-type']
if not content_type =~ /text\/xml/ || content_type =~ /application\/xml/ || content_type =~ /application\/octet-stream/
Expand Down
101 changes: 101 additions & 0 deletions lib/ruby-mws/api/fulfillment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require 'builder'

module MWS
module API

class Fulfillment < Base
include Feeds

# Takes a hash of order and item details
# Returns true if the order was acknowledged successfully
# Otherwise raises an exception
def order_acknowledgement(hash)
# example use: order_acknowledgement(amazon_order_id: '112-2598432-0713054', merchant_order_id: 336, status: 'Success', items: {'47979057082330' => '438'})
# order_acknowledgement(amazon_order_id: '112-2598432-0713054', merchant_order_id: 336, status: 'Success')
# order acknowledgment is done by sending an XML "feed" to Amazon
# as of this writing, XML schema docs are available at:
# https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/XML_Documentation_Intl.pdf
# https://images-na.ssl-images-amazon.com/images/G/01/mwsportal/doc/en_US/bde/MWSFeedsApiReference._V372272627_.pdf
xml = ""
builder = Builder::XmlMarkup.new(:indent => 2, :target => xml)
builder.instruct! # <?xml version="1.0" encoding="UTF-8"?>
builder.AmazonEnvelope(:"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", :"xsi:noNamespaceSchemaLocation" => "amzn-envelope.xsd") do |env|
env.Header do |head|
head.DocumentVersion('1.01')
head.MerchantIdentifier(@connection.seller_id)
end
env.MessageType('OrderAcknowledgement')
env.Message do |mes|
mes.MessageID('1')
mes.OrderAcknowledgement do |oa|
oa.AmazonOrderID(hash[:amazon_order_id])
oa.MerchantOrderID(hash[:merchant_order_id]) unless hash[:merchant_order_id].blank?
oa.StatusCode(hash[:status])
(hash[:items] || {}).each do |item_code, merchant_item_id|
oa.Item do |item|
item.AmazonOrderItemCode(item_code)
item.MerchantOrderItemID(merchant_item_id) unless merchant_item_id.blank?
end
end
end
end
end

submit_feed('_POST_ORDER_ACKNOWLEDGEMENT_DATA_', xml)
end

def shipping_confirmation(hash)
# example use: shipping_confirmation(merchant_order_id: 336, date: Time.parse("2013-06-03 17:58:25"), carrier: 'USPS', shipping_method: 'First Class', tracking: '9400110200883803727403')
# shipping_confirmation(mamazon_order_id: '112-2598432-0713054', date: Time.parse("2013-06-03 17:58:25"), carrier: 'USPS', shipping_method: 'First Class', tracking: '9400110200883803727403')
if hash[:date].is_a? String
# do nothing for now. Maybe later ensure it's the correct format.
else
# assume it's a Time, DateTime, ActiveSupport::TimeWithZone, etc...
hash[:date] = hash[:date].strftime('%Y-%m-%dT%H:%M:%S%:z')
end
xml = ""
builder = Builder::XmlMarkup.new(:indent => 2, :target => xml)
builder.instruct! # <?xml version="1.0" encoding="UTF-8"?>
builder.AmazonEnvelope(:"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", :"xsi:noNamespaceSchemaLocation" => "amzn-envelope.xsd") do |env|
env.Header do |head|
head.DocumentVersion('1.01')
head.MerchantIdentifier(@connection.seller_id)
end
env.MessageType('OrderFulfillment')
env.Message do |mes|
mes.MessageID('1')
mes.OrderFulfillment do |orf|
if hash[:amazon_order_id]
orf.AmazonOrderID(hash[:amazon_order_id])
else
orf.MerchantOrderID(hash[:merchant_order_id])
end
orf.MerchantFulfillmentID(hash[:merchant_fulfillment_id]) if hash[:merchant_fulfillment_id]
orf.FulfillmentDate(hash[:date])
orf.FulfillmentData do |fd|
fd.CarrierCode(hash[:carrier])
fd.ShippingMethod(hash[:shipping_method])
fd.ShipperTrackingNumber(hash[:tracking])
end
(hash[:items] || []).each do |item|
orf.Item do |oi|
if item[:amazon_order_item_code]
oi.AmazonOrderItemCode(item[:amazon_order_item_code])
else
oi.MerchantOrderItemID(item[:merchant_order_item_id])
end
oi.MerchantFulfillmentItemID(item[:merchant_fulfillment_item_id]) if item[:merchant_fulfillment_item_id]
oi.Quantity if item[:quantity]
end
end
end
end
end
submit_feed('_POST_ORDER_FULFILLMENT_DATA_', xml)
end

end

end

end
8 changes: 6 additions & 2 deletions lib/ruby-mws/api/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module MWS
module API

class Order < Base

def_request [:list_orders, :list_orders_by_next_token],
:verb => :get,
:uri => '/Orders/2011-01-01',
Expand All @@ -10,15 +11,18 @@ class Order < Base
:order_status => "OrderStatus.Status"
},
:mods => [
lambda {|r| r.orders = r.orders.order if r.orders}
lambda {|r| r.orders = r.orders.order if r.orders && r.orders.size > 1},
lambda {|r| r.orders = [r.orders.order] if r.orders && r.orders.size == 1}
]

def_request [:list_order_items, :list_order_items_by_next_token],
:verb => :get,
:uri => '/Orders/2011-01-01',
:version => '2011-01-01',
:mods => [
lambda {|r| r.order_items = [r.order_items.order_item].flatten}
# lambda {|r| r.order_items = [r.order_items.order_item].flatten}
lambda {|i| i.order_items = i.order_items.order_item if i.order_items && i.order_items.size > 1},
lambda {|i| i.order_items = [i.order_items.order_item] if i.order_items && i.order_items.size == 1}
]

def_request :get_order,
Expand Down
4 changes: 2 additions & 2 deletions lib/ruby-mws/api/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def canonical
end

def signature
digest = OpenSSL::Digest::Digest.new('sha256')
digest = OpenSSL::Digest.new('sha256')
key = @params[:secret_access_key]
Base64.encode64(OpenSSL::HMAC.digest(digest, key, canonical)).chomp
end
Expand Down Expand Up @@ -89,4 +89,4 @@ def set_body_digest
end

end
end
end
4 changes: 4 additions & 0 deletions lib/ruby-mws/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def sellers
@sellers ||= MWS::API::Sellers.new(@connection)
end

def fulfillment
@fulfillment ||= MWS::API::Fulfillment.new(@connection)
end

# serves as a server ping
def self.server_time
MWS::Connection.server_time
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-mws/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MWS
VERSION = "0.0.3"
VERSION = "0.0.5"
end