Class: Utopia::Static::MimeTypeLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/static/mime_types.rb

Overview

A class to assist with loading mime-type metadata.

Defined Under Namespace

Classes: ExpansionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library) ⇒ MimeTypeLoader

Returns a new instance of MimeTypeLoader.



64
65
66
67
# File 'lib/utopia/static/mime_types.rb', line 64

def initialize(library)
	@extensions = {}
	@library = library
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



69
70
71
# File 'lib/utopia/static/mime_types.rb', line 69

def extensions
  @extensions
end

Class Method Details

.extensions_for(types, library = MIME_TYPES) ⇒ Object



71
72
73
74
75
# File 'lib/utopia/static/mime_types.rb', line 71

def self.extensions_for(types, library = MIME_TYPES)
	loader = self.new(library)
	loader.expand(types)
	return loader.extensions
end

Instance Method Details

#expand(types) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/utopia/static/mime_types.rb', line 88

def expand(types)
	types.each do |type|
		current_count = @extensions.size
		
		begin
			case type
			when Symbol
				self.expand(MIME_TYPES[type])
			when Array
				@extensions["." + type[0]] = type[1]
			when String
				self.extract_extensions MIME::Types.of(type)
			when Regexp
				self.extract_extensions MIME::Types[type]
			when MIME::Type
				self.extract_extensions.call([type])
			end
		rescue
			raise ExpansionError.new("#{self.class.name}: Error while processing #{type.inspect}!")
		end
		
		if @extensions.size == current_count
			raise ExpansionError.new("#{self.class.name}: Could not find any mime type for #{type.inspect}")
		end
	end
end

#extract_extensions(mime_types) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/utopia/static/mime_types.rb', line 77

def extract_extensions(mime_types)
	mime_types.select{|mime_type| !mime_type.obsolete?}.each do |mime_type|
		mime_type.extensions.each do |ext|
			@extensions["." + ext] = mime_type.content_type
		end
	end
end