Class: Aperitiiif::Asset

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

Overview

to do has smell :reek:TooManyMethods

Constant Summary collapse

TARGET_EXT =
'.jpg'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_id, source, config) ⇒ Asset

Returns a new instance of Asset.



16
17
18
19
20
21
22
23
# File 'lib/aperitiiif/asset.rb', line 16

def initialize(parent_id, source, config)
  @parent_id  = parent_id
  @source     = source
  @config     = config

  validate_source
  validate_config
end

Instance Attribute Details

#parent_idObject (readonly)

Returns the value of attribute parent_id.



12
13
14
# File 'lib/aperitiiif/asset.rb', line 12

def parent_id
  @parent_id
end

Instance Method Details

#annotationObject



106
107
108
109
110
111
112
113
# File 'lib/aperitiiif/asset.rb', line 106

def annotation
  opts = {
    '@id' => annotation_url,
    'on' => canvas_url,
    'resource' => resource
  }
  IIIF::Presentation::Annotation.new opts
end

#annotation_urlObject



70
71
72
# File 'lib/aperitiiif/asset.rb', line 70

def annotation_url
  @annotation_url ||= "#{@config.presentation_api_url}/annotation/#{id}.json"
end

#build_idObject



82
83
84
85
86
# File 'lib/aperitiiif/asset.rb', line 82

def build_id
  id = Utils.basename_no_ext @source
  id.prepend "#{@parent_id}_" unless id == @parent_id
  id.prepend "#{@config.namespace}_"
end

#build_targetObject



90
# File 'lib/aperitiiif/asset.rb', line 90

def build_target = "#{@config.image_build_dir}/#{id}#{TARGET_EXT}"

#canvasObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/aperitiiif/asset.rb', line 125

def canvas
  opts = {
    '@id' => canvas_url,
    'label' => id,
    'width' => width,
    'height' => height,
    'thumbnail' => thumbnail_url,
    'images' => [annotation]
  }
  IIIF::Presentation::Canvas.new opts
end

#canvas_urlObject



62
63
64
# File 'lib/aperitiiif/asset.rb', line 62

def canvas_url
  @canvas_url ||= "#{@config.presentation_api_url}/canvas/#{id}.json"
end

#full_resource_urlObject



74
75
76
# File 'lib/aperitiiif/asset.rb', line 74

def full_resource_url
  @full_resource_url ||= "#{@config.image_api_url}/#{id}/full/full/0/default.jpg"
end

#heightObject



49
50
51
# File 'lib/aperitiiif/asset.rb', line 49

def height
  @height ||= Vips::Image.new_from_file(@source).height
end

#idObject



37
38
39
# File 'lib/aperitiiif/asset.rb', line 37

def id
  @id ||= build_id
end

#resourceObject



115
116
117
118
119
120
121
122
123
# File 'lib/aperitiiif/asset.rb', line 115

def resource
  opts = {
    '@id' => full_resource_url,
    '@type' => 'dcterms:Image',
    'service' => service,
    'format' => target_mime
  }
  IIIF::Presentation::Resource.new opts
end

#serviceObject



99
100
101
102
103
104
# File 'lib/aperitiiif/asset.rb', line 99

def service
  {
    '@context' => 'http://iiif.io/api/image/2/context.json',
    '@id' => service_url
  }
end

#service_urlObject



78
79
80
# File 'lib/aperitiiif/asset.rb', line 78

def service_url
  @service_url ||= "#{@config.image_api_url}/#{id}"
end

#source_mimeObject



58
59
60
# File 'lib/aperitiiif/asset.rb', line 58

def source_mime
  @source_mime ||= Utils.mime @source
end

#targetObject



41
42
43
# File 'lib/aperitiiif/asset.rb', line 41

def target
  @target ||= build_target
end

#target_mimeObject



53
54
55
56
# File 'lib/aperitiiif/asset.rb', line 53

def target_mime
  write_to_target
  @target_mime ||= Utils.mime target
end

#target_written?Boolean

Returns:

  • (Boolean)


88
# File 'lib/aperitiiif/asset.rb', line 88

def target_written? = File.file? target

#thumbnail_urlObject



66
67
68
# File 'lib/aperitiiif/asset.rb', line 66

def thumbnail_url
  @thumbnail_url ||= "#{@config.image_api_url}/#{id}/full/250,/0/default.jpg"
end

#validate_configObject

Raises:



30
31
32
33
34
35
# File 'lib/aperitiiif/asset.rb', line 30

def validate_config
  raise Aperitiiif::Error, "No value found for 'namespace'. Check your config?" if @config.namespace.empty?
  raise Aperitiiif::Error, "No value found for 'image_build_dir'. Check your config?" if @config.image_build_dir.empty?
  raise Aperitiiif::Error, "No value found for 'presentation_api_url'. Check your config?" if @config.presentation_api_url.empty?
  raise Aperitiiif::Error, "No value found for 'image_api_url'. Check your config?" if @config.image_api_url.empty?
end

#validate_sourceObject

Raises:



25
26
27
28
# File 'lib/aperitiiif/asset.rb', line 25

def validate_source
  raise Aperitiiif::Error, "Asset source #{@source} does not exist" unless File.file? @source
  raise Aperitiiif::Error, "Asset source #{@source} is not a valid format" unless Utils.valid_source? @source
end

#widthObject



45
46
47
# File 'lib/aperitiiif/asset.rb', line 45

def width
  @width ||= Vips::Image.new_from_file(@source).width
end

#write_to_targetObject



92
93
94
95
96
97
# File 'lib/aperitiiif/asset.rb', line 92

def write_to_target
  return false if target_written?

  FileUtils.mkdir_p @config.image_build_dir
  Vips::Image.new_from_file(@source).write_to_file target
end