Skip to content

Commit

Permalink
Merge pull request #46 from curationexperts/struct
Browse files Browse the repository at this point in the history
Represent the transformation as a struct, not a hash
  • Loading branch information
mejackreed authored Mar 15, 2017
2 parents a7741d6 + 4d5d69d commit a80d392
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
16 changes: 8 additions & 8 deletions app/models/riiif/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ def self.create(ext = nil, _validate = true, &block)

end

def extract(options)

# @param [Transformation] transformation
def extract(transformation)
command = 'convert'
command << " -crop #{options[:crop]}" if options[:crop]
command << " -resize #{options[:size]}" if options[:size]
if options[:rotation]
command << " -virtual-pixel white +distort srt #{options[:rotation]}"
command << " -crop #{transformation.crop}" if transformation.crop
command << " -resize #{transformation.size}" if transformation.size
if transformation.rotation
command << " -virtual-pixel white +distort srt #{transformation.rotation}"
end

case options[:quality]
case transformation.quality
when 'grey'
command << ' -colorspace Gray'
when 'bitonal'
command << ' -colorspace Gray'
command << ' -type Bilevel'
end
command << " #{path} #{options[:format]}:-"
command << " #{path} #{transformation.format}:-"
execute(command)
end

Expand Down
13 changes: 7 additions & 6 deletions app/models/riiif/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def expires_in


def cache_key(id, options)
str = options.merge(id: id).delete_if { |_, v| v.nil? }.to_s
str = options.to_h.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
Expand All @@ -62,14 +62,15 @@ def cache_key(id, options)

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

def decode_quality(quality)
Expand Down
2 changes: 2 additions & 0 deletions lib/riiif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ def initialize(orig = nil)
end
end

Transformation = Struct.new(:crop, :size, :quality, :rotation, :format)

mattr_accessor :not_found_image # the image to use when a lookup fails
end

0 comments on commit a80d392

Please sign in to comment.