lib/httpthumbnailer/thumbnail_specs.rb in httpthumbnailer-0.3.1 vs lib/httpthumbnailer/thumbnail_specs.rb in httpthumbnailer-1.0.0

- old
+ new

@@ -1,8 +1,30 @@ -require 'httpthumbnailer/thumbnailer' - class ThumbnailSpecs < Array + def self.from_uri(specs) + ts = ThumbnailSpecs.new + specs.split('/').each do |spec| + ts << ThumbnailSpec.from_uri(spec) + end + ts + end + + def max_width + map do |spec| + return nil unless spec.width.is_a? Integer + spec.width + end.max + end + + def max_height + map do |spec| + return nil unless spec.height.is_a? Integer + spec.height + end.max + end +end + +class ThumbnailSpec class BadThubnailSpecError < ArgumentError class MissingArgumentError < BadThubnailSpecError def initialize(spec) super "missing argument in: #{spec}" end @@ -12,69 +34,50 @@ def initialize(option) super "missing option key or value in: #{option}" end end - class BadDimmensionValueError < BadThubnailSpecError + class BadDimensionValueError < BadThubnailSpecError def initialize(value) - super "bad dimmension value: #{value}" + super "bad dimension value: #{value}" end end end - class ThumbnailSpec - def initialize(method, width, height, format, options = {}) - @method = method - @width = cast_dimmension(width) - @height = cast_dimmension(height) - @format = (format == 'INPUT' ? :input : format.upcase) - @options = options - end + def initialize(method, width, height, format, options = {}) + @method = method + @width = cast_dimension(width) + @height = cast_dimension(height) + @format = (format == 'input' ? :input : format.upcase) + @options = options + end - attr_reader :method, :width, :height, :format, :options + def self.from_uri(spec) + method, width, height, format, *options = *spec.split(',') + raise BadThubnailSpecError::MissingArgumentError.new(spec) unless method and width and height and format - def to_s - "#{method} #{width}x#{height} (#{format}) #{options.inspect}" + opts = {} + options.each do |option| + key, value = option.split(':') + raise BadThubnailSpecError::MissingOptionKeyOrValueError.new(option) unless key and value + opts[key] = value end - private - - def cast_dimmension(string) - return :input if string == 'INPUT' - raise BadThubnailSpecError::BadDimmensionValueError.new(string) unless string =~ /^\d+$/ - string.to_i - end + ThumbnailSpec.new(method, width, height, format, opts) end - def self.from_uri(specs) - ts = ThumbnailSpecs.new - specs.split('/').each do |spec| - method, width, height, format, *options = *spec.split(',') - raise BadThubnailSpecError::MissingArgumentError.new(spec) unless method and width and height and format - opts = {} - options.each do |option| - key, value = option.split(':') - raise BadThubnailSpecError::MissingOptionKeyOrValueError.new(option) unless key and value - opts[key] = value - end + attr_reader :method, :width, :height, :format, :options - ts << ThumbnailSpec.new(method, width, height, format, opts) - end - ts + def to_s + "#{method} #{width}x#{height} (#{format.downcase}) #{options.inspect}" end - def max_width - map do |spec| - return nil unless spec.width.is_a? Integer - spec.width - end.max - end + private - def max_height - map do |spec| - return nil unless spec.height.is_a? Integer - spec.height - end.max + def cast_dimension(string) + return :input if string == 'input' + raise BadThubnailSpecError::BadDimensionValueError.new(string) unless string =~ /^\d+$/ + string.to_i end end