Class: Mack::Utils::MimeTypes

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mack/utils/mime_types.rb

Overview

Houses the MimeTypes used by the system to set the proper ‘Content-Type’ for requests.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMimeTypes

:nodoc:



7
8
9
# File 'lib/mack/utils/mime_types.rb', line 7

def initialize # :nodoc:
  @types = YAML.load(File.open(File.join(File.dirname(__FILE__), "mime_types.yml")))
end

Class Method Details

.[](name) ⇒ Object

Maps to Mack::Utils::MimeTypes.instance.get



43
44
45
# File 'lib/mack/utils/mime_types.rb', line 43

def [](name)
  Mack::Utils::MimeTypes.instance.get(name.to_sym)
end

.register(name, type) ⇒ Object

Maps to Mack::Utils::MimeTypes.instance.register



48
49
50
# File 'lib/mack/utils/mime_types.rb', line 48

def register(name, type)
  Mack::Utils::MimeTypes.instance.register(name, type)
end

Instance Method Details

#get(name) ⇒ Object

Returns the type registered for the key, if there is no type for the key, then “text/html” is returned

Examples: Mack::Utils::MimeTypes.get(:iphone)

# => "app/iphone"

Mack::Utils::MimeTypes.get(:i_dont_exist)

# => "text/html"


36
37
38
# File 'lib/mack/utils/mime_types.rb', line 36

def get(name)
  @types[name.to_sym] || "text/html"
end

#register(name, type) ⇒ Object

Registers a new mime-type to the system. If the key already exists, it will be appended to the existing type.

Examples: Mack::Utils::MimeTypes.register(:iphone, “app/iphone”)

"/users.iphone" # => will have a 'Content-Type' header of "app/iphone"

Mack::Utils::MimeTypes.register(:iphone, “application/mac-iphone”)

"/users.iphone" # => will have a 'Content-Type' header of "app/iphone; application/mac-iphone"


19
20
21
22
23
24
25
26
# File 'lib/mack/utils/mime_types.rb', line 19

def register(name, type)
  name = name.to_sym
  if @types.has_key?(name)
    @types[name] << "; " << type
  else
    @types[name] = type
  end
end