Skip to content

Instantly share code, notes, and snippets.

@yevgenko
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yevgenko/2e4eed7d05bd69e2fcdf to your computer and use it in GitHub Desktop.
Save yevgenko/2e4eed7d05bd69e2fcdf to your computer and use it in GitHub Desktop.
Parser for Amazon Marketplace Web Service XSD's: Retrieves the list of DataNodes (referenced as Product Category XSDs) and ProductType for the corresponding DataNode
source 'https://rubygems.org'
gem 'mws-rb', git: 'https://github.com/veeqo/mws-rb.git'
##
# # Usage Example
# parser = AmazonMWS::XSDParser.new
# parser.product_data_list # => ['ClothingAccessories', 'Clothing', 'Miscellaneous', ...]
# parser.product_type_list('Clothig') # => ['Shirt', 'Sweater', 'Pants', ...]
##
module AmazonMWS
class XSDParser
attr_reader :xsd_path
def initialize(options = {})
@xsd_path = options.fetch(:xsd_path, default_xsd_path)
end
def product_data_list
# we still need to extract value, because xpath returns attribute
# as a nokogiri object
xml_doc('product').xpath(product_data_query).map(&:value)
end
def product_type_list(category)
filename = filename_for_category(category)
xml_doc(filename).xpath(product_type_query).map do |product_type|
product_type.attributes.values.first.value
end
end
private
def filename_for_category(category)
{
"Clothing" => "ProductClothing",
"SoftwareVideoGames" => "SWVG",
"GiftCard" => "GiftCards"
}.fetch(category, category)
end
def default_xsd_path
MWS::API::Feeds::XSD_PATH
end
def xml_doc(filename)
Nokogiri::XML(File.read(File.join(xsd_path, "#{filename.camelize}.xsd")))
end
def product_data_query
"//xsd:element[@name='ProductData']/xsd:complexType/xsd:choice/xsd:element/@ref"
end
def product_type_complex_type_query
"//xsd:element[@name='ProductType']/xsd:complexType/xsd:choice/xsd:element"
end
def product_type_simple_type_query
"//xsd:element[@name='ProductType']/xsd:simpleType/xsd:restriction/xsd:enumeration"
end
def clothing_type_query
"//xsd:element[@name='ClothingType']/xsd:simpleType/xsd:restriction/xsd:enumeration"
end
def product_type_query
[ product_type_complex_type_query,
product_type_simple_type_query,
clothing_type_query
].join(" | ")
end
end
end
require 'spec_helper'
describe AmazonMWS::XSDParser do
shared_examples 'list of nodes' do
it { should_not be_empty }
it 'returns node names' do
expect(subject.first).to be_kind_of(String)
end
end
describe "#product_data_list" do
subject { described_class.new.product_data_list }
it_behaves_like 'list of nodes'
end
describe "#product_type_list" do
subject { described_class.new.product_type_list('Home') }
it_behaves_like 'list of nodes'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment