Sha256: df90ebdf8ac2e55d7486b719527871f7cc438d058423b2baa7bb204d3f12f85e

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

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
		end

		class MissingOptionKeyOrValueError < BadThubnailSpecError
			def initialize(option)
				super "missing option key or value in: #{option}"
			end
		end

		class BadDimensionValueError < BadThubnailSpecError
			def initialize(value)
				super "bad dimension value: #{value}"
			end
		end
	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

	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

		opts = {}
		options.each do |option|
			key, value = option.split(':')
			raise BadThubnailSpecError::MissingOptionKeyOrValueError.new(option) unless key and value
			opts[key] = value
		end

		ThumbnailSpec.new(method, width, height, format, opts)
	end


	attr_reader :method, :width, :height, :format, :options

	def to_s
		"#{method} #{width}x#{height} (#{format.downcase}) #{options.inspect}"
	end

	private

	def cast_dimension(string)
		return :input if string == 'input'
		raise BadThubnailSpecError::BadDimensionValueError.new(string) unless string =~ /^\d+$/
		string.to_i
	end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
httpthumbnailer-1.2.0 lib/httpthumbnailer/thumbnail_specs.rb
httpthumbnailer-1.1.2 lib/httpthumbnailer/thumbnail_specs.rb
httpthumbnailer-1.1.1 lib/httpthumbnailer/thumbnail_specs.rb
httpthumbnailer-1.1.0 lib/httpthumbnailer/thumbnail_specs.rb
httpthumbnailer-1.0.0 lib/httpthumbnailer/thumbnail_specs.rb