Class: Aperitiiif::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/aperitiiif/index.rb

Overview

TO DO COMMENT

Constant Summary collapse

VALID_TYPES =
%i[json html].freeze
TEMPLATE_FILE =
"#{__dir__}/template/index.html".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(batch) ⇒ Index

Returns a new instance of Index.



15
16
17
18
19
# File 'lib/aperitiiif/index.rb', line 15

def initialize(batch)
  @batch  = batch
  @config = batch.config
  @items  = batch.items
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/aperitiiif/index.rb', line 13

def config
  @config
end

#itemsObject (readonly)

Returns the value of attribute items.



13
14
15
# File 'lib/aperitiiif/index.rb', line 13

def items
  @items
end

Instance Method Details

#path(type) ⇒ Object



21
22
23
# File 'lib/aperitiiif/index.rb', line 21

def path(type)
  "#{@config.html_build_dir}/index.#{type}"
end

#to_html(items = self.items, config = self.config) ⇒ Object

has smell :reek:DuplicateMethodCall



47
48
49
50
51
52
53
54
55
56
# File 'lib/aperitiiif/index.rb', line 47

def to_html(items = self.items, config = self.config)
  vars = {
    'items' => items.map(&:to_hash),
    'config' => config.hash,
    'iiif_collection_url' => @batch.iiif_collection_url,
    'formatted_date' => Aperitiiif::Utils.formatted_datetime
  }
  template = Liquid::Template.parse File.open(TEMPLATE_FILE).read
  template.render vars
end

#to_json(items = self.items) ⇒ Object

rubocop: enable Metrics/AbcSize



42
43
44
# File 'lib/aperitiiif/index.rb', line 42

def to_json(items = self.items)
  JSON.pretty_generate items.map(&:to_hash)
end

#write(**options) ⇒ Object

rubocop: disable Metrics/AbcSize has smell :reek:TooManyStatements

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aperitiiif/index.rb', line 27

def write(**options)
  raise Aperitiiif::Error, "Index#write is missing required 'type:' option" unless options.key? :type
  raise Aperitiiif::Error, "Index#write 'type: #{options[:type]}' does not match available types #{VALID_TYPES}" unless VALID_TYPES.include? options[:type]

  print "Writing #{options[:type]} index...".colorize(:cyan)

  index = options[:type] == :html ? to_html : to_json
  path  = options&.[](:path) || path(options[:type])

  Aperitiiif::Utils.mkfile_p path, index

  puts "\r#{"Writing #{options[:type]} index:".colorize(:cyan)} #{'Done ✓'.colorize(:green)}    "
end