Class: ONIX::CodeListExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/onix/code_list_extractor.rb

Overview

A utility class that processes the code list XSD from the ONIX spec and creates a set of TSV files. The generated files are used by this library to make hashes of the code lists available to users.

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ CodeListExtractor

Creates a new extractor. Expects the path to a copy of the code lists file from the spec (called ONIX_BookProduct_CodeLists.xsd on my system).

Raises:

  • (ArgumentError)


14
15
16
17
18
# File 'lib/onix/code_list_extractor.rb', line 14

def initialize(filename)
  raise ArgumentError, "#{filename} not found" unless File.file?(filename)

  @filename = filename
end

Instance Method Details

#run(dir) ⇒ Object

generate a set of TSV files in the given directory. Creates the directory if it doesn’t exist and will overwrite existing files.



23
24
25
26
27
28
29
30
31
32
# File 'lib/onix/code_list_extractor.rb', line 23

def run(dir)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)

  each_list do |number, data|
    #puts number
    file = number.to_s.rjust(3, "0") + ".tsv"
    path = File.join(dir, file)
    File.open(path, "w") { |f| f.write data}
  end
end