Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Inventory Feed API (via new Inventory#update_inventory_supply method) #4

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

alexdowad
Copy link

(Development of this patch was funded by Sam Hamilton of Mad Jungle. It was done for Sam's web site, but he gave me permission to donate the code back to ruby-mws so others can benefit from it. Thanks, Sam!)

This PR adds support for the Inventory Feeds API, so users of ruby-mws can update the inventory stock levels for their Amazon MWS products.

Using the Feeds API is a bit different from the other APIs. Most of the others just require a single HTTP request, but the Feeds API requires a series of related requests; first to submit a "feed" or XML document, then to check whether Amazon is finished processing the "feed", then to check whether Amazon encountered any errors while processing the "feed".

This means that the code for the new method is rather long compared to the other existing ones. If support for more of the Feeds APIs was desired, a lot of this code could be factored out into a common method. But for now, this is the first one.

The Feeds API requires a "Content-MD5" header be used on the HTTP POST request, so I added some code to MWS::API::Base which can compute and add the needed header. I think it might be useful for access to more of the Amazon MWS API in the future.

@elyngved
Copy link
Owner

Hl @alexdowad, thanks for this PR. I'm finally picking this project back up. Have you been using it with success?

@alexdowad
Copy link
Author

The project for which I wrote this patch was successfully completed a long time ago, and the customer hasn't come back with any complaints. So I assume that ruby-mws is working for them. (Thanks!)

@elyngved
Copy link
Owner

Good to hear. Do you have any specs to contribute as well? Or at least examples of usage? Thanks

@alexdowad
Copy link
Author

Hi! The project which I wrote this patch for is already finished, and I'm
currently busy working on other projects, so I don't have time to write up
specs for this code. I can show you an example of usage (from a Rake task),
though:

require 'ruby-mws'

desc "Upload current stock levels to Amazon MWS"
task :update_amazon_stock => :environment do
  # get a list of all the products which may need to be updated on Amazon
  products = Spree::Product.find(:all, :include => :master)
  skus     = {}
  products.each do |product|
    skus[product.sku] = product.count_on_hand
  end

  # connect to Amazon MWS API
  mws = MWS.new(:aws_access_key_id => 'omitted',
                :secret_access_key => 'omitted',
                :seller_id => 'omitted',
                :marketplace_id => 'omitted',
                :host => 'mws.amazonservices.co.uk')

  # check what stock levels they have in their DB right now
  puts "Retrieving inventory levels from Amazon MWS..."
  response = mws.inventory.list_inventory_supply(:seller_skus => skus.keys)
  stock    = [*response.inventory_supply_list]

  # build up a list of SKUs for products which need updating
  to_update = {}
  stock.each do |item|
    if !skus.key?(item.seller_sku)
      puts "SKU #{item.seller_sku} is up on Amazon MWS, but we don't have it in our Spree database!"
    elsif item.in_stock_supply_quantity.to_i != skus[item.seller_sku]
      puts "Need to update quantity of #{item.seller_sku} from #{item.in_stock_supply_quantity} to #{skus[item.seller_sku]}"
      to_update[item.seller_sku] = skus[item.seller_sku]
    end
  end

  # send an XML document to Amazon updating all outdated inventory data
  puts "Sending XML document to Amazon..."
  if mws.inventory.update_inventory_supply('omitted', to_update)
    puts "Updated successfully!"
  end
end

@phstc
Copy link

phstc commented Oct 22, 2013

No news for this PR?

@SunnyIzr
Copy link

SunnyIzr commented Nov 5, 2013

Hey Guys--yea just wondering if this PR was to be merged. Let me know--this would be SUPER helpful!

@elyngved elyngved closed this Nov 14, 2013
@elyngved elyngved reopened this Nov 14, 2013
@elyngved
Copy link
Owner

Hey all, sorry I've been so busy and disregarding this project. I'm going to take a look at this over the next few days. I haven't had a chance to use the inventory API but I will be soon so I'll have use for this method and I'll be building support for other inventory API endpoints in the near future. Thanks for your patience.

pierrel referenced this pull request in blurb/ruby-mws Mar 13, 2014
Renaming Camelize monkey-patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants