Sha256: 162411a3c00c66342e285220a9b37d20ef97b20b9dd10c90e5698f4b44cf4ff6

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

require 'mime/types'
require 'pathname'

class ImagePath
	class CouldNotDetermineFileExtensionError < ArgumentError
		def initialize(mime_type)
			super "could not determine file extension for mime type: #{mime_type}"
		end
	end

	def initialize(id)
		@id = id.to_s
	end

	private

	def mime_extension(mime_type)
		mime = MIME::Types[mime_type].first or raise CouldNotDetermineFileExtensionError, mime_type
		'.' + (mime.extensions.select{|e| e.length == 3}.first or mime.extensions.first)
	end

	class Auto < ImagePath
		def original_image(mime_type)
			"#{@id}#{mime_extension(mime_type)}"
		end

		def thumbnail_image(mime_type, thumbnail_class)
			"#{@id}/#{thumbnail_class}#{mime_extension(mime_type)}"
		end
	end

	class Custom < ImagePath
		def initialize(id, path)
			super(id)
			@path = Pathname.new(path)
		end

		def original_image(mime_type)
			extension = begin
				mime_extension(mime_type)
			rescue CouldNotDetermineFileExtensionError
				raise if @path.extname.empty?
				@path.extname
			end

			(@path.dirname + @id + "#{@path.basename(@path.extname)}#{extension}").to_s
		end

		def thumbnail_image(mime_type, thumbnail_class)
			(@path.dirname + @id + "#{@path.basename(@path.extname)}-#{thumbnail_class}#{mime_extension(mime_type)}").to_s
		end
	end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
httpimagestore-0.5.0 lib/httpimagestore/image_path.rb
httpimagestore-0.4.0 lib/httpimagestore/image_path.rb
httpimagestore-0.3.2 lib/httpimagestore/image_path.rb
httpimagestore-0.3.1 lib/httpimagestore/image_path.rb
httpimagestore-0.3.0 lib/httpimagestore/image_path.rb
httpimagestore-0.2.4 lib/httpimagestore/image_path.rb
httpimagestore-0.2.3 lib/httpimagestore/image_path.rb
httpimagestore-0.2.2 lib/httpimagestore/image_path.rb
httpimagestore-0.2.1 lib/httpimagestore/image_path.rb