Last active
August 29, 2015 14:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'mws-rb', git: 'https://github.com/veeqo/mws-rb.git' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# # 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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