Class: Riiif::OptionDecoder

Inherits:
Object
  • Object
show all
Defined in:
app/services/riiif/option_decoder.rb

Overview

Decodes the URL parameters into a Transformation object

Constant Summary collapse

OUTPUT_FORMATS =
%w(jpg png).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, image_info) ⇒ OptionDecoder

Returns a new instance of OptionDecoder.

Parameters:

  • options (ActiveSupport::HashWithIndifferentAccess)
  • image_info (ImageInformation)


15
16
17
18
# File 'app/services/riiif/option_decoder.rb', line 15

def initialize(options, image_info)
  @options = options
  @image_info = image_info
end

Instance Attribute Details

#image_infoObject (readonly)

Returns the value of attribute image_info.



20
21
22
# File 'app/services/riiif/option_decoder.rb', line 20

def image_info
  @image_info
end

Class Method Details

.decode(options, image_info) ⇒ Object

a helper method for instantiating the OptionDecoder

Parameters:

  • options (ActiveSupport::HashWithIndifferentAccess)
  • image_info (ImageInformation)


9
10
11
# File 'app/services/riiif/option_decoder.rb', line 9

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

Instance Method Details

#decodeTransformation

Returns:

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
# File 'app/services/riiif/option_decoder.rb', line 24

def decode
  raise ArgumentError, "You must provide a format. You provided #{@options}" unless @options[:format]
  validate_format!(@options[:format])
  Riiif::Transformation.new(decode_region(@options.delete(:region)),
                            decode_size(@options.delete(:size)),
                            decode_quality(@options[:quality]),
                            decode_rotation(@options[:rotation]),
                            @options[:format])
end

#decode_quality(quality) ⇒ Object



34
35
36
37
38
# File 'app/services/riiif/option_decoder.rb', line 34

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

#decode_region(region) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/riiif/option_decoder.rb', line 53

def decode_region(region)
  if region.nil? || region == 'full'
    Riiif::Region::Imagemagick::FullDecoder.new.decode
  elsif md = /^pct:(\d+),(\d+),(\d+),(\d+)$/.match(region)
    Riiif::Region::Imagemagick::PercentageDecoder
      .new(image_info, md[1], md[2], md[3], md[4]).decode
  elsif md = /^(\d+),(\d+),(\d+),(\d+)$/.match(region)
    Riiif::Region::Imagemagick::AbsoluteDecoder.new(md[1], md[2], md[3], md[4]).decode
  elsif region == 'square'
    Riiif::Region::Imagemagick::SquareDecoder.new(image_info).decode
  else
    raise InvalidAttributeError, "Invalid region: #{region}"
  end
end

#decode_rotation(rotation) ⇒ Object



40
41
42
43
44
45
46
47
# File 'app/services/riiif/option_decoder.rb', line 40

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

#decode_size(size) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/services/riiif/option_decoder.rb', line 69

def decode_size(size)
  if size.nil? || size == 'full'
    Riiif::Size::Imagemagick::FullDecoder.new.decode
  elsif md = /^,(\d+)$/.match(size)
    Riiif::Size::Imagemagick::HeightDecoder.new(md[1]).decode
  elsif md = /^(\d+),$/.match(size)
    Riiif::Size::Imagemagick::WidthDecoder.new(md[1]).decode
  elsif md = /^pct:(\d+(.\d+)?)$/.match(size)
    Riiif::Size::Imagemagick::PercentDecoder.new(md[1]).decode
  elsif md = /^(\d+),(\d+)$/.match(size)
    Riiif::Size::Imagemagick::AbsoluteDecoder.new(md[1], md[2]).decode
  elsif md = /^!(\d+),(\d+)$/.match(size)
    Riiif::Size::Imagemagick::BestFitDecoder.new(md[1], md[2]).decode
  else
    raise InvalidAttributeError, "Invalid size: #{size}"
  end
end

#validate_format!(format) ⇒ Object



49
50
51
# File 'app/services/riiif/option_decoder.rb', line 49

def validate_format!(format)
  raise InvalidAttributeError, "Unsupported format: #{format}" unless OUTPUT_FORMATS.include?(format)
end