Encapsulates the notion of a mime type. Can be used at render time, for example, with:

  class PostsController < ActionController::Base
    def show
      @post = Post.find(params[:id])

      respond_to do |format|
        format.html
        format.ics { render :text => post.to_ics, :mime_type => Mime::Type["text/calendar"]  }
        format.xml { render :xml => @people.to_xml }
      end
    end
  end
Methods
#
H
L
N
P
R
T
U
V
Classes and Modules
Constants
TRAILING_STAR_REGEXP = /(text|application)\/\*/
Q_SEPARATOR_REGEXP = /;\s*q=/
Attributes
[R] symbol
Class Public methods
lookup(string)
    # File actionpack/lib/action_dispatch/http/mime_type.rb, line 88
88:       def lookup(string)
89:         LOOKUP[string]
90:       end
lookup_by_extension(extension)
    # File actionpack/lib/action_dispatch/http/mime_type.rb, line 92
92:       def lookup_by_extension(extension)
93:         EXTENSION_LOOKUP[extension.to_s]
94:       end
new(string, symbol = nil, synonyms = [])
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 208
208:     def initialize(string, symbol = nil, synonyms = [])
209:       @symbol, @synonyms = symbol, synonyms
210:       @string = string
211:     end
parse(accept_header)
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 111
111:       def parse(accept_header)
112:         if accept_header !~ /,/
113:           accept_header = accept_header.split(Q_SEPARATOR_REGEXP).first
114:           if accept_header =~ TRAILING_STAR_REGEXP
115:             parse_data_with_trailing_star($1)
116:           else
117:             [Mime::Type.lookup(accept_header)]
118:           end
119:         else
120:           # keep track of creation order to keep the subsequent sort stable
121:           list, index = [], 0
122:           accept_header.split(/,/).each do |header|
123:             params, q = header.split(Q_SEPARATOR_REGEXP)
124:             if params.present?
125:               params.strip!
126: 
127:               if params =~ TRAILING_STAR_REGEXP
128:                 parse_data_with_trailing_star($1).each do |m|
129:                   list << AcceptItem.new(index, m.to_s, q)
130:                   index += 1
131:                 end
132:               else
133:                 list << AcceptItem.new(index, params, q)
134:                 index += 1
135:               end
136:             end
137:           end
138:           list.sort!
139: 
140:           # Take care of the broken text/xml entry by renaming or deleting it
141:           text_xml = list.index("text/xml")
142:           app_xml = list.index(Mime::XML.to_s)
143: 
144:           if text_xml && app_xml
145:             # set the q value to the max of the two
146:             list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
147: 
148:             # make sure app_xml is ahead of text_xml in the list
149:             if app_xml > text_xml
150:               list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
151:               app_xml, text_xml = text_xml, app_xml
152:             end
153: 
154:             # delete text_xml from the list
155:             list.delete_at(text_xml)
156: 
157:           elsif text_xml
158:             list[text_xml].name = Mime::XML.to_s
159:           end
160: 
161:           # Look for more specific XML-based types and sort them ahead of app/xml
162: 
163:           if app_xml
164:             idx = app_xml
165:             app_xml_type = list[app_xml]
166: 
167:             while(idx < list.length)
168:               type = list[idx]
169:               break if type.q < app_xml_type.q
170:               if type.name =~ /\+xml$/
171:                 list[app_xml], list[idx] = list[idx], list[app_xml]
172:                 app_xml = idx
173:               end
174:               idx += 1
175:             end
176:           end
177: 
178:           list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
179:           list
180:         end
181:       end
parse_data_with_trailing_star(input)

input: ‘text’ returned value: [Mime::JSON, Mime::XML, Mime::ICS, Mime::HTML, Mime::CSS, Mime::CSV, Mime::JS, Mime::YAML, Mime::TEXT]

input: ‘application’ returned value: [Mime::HTML, Mime::JS, Mime::XML, Mime::YAML, Mime::ATOM, Mime::JSON, Mime::RSS, Mime::URL_ENCODED_FORM]

     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 188
188:       def parse_data_with_trailing_star(input)
189:         Mime::SET.select { |m| m =~ input }
190:       end
register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 102
102:       def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
103:         Mime.const_set(symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms))
104: 
105:         SET << Mime.const_get(symbol.to_s.upcase)
106: 
107:         ([string] + mime_type_synonyms).each { |str| LOOKUP[str] = SET.last } unless skip_lookup
108:         ([symbol] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext.to_s] = SET.last }
109:       end
register_alias(string, symbol, extension_synonyms = [])

Registers an alias that’s not used on mime type lookup, but can be referenced directly. Especially useful for rendering different HTML versions depending on the user agent, like an iPhone.

     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 98
 98:       def register_alias(string, symbol, extension_synonyms = [])
 99:         register(string, symbol, [], extension_synonyms, true)
100:       end
unregister(symbol)

This method is opposite of register method.

Usage:

Mime::Type.unregister(:mobile)

     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 197
197:       def unregister(symbol)
198:         symbol = symbol.to_s.upcase
199:         mime = Mime.const_get(symbol)
200:         Mime.instance_eval { remove_const(symbol) }
201: 
202:         SET.delete_if { |v| v.eql?(mime) }
203:         LOOKUP.delete_if { |k,v| v.eql?(mime) }
204:         EXTENSION_LOOKUP.delete_if { |k,v| v.eql?(mime) }
205:       end
Instance Public methods
==(mime_type)
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 237
237:     def ==(mime_type)
238:       return false if mime_type.blank?
239:       (@synonyms + [ self ]).any? do |synonym|
240:         synonym.to_s == mime_type.to_s || synonym.to_sym == mime_type.to_sym
241:       end
242:     end
===(list)
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 229
229:     def ===(list)
230:       if list.is_a?(Array)
231:         (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) }
232:       else
233:         super
234:       end
235:     end
=~(mime_type)
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 244
244:     def =~(mime_type)
245:       return false if mime_type.blank?
246:       regexp = Regexp.new(Regexp.quote(mime_type.to_s))
247:       (@synonyms + [ self ]).any? do |synonym|
248:         synonym.to_s =~ regexp
249:       end
250:     end
html?()
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 258
258:     def html?
259:       @@html_types.include?(to_sym) || @string =~ /html/
260:     end
ref()
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 225
225:     def ref
226:       to_sym || to_s
227:     end
to_s()
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 213
213:     def to_s
214:       @string
215:     end
to_str()
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 217
217:     def to_str
218:       to_s
219:     end
to_sym()
     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 221
221:     def to_sym
222:       @symbol
223:     end
verify_request?()

Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See ActionController::RequestForgeryProtection.

     # File actionpack/lib/action_dispatch/http/mime_type.rb, line 254
254:     def verify_request?
255:       @@browser_generated_types.include?(to_sym)
256:     end