Class: WaxIiif::Builder

Inherits:
Object
  • Object
show all
Includes:
Utilities::Helpers
Defined in:
lib/wax_iiif/builder.rb

Overview

Builder class

Constant Summary collapse

HEADER_VAL =
'filename'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities::Helpers

#escape_yaml, #generate_build_location, #generate_id, #generate_image_location, #get_data_path, #save_to_disk

Constructor Details

#initialize(config = {}) ⇒ Void

Initialize the builder.

Parameters:

  • config (Hash) (defaults to: {})

    an optional configuration object.

See Also:



34
35
36
37
# File 'lib/wax_iiif/builder.rb', line 34

def initialize(config = {})
  @manifests = []
  @config = WaxIiif::Config.new(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



26
27
28
# File 'lib/wax_iiif/builder.rb', line 26

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/wax_iiif/builder.rb', line 17

def data
  @data
end

#manifestsArray<Hash>

Returns The manifest hashes for this configuration.

Returns:

  • (Array<Hash>)

    The manifest hashes for this configuration



22
23
24
# File 'lib/wax_iiif/builder.rb', line 22

def manifests
  @manifests
end

Instance Method Details

#create_build_directoriesVoid

Creates the required directories for exporting to the file system.

Returns:

  • (Void)


110
111
112
113
114
115
# File 'lib/wax_iiif/builder.rb', line 110

def create_build_directories
  root_dir = generate_build_location('')
  Dir.mkdir root_dir unless Dir.exist?(root_dir)
  img_dir = generate_image_location('').split('/')[0...-1].join('/')
  Dir.mkdir img_dir unless Dir.exist?(img_dir)
end

#generate_collectionObject



101
102
103
104
105
# File 'lib/wax_iiif/builder.rb', line 101

def generate_collection
  collection = Collection.new(@config)
  manifests.each { |m| collection.add_manifest(m) }
  collection.save
end

#load(data) ⇒ Void

Load data into the IIIF builder.

This will load the data, perform some basic verifications on it, and sort it into proper order.

Parameters:

Returns:

  • (Void)

Raises:



52
53
54
55
56
57
58
59
# File 'lib/wax_iiif/builder.rb', line 52

def load(data)
  @data = [data].flatten # handle hashes and arrays of hashes
  # validate
  @data.each do |image_record|
    raise WaxIiif::Error::InvalidImageData, "Image record #{image_record.inspect} is not an ImageRecord" unless image_record.is_a? ImageRecord
    raise WaxIiif::Error::InvalidImageData, "Image record #{image_record.inspect} does not have an ID" if image_record.id.nil?
  end
end

#load_csv(csv_path) ⇒ Void

TODO:

Fix this to use the correct data format!

Load data into the IIIF server from a CSV

Parameters:

  • csv_path (String)

    Path to the CSV file containing the image data

Returns:

  • (Void)

Raises:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/wax_iiif/builder.rb', line 124

def load_csv(csv_path)
  raise Error::InvalidCSV unless File.exist? csv_path
  begin
    vals = CSV.read(csv_path)
  rescue CSV::MalformedCSVError
    raise Error::InvalidCSV
  end

  raise Error::BlankCSV if vals.length.zero?
  raise Error::InvalidCSV if vals[0].length != 3

  # remove optional header
  vals.shift if vals[0][0] == HEADER_VAL

  @data = vals.collect { |d| { 'image_path' => d[0], 'id' => d[1], 'label' => d[2] } }
end

#process_data(thread_count: Parallel.processor_count) ⇒ Void

Take the loaded data and generate all the files.

Parameters:

  • thread_count (Integer) (defaults to: Parallel.processor_count)

    Thread count to use for processing images concurrently. Defaults to number of processors.

Returns:

  • (Void)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/wax_iiif/builder.rb', line 68

def process_data(thread_count: Parallel.processor_count)
  puts Rainbow("Running on #{thread_count} threads.").blue

  return nil if @data.nil? # do nothing without data.

  @manifests = []

  data = @data.group_by(&:manifest_id)
  bar = ProgressBar.new(data.length)
  bar.write

  data.each do |key, value|
    manifest_id   = key
    image_records = value
    resources     = process_image_records(image_records,
                                          thread_count: thread_count)

    # Generate the manifest
    if manifest_id.to_s.empty?
      resources.each do |_key, val|
        manifests.push generate_manifest(val, @config)
      end
    else
      manifests.push generate_manifest(image_records, @config)
    end

    bar.increment!
    bar.write
  end

  generate_collection
end