Sha256: 3171dff33df7f815b002a64d8f99b254ac272042dfe42ea7843a6a5ac466a2f1

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

module IIIFManifest
  module V3
    class ManifestBuilder
      class AnnotationContentBuilder
        attr_reader :annotation_content, :iiif_annotation_factory, :body_builder_factory
        def initialize(annotation_content, iiif_annotation_factory:, body_builder_factory:)
          @annotation_content = annotation_content
          @iiif_annotation_factory = iiif_annotation_factory
          @body_builder_factory = body_builder_factory
          build_annotation_resource
        end

        def apply(canvas)
          # Assume first item in canvas annotations is an annotation page
          canvas_id = canvas.annotations.first['id']
          generic_annotation['id'] = annotation_id(canvas_id)
          generic_annotation['target'] = target(canvas)
          generic_annotation['motivation'] = motivation
          generic_annotation
        end

        private

        def build_annotation_resource
          annotation_body_builder.apply(generic_annotation)
        end

        def annotation_body_builder
          body_builder_factory.new(annotation_content)
        end

        def generic_annotation
          @generic_annotation ||= iiif_annotation_factory.new
        end

        def annotation_id(canvas_id)
          if annotation_content.try(:annotation_id).blank?
            "#{canvas_id}/#{motivation.presence || 'annotation'}/#{generic_annotation.index}"
          else
            annotation_content.annotation_id
          end
        end

        def motivation
          annotation_content.motivation if annotation_content.try(:motivation).present?
        end

        def target(canvas)
          if annotation_content.try(:media_fragment).present?
            canvas['id'] + "##{annotation_content.media_fragment}"
          else
            canvas['id']
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/iiif_manifest-1.5.0/lib/iiif_manifest/v3/manifest_builder/annotation_content_builder.rb
iiif_manifest-1.6.0 lib/iiif_manifest/v3/manifest_builder/annotation_content_builder.rb
iiif_manifest-1.5.0 lib/iiif_manifest/v3/manifest_builder/annotation_content_builder.rb
iiif_manifest-1.4.0 lib/iiif_manifest/v3/manifest_builder/annotation_content_builder.rb