Class: Riiif::Image

Inherits:
Object
  • Object
show all
Defined in:
app/models/riiif/image.rb

Constant Summary collapse

OUTPUT_FORMATS =
%w(jpg png).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, file = nil) ⇒ Image

Returns a new instance of Image.

Parameters:

  • id (String)

    The identifier of the file to be looked up.

  • file (Riiif::File) (defaults to: nil)

    Optional: The Riiif::File to use instead of looking one up.



26
27
28
29
# File 'app/models/riiif/image.rb', line 26

def initialize(id, file = nil)
  @id = id
  @image = file if file.present?
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



22
23
24
# File 'app/models/riiif/image.rb', line 22

def id
  @id
end

Class Method Details

.cache_key(id, options) ⇒ Object



52
53
54
55
56
# File 'app/models/riiif/image.rb', line 52

def cache_key(id, options)
  str = options.merge(id: id).delete_if { |_, v| v.nil? }.to_s
  # Use a MD5 digest to ensure the keys aren't too long.
  Digest::MD5.hexdigest(str)
end

.expires_inObject



47
48
49
# File 'app/models/riiif/image.rb', line 47

def expires_in
  Riiif::Engine.config.cache_duration_in_days.days
end

Instance Method Details

#imageObject



31
32
33
# File 'app/models/riiif/image.rb', line 31

def image
  @image ||= file_resolver.find(id)
end

#infoObject



42
43
44
# File 'app/models/riiif/image.rb', line 42

def info
  info_service.call(id, image)
end

#render(args) ⇒ Object



35
36
37
38
39
40
# File 'app/models/riiif/image.rb', line 35

def render(args)
  options = decode_options!(args)
  Rails.cache.fetch(Image.cache_key(id, options), compress: true, expires_in: Image.expires_in) do
    image.extract(options)
  end
end