diff --git a/bin/ruby-mws b/bin/ruby-mws old mode 100644 new mode 100755 diff --git a/lib/ruby-mws.rb b/lib/ruby-mws.rb index 1694f99..a34eef7 100644 --- a/lib/ruby-mws.rb +++ b/lib/ruby-mws.rb @@ -56,7 +56,8 @@ def camelize(first_letter_in_uppercase = true) require 'ruby-mws/api/base' require 'ruby-mws/api/inventory' require 'ruby-mws/api/order' +require 'ruby-mws/api/product' require 'ruby-mws/api/report' require 'ruby-mws/api/query' require 'ruby-mws/api/response' -require 'ruby-mws/api/binary_response' \ No newline at end of file +require 'ruby-mws/api/binary_response' diff --git a/lib/ruby-mws/api/base.rb b/lib/ruby-mws/api/base.rb index a23bcae..e7777aa 100644 --- a/lib/ruby-mws/api/base.rb +++ b/lib/ruby-mws/api/base.rb @@ -37,7 +37,9 @@ def send_request(name, params, options={}) params = [default_params(name), params, options, @connection.to_hash].inject :merge params[:lists] ||= {} - params[:lists][:marketplace_id] = "MarketplaceId.Id" + if not params[:uri].start_with? '/Products' + params[:lists][:marketplace_id] = "MarketplaceId.Id" + end query = Query.new params resp = self.class.send(params[:verb], query.request_uri) @@ -76,4 +78,4 @@ def inspect end end -end \ No newline at end of file +end diff --git a/lib/ruby-mws/api/product.rb b/lib/ruby-mws/api/product.rb new file mode 100644 index 0000000..aeab1ef --- /dev/null +++ b/lib/ruby-mws/api/product.rb @@ -0,0 +1,55 @@ +module MWS + module API + class Product < Base + def_request [:get_matching_product], + verb: :get, + uri: '/Products/2011-10-01', + version: '2011-10-01', + mods: [ + lambda do |response| + response.map! do |p| + n = p.product.try(:attribute_sets).try(:item_attributes) + n.asin = p.asin if n + n + end + end + ] + + def_request [:get_competitive_pricing_for_ASIN], + verb: :get, + uri: '/Products/2011-10-01', + version: '2011-10-01', + mods: [ + lambda do |response| + response.map! do |p| + n = p.product.try(:competitive_pricing) + if n + n.sales_rankings = p.product.sales_rankings + n.asin = p.asin + end + n + end + end + ] + + def_request [:get_lowest_offer_listings_for_ASIN], + verb: :get, + uri: '/Products/2011-10-01', + version: '2011-10-01', + mods: [ + lambda do |response| + response.map! do |p| + n = p.product.lowest_offer_listings + n.asin = p.asin + n + end + end + ] + + def_request [:get_product_categories_for_ASIN], + verb: :get, + uri: '/Products/2011-10-01', + version: '2011-10-01' + end + end +end diff --git a/lib/ruby-mws/base.rb b/lib/ruby-mws/base.rb index f8c72e5..b5fb06d 100644 --- a/lib/ruby-mws/base.rb +++ b/lib/ruby-mws/base.rb @@ -16,6 +16,10 @@ def inventory @inventory ||= MWS::API::Inventory.new(@connection) end + def products + @products ||= MWS::API::Product.new(@connection) + end + def reports @reports ||= MWS::API::Report.new(@connection) end @@ -27,4 +31,4 @@ def self.server_time end end -end \ No newline at end of file +end diff --git a/lib/tasks/mws.rake b/lib/tasks/mws.rake new file mode 100644 index 0000000..d156997 --- /dev/null +++ b/lib/tasks/mws.rake @@ -0,0 +1,64 @@ +require 'pp' + +desc 'Access MWS services' + +namespace :mws do + desc "Print server time" + task time: :environment do + puts MWS::Base.server_time + end + + desc "List orders" + task orders: :environment do + response = mws.orders.list_orders last_updated_after: 4.hours.ago + + pp response.orders + end + + # rake mws:product[B002DYJTVW,B0018KVHJO] + desc "List product" + task :product, [*1..10].map!{ |i| ("ASINList.ASIN." + i.to_s).to_sym } => :environment do |t, args| + args.with_defaults(:'ASINList.ASIN.1' => 'B002DYJTVW', :'ASINList.ASIN.2' => 'B0018KVHJO') if not args[:'ASINList.ASIN.1'] + + # should be response.products + products = mws.products.get_matching_product args + + pp products + end + + desc "Competitive pricing" + task :cprice, [*1..10].map!{ |i| ("ASINList.ASIN." + i.to_s).to_sym } => :environment do |t, args| + args.with_defaults(:'ASINList.ASIN.1' => 'B002DYJTVW', :'ASINList.ASIN.2' => 'B0018KVHJO') if not args[:'ASINList.ASIN.1'] + + response = mws.products.get_competitive_pricing_for_ASIN args + + pp response + end + + desc "Lowest price listing" + task :lprice, [*1..10].map!{ |i| ("ASINList.ASIN." + i.to_s).to_sym } => :environment do |t, args| + args.with_defaults(:'ASINList.ASIN.1' => 'B002DYJTVW', :'ASINList.ASIN.2' => 'B0018KVHJO') if not args[:'ASINList.ASIN.1'] + + response = mws.products.get_lowest_offer_listings_for_ASIN args + + pp response + end + + desc "Categories" + task :categories, [:asin] => :environment do |t, args| + args.with_defaults(asin: 'B002DYJTVW') + + response = mws.products.get_product_categories_for_ASIN ASIN: args.asin + + pp response + end + + def mws + @mws ||= MWS.new(aws_access_key_id: ENV['AWS_ID'], + secret_access_key: ENV['AWS_KEY'], + seller_id: ENV['MERCHANT_ID'], + marketplace_id: ENV['MARKETPLACE_ID']) + end + + +end diff --git a/spec/ruby-mws/api/product_spec.rb b/spec/ruby-mws/api/product_spec.rb new file mode 100644 index 0000000..1022d21 --- /dev/null +++ b/spec/ruby-mws/api/product_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +describe MWS::API::Order do + + before :all do + EphemeralResponse.activate + @mws = MWS.new(auth_params) + end + + context 'requests' do + describe 'get_matching_product' do + it 'should receive a list of products' do + products = @mws.products.get_matching_product :'ASINList.ASIN.1' => 'B002DYJTVW', :'ASINList.ASIN.2' => 'B0018KVHJO' + + products.should be_an_instance_of Array + products.first.should have_key :asin + products.first.should have_key :title + end + end + + describe 'get_competitive_pricing_for_ASIN' do + it 'should recieve competitive pricing for an asin' do + products = @mws.products.get_competitive_pricing_for_ASIN :'ASINList.ASIN.1' => 'B002DYJTVW', :'ASINList.ASIN.2' => 'B0018KVHJO' + + products.should be_an_instance_of Array + products.first.should have_key :asin + products.first.should have_key :prices + end + end + + describe 'get_lowest_offer_listings_for_ASIN' do + it 'should recieve competitive pricing for an asin' do + products = @mws.products.get_lowest_offer_listings_for_ASIN :'ASINList.ASIN.1' => 'B002DYJTVW', :'ASINList.ASIN.2' => 'B0018KVHJO' + + products.should be_an_instance_of Array + products.first.should have_key :asin + products.first.should have_key :lowest_offer_listing + end + end + + describe 'get_lowest_offer_listings_for_ASIN' do + it 'should recieve competitive pricing for an asin' do + category = @mws.products.get_product_categories_for_ASIN ASIN: 'B002DYJTVW' + + category.self.should have_key :product_category_id + category.self.should have_key :product_category_name + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7738559..6a144d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -31,9 +31,9 @@ def auth_params def timestamp # use a recent timestamp here... how to replace this? - "2013-11-17T21:17:59-06:00" + "2014-02-12T20:56:59-05:00" end end class TestWorksError < StandardError -end \ No newline at end of file +end