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
- CLASS Mime::Type::AcceptItem
Constants
| TRAILING_STAR_REGEXP | = | /(text|application)\/\*/ |
| Q_SEPARATOR_REGEXP | = | /;\s*q=/ |
Attributes
| [R] | symbol |
Class Public methods
# 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
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 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
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 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
Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See ActionController::RequestForgeryProtection.