Class: IIIF::V3::Presentation::Canvas

Inherits:
AbstractResource show all
Defined in:
lib/iiif/v3/presentation/canvas.rb

Constant Summary collapse

TYPE =
'Canvas'.freeze

Constants inherited from AbstractResource

AbstractResource::CONTENT_RESOURCE_PROPERTIES, AbstractResource::PAGING_PROPERTIES

Constants included from HashBehaviours

HashBehaviours::SIMPLE_SELF_RETURNERS

Instance Method Summary collapse

Methods inherited from AbstractResource

#any_type_keys, from_ordered_hash, #hash_only_keys, #legal_viewing_direction_values, #numeric_only_keys, parse, #string_only_keys, #to_json, #to_ordered_hash, #uri_only_keys

Methods included from HashBehaviours

#clear, #merge, #merge!, #reject!, #select, #select!

Constructor Details

#initialize(hsh = {}) ⇒ Canvas



28
29
30
31
# File 'lib/iiif/v3/presentation/canvas.rb', line 28

def initialize(hsh={})
  hsh['type'] = TYPE unless hsh.has_key? 'type'
  super(hsh)
end

Instance Method Details

#array_only_keysObject



20
21
22
# File 'lib/iiif/v3/presentation/canvas.rb', line 20

def array_only_keys
  super + %w{ content }
end

#int_only_keysObject



16
17
18
# File 'lib/iiif/v3/presentation/canvas.rb', line 16

def int_only_keys
  super + %w{ depth }
end


24
25
26
# File 'lib/iiif/v3/presentation/canvas.rb', line 24

def legal_viewing_hint_values
  super + %w{ paged continuous non-paged facing-pages auto-advance }
end

#prohibited_keysObject



12
13
14
# File 'lib/iiif/v3/presentation/canvas.rb', line 12

def prohibited_keys
  super + PAGING_PROPERTIES + %w{ viewing_direction format nav_date start_canvas content_annotations }
end

#required_keysObject



8
9
10
# File 'lib/iiif/v3/presentation/canvas.rb', line 8

def required_keys
  super + %w{ id label }
end

#validateObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/iiif/v3/presentation/canvas.rb', line 33

def validate
  super

  id_uri = URI.parse(self['id'])
  unless self['id'] =~ /^https?:/ && id_uri.fragment.nil?
    err_msg = "id must be an http(s) URI without a fragment for #{self.class}"
    raise IIIF::V3::Presentation::IllegalValueError, err_msg
  end

  content = self['content']
  if content && content.any?
    unless content.all? { |entry| entry.instance_of?(IIIF::V3::Presentation::AnnotationPage) }
      err_msg = 'All entries in the content list must be a IIIF::V3::Presentation::AnnotationPage'
      raise IIIF::V3::Presentation::IllegalValueError, err_msg
    end
    content.each do |anno_page|
      annos = anno_page['items']
      if annos && annos.any?
        unless annos.all? { |anno| anno.target == self.id }
          err_msg = 'URI of the canvas must be repeated in the target field of included Annotations'
          raise IIIF::V3::Presentation::IllegalValueError, err_msg
        end
      end
    end
  end

  # "A canvas MUST have exactly one width and one height, or exactly one duration.
  # It may have width, height and duration.""
  height = self['height']
  width = self['width']
  if (!!height ^ !!width) # this is an exclusive or: forces height and width to boolean
    extent_err_msg = "#{self.class} requires both height and width or neither"
    raise IIIF::V3::Presentation::IllegalValueError, extent_err_msg
  end
  # NOTE:  relaxing requirement for (exactly one width and one height, and/or exactly one duration)
  #  as Stanford has objects (such as txt files) for which this makes no sense
  #  (see sul-dlss/purl/issues/169)
  # duration = self['duration']
  # unless (height && width) || duration
  #   extent_err_msg = "#{self.class} must have (a height and a width) and/or a duration"
  #   raise IIIF::V3::Presentation::IllegalValueError, extent_err_msg
  # end

  # TODO: Content must not be associated with space or time outside of the Canvas’s dimensions,
  # such as at coordinates below 0,0, greater than the height or width, before 0 seconds, or after the duration.
end