Sha256: c6a600c8c0817ed1e319624aa213fd52be23b8748e04644235408aa3e330234e

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

# Disabling some rubocops. This is a script maintained otherplace, and we don't
# care about the style of this file.

# rubocop:disable Security/Open
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize

require 'nokogiri'

namespace :amazon do
  desc 'Download XSD files recursively'
  task xsd: :environment do
    dest = download_xsd_file(
      'amzn-envelope.xsd',
      'https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_1_9/'
    )
    # Validate schema
    Nokogiri::XML::Schema(File.open(dest))
    puts 'Done!'
  end

  def download_xsd_file(file_name, base_url, downloads: [])
    return if downloads.include?(file_name)

    puts "- downloading #{file_name}..."
    downloads << file_name
    destination = File.expand_path(file_name, './lib/xsd')
    FileUtils.mkdir_p File.dirname destination

    File.open(destination, 'w') do |f|
      IO.copy_stream(open(base_url + file_name), f)
    end

    raw_xml = Nokogiri::XML(File.open(destination))
    raw_xml.xpath('//xsd:include').each do |tag|
      file_name = tag.attribute('schemaLocation').value
      download_xsd_file(file_name, base_url, downloads: downloads)
    end

    destination
  end
end

# rubocop:enable Security/Open
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amazon_feed_validator-0.1.0 lib/tasks/amazon.rake