Class: Riiif::OptionDecoder
- Inherits:
-
Object
- Object
- Riiif::OptionDecoder
- 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
-
#image_info ⇒ Object
readonly
Returns the value of attribute image_info.
Class Method Summary collapse
-
.decode(options, image_info) ⇒ Object
a helper method for instantiating the OptionDecoder.
Instance Method Summary collapse
- #decode ⇒ Transformation
- #decode_quality(quality) ⇒ Object
- #decode_region(region) ⇒ Object
- #decode_rotation(rotation) ⇒ Object
-
#decode_size(size) ⇒ Object
rubocop:disable Metrics/PerceivedComplexity.
-
#initialize(options, image_info) ⇒ OptionDecoder
constructor
A new instance of OptionDecoder.
- #validate_format!(format) ⇒ Object
Constructor Details
#initialize(options, image_info) ⇒ OptionDecoder
Returns a new instance of OptionDecoder.
15 16 17 18 |
# File 'app/services/riiif/option_decoder.rb', line 15 def initialize(, image_info) = @image_info = image_info end |
Instance Attribute Details
#image_info ⇒ Object (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
9 10 11 |
# File 'app/services/riiif/option_decoder.rb', line 9 def self.decode(, image_info) new(, image_info).decode end |
Instance Method Details
#decode ⇒ Transformation
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 [:format] validate_format!([:format]) Riiif::Transformation.new(decode_region(.delete(:region)), decode_size(.delete(:size)), decode_quality([:quality]), decode_rotation([:rotation]), [: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 |