Class: IIIF::Image::OptionDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/iiif/image/services/option_decoder.rb

Overview

Decodes the URL parameters into a Transformation object

Constant Summary collapse

OUTPUT_FORMATS =
%w(jpg png).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OptionDecoder

Returns a new instance of OptionDecoder.

Parameters:

  • options (ActiveSupport::HashWithIndifferentAccess)


15
16
17
# File 'lib/iiif/image/services/option_decoder.rb', line 15

def initialize(options)
  @options = options
end

Class Method Details

.decode(options) ⇒ Object

a helper method for instantiating the OptionDecoder

Parameters:

  • options (ActiveSupport::HashWithIndifferentAccess)


10
11
12
# File 'lib/iiif/image/services/option_decoder.rb', line 10

def self.decode(options)
  new(options).decode
end

Instance Method Details

#decodeTransformation

Returns:



21
22
23
24
25
26
27
# File 'lib/iiif/image/services/option_decoder.rb', line 21

def decode
  IIIF::Image::Transformation.new(region: decode_region(@options.delete(:region)),
                           size: decode_size(@options.delete(:size)),
                           quality: decode_quality(@options[:quality]),
                           rotation: decode_rotation(@options[:rotation]),
                           format: decode_format(@options[:format]))
end

#decode_format(format) ⇒ Object



44
45
46
47
48
# File 'lib/iiif/image/services/option_decoder.rb', line 44

def decode_format(format)
  format ||= 'jpg'
  return format if OUTPUT_FORMATS.include?(format)
  raise InvalidAttributeError, "Unsupported format: #{format}"
end

#decode_quality(quality) ⇒ Object



29
30
31
32
33
# File 'lib/iiif/image/services/option_decoder.rb', line 29

def decode_quality(quality)
  return quality if %w(bitonal grey default color).include?(quality)
  return 'default' if quality.nil?
  raise InvalidAttributeError, "Unsupported quality: #{quality}"
end

#decode_region(region) ⇒ Object

rubocop:disable Metrics/AbcSize



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/iiif/image/services/option_decoder.rb', line 51

def decode_region(region)
  if region.nil? || region == 'full'
    Region::Full.new
  elsif md = /^pct:(\d+(?:.\d+)?),(\d+(?:.\d+)?),(\d+(?:.\d+)?),(\d+(?:.\d+)?)$/.match(region)
    Region::Percent
      .new(md[1].to_f, md[2].to_f, md[3].to_f, md[4].to_f)
  elsif md = /^(\d+),(\d+),(\d+),(\d+)$/.match(region)
    Region::Absolute.new(md[1].to_i, md[2].to_i, md[3].to_i, md[4].to_i)
  elsif region == 'square'
    Region::Square.new
  else
    raise InvalidAttributeError, "Invalid region: #{region}"
  end
end

#decode_rotation(rotation) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/iiif/image/services/option_decoder.rb', line 35

def decode_rotation(rotation)
  return 0 if rotation.nil? || rotation == '0'
  begin
    Float(rotation)
  rescue ArgumentError
    raise InvalidAttributeError, "Unsupported rotation: #{rotation}"
  end
end

#decode_size(size) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/iiif/image/services/option_decoder.rb', line 66

def decode_size(size)
  if size.nil? || size == 'max'
    Size::Max.new
  elsif size == 'full'
    Size::Full.new # Deprecated
  elsif md = /^,(\d+)$/.match(size)
    Size::Height.new(md[1].to_i)
  elsif md = /^(\d+),$/.match(size)
    Size::Width.new(md[1].to_i)
  elsif md = /^pct:(\d+(?:.\d+)?)$/.match(size)
    Size::Percent.new(md[1].to_f)
  elsif md = /^(\d+),(\d+)$/.match(size)
    Size::Absolute.new(md[1].to_i, md[2].to_i)
  elsif md = /^!(\d+),(\d+)$/.match(size)
    Size::BestFit.new(md[1].to_i, md[2].to_i)
  else
    raise InvalidAttributeError, "Invalid size: #{size}"
  end
end