Class: Riiif::Image

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, passed_file = nil) ⇒ Image

Returns a new instance of Image.

Parameters:

  • id (String)

    The identifier of the file to be looked up.

  • file (Riiif::File)

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



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

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



39
40
41
# File 'app/models/riiif/image.rb', line 39

def id
  @id
end

Class Method Details

.cache_key(id, options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'app/models/riiif/image.rb', line 78

def cache_key(id, options)
  str = options.to_h.merge(id: id)
               .delete_if { |_, v| v.nil? }
               .sort_by { |k, _v| k.to_s }
               .to_s

  # Use a MD5 digest to ensure the keys aren't too long, and a prefix
  # to avoid collisions with other components in shared cache.
  'riiif:' + Digest::MD5.hexdigest(str)
end

.expires_inObject



74
75
76
# File 'app/models/riiif/image.rb', line 74

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

Instance Method Details

#fileObject Also known as: image



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

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

#infoObject



66
67
68
69
70
71
# File 'app/models/riiif/image.rb', line 66

def info
  @info ||= begin
              result = info_service.call(id, file)
              ImageInformation.new(result[:width], result[:height])
            end
end

#render(args) ⇒ Object

Parameters:

  • args (ActiveSupport::HashWithIndifferentAccess)


57
58
59
60
61
62
63
64
# File 'app/models/riiif/image.rb', line 57

def render(args)
  cache_opts = args.select { |a| %w(region size quality rotation format).include? a.to_s }
  key = Image.cache_key(id, cache_opts)

  cache.fetch(key, compress: true, expires_in: Image.expires_in) do
    file.extract(OptionDecoder.decode(args, info))
  end
end