Class: WaxIiif::ImageInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/wax_iiif/image_info.rb

Overview

Class ImageInfo is a data object for the JSON representation of the image.

It is designed to support the iiif.io/api/image/2.0/#image-information spec.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, variants, tile_width = nil, tile_scale_factors = nil) ⇒ ImageInfo

Returns a new instance of ImageInfo.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wax_iiif/image_info.rb', line 12

def initialize(uri, variants, tile_width = nil, tile_scale_factors = nil)
  raise WaxIiif::Error::InvalidImageData, "No full variant provided:  variants: #{variants}" unless variants['full']
  raise WaxIiif::Error::InvalidImageData, "No thumbnail variant provided:  variants: #{variants}" unless variants['thumbnail']
  raise WaxIiif::Error::InvalidImageData, 'No URI was provided for this image!' if uri.nil?

  @id = uri
  @full = variants['full']
  @variants = variants
  @width = @full.width
  @height = @full.height
  @tile_width = tile_width
  @tile_scale_factors = tile_scale_factors
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



8
9
10
# File 'lib/wax_iiif/image_info.rb', line 8

def height
  @height
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/wax_iiif/image_info.rb', line 6

def id
  @id
end

#tile_scale_factorsObject

Returns the value of attribute tile_scale_factors.



10
11
12
# File 'lib/wax_iiif/image_info.rb', line 10

def tile_scale_factors
  @tile_scale_factors
end

#tile_widthObject

Returns the value of attribute tile_width.



9
10
11
# File 'lib/wax_iiif/image_info.rb', line 9

def tile_width
  @tile_width
end

#widthObject

Returns the value of attribute width.



7
8
9
# File 'lib/wax_iiif/image_info.rb', line 7

def width
  @width
end

Instance Method Details

#contextString

Returns The IIIF context for this image.

Returns:

  • (String)

    The IIIF context for this image



68
69
70
# File 'lib/wax_iiif/image_info.rb', line 68

def context
  WaxIiif::IMAGE_CONTEXT
end

#profileString

Returns The IIIF profile this image supports.

Returns:

  • (String)

    The IIIF profile this image supports



78
79
80
81
82
# File 'lib/wax_iiif/image_info.rb', line 78

def profile
  [WaxIiif::LEVEL_0, {
    supports: %w[cors sizeByWhListed baseUriRedirect]
  }]
end

#protocolString

Returns The IIIF protocol for this image.

Returns:

  • (String)

    The IIIF protocol for this image



73
74
75
# File 'lib/wax_iiif/image_info.rb', line 73

def protocol
  WaxIiif::IMAGE_PROTOCOL
end

#serviceObject



85
86
87
# File 'lib/wax_iiif/image_info.rb', line 85

def service
  nil
end

#sizesHash

Returns a collection of valid sizes based on the available image variants.

Returns:

  • (Hash)

    a collection of valid sizes based on the available image variants



28
29
30
31
32
# File 'lib/wax_iiif/image_info.rb', line 28

def sizes
  @variants.collect do |_name, obj|
    { 'width' => obj.width, 'height' => obj.height }
  end
end

#tilesHash?

The hash of tile information, or nil if the information does not exist.

Returns:

  • (Hash, nil)

    A hash of the tile metadata properly formatted for IIIF JSON.



38
39
40
41
42
43
44
45
# File 'lib/wax_iiif/image_info.rb', line 38

def tiles
  return nil if @tile_scale_factors.nil? || @tile_scale_factors.empty?

  [{
    'width' => @tile_width,
    'scaleFactors' => @tile_scale_factors
  }]
end

#to_json(*_args) ⇒ String

Generate the JSON data for this image in the IIIF-expected format.

Returns:

  • (String)

    the JSON representation of this image



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wax_iiif/image_info.rb', line 51

def to_json(*_args)
  obj = {
    '@context' => context,
    '@id' => id,
    'protocol' => protocol,
    'width' => width,
    'height' => height,
    'sizes' => sizes,
    'profile' => profile
  }
  obj['tiles']   = tiles unless tiles.nil?
  obj['profile'] = profile
  obj['service'] = service unless service.nil?
  JSON.pretty_generate obj
end