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

Adding product endpoints #10

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
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.
3 changes: 2 additions & 1 deletion lib/ruby-mws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
require 'ruby-mws/api/binary_response'
6 changes: 4 additions & 2 deletions lib/ruby-mws/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -76,4 +78,4 @@ def inspect
end

end
end
end
55 changes: 55 additions & 0 deletions lib/ruby-mws/api/product.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion lib/ruby-mws/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,4 +31,4 @@ def self.server_time
end

end
end
end
64 changes: 64 additions & 0 deletions lib/tasks/mws.rake
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions spec/ruby-mws/api/product_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end