Object
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
# File lib/action_dispatch/http/mime_type.rb, line 83
83: def lookup(string)
84: LOOKUP[string]
85: end
# File lib/action_dispatch/http/mime_type.rb, line 87
87: def lookup_by_extension(extension)
88: EXTENSION_LOOKUP[extension.to_s]
89: end
# File lib/action_dispatch/http/mime_type.rb, line 165
165: def initialize(string, symbol = nil, synonyms = [])
166: @symbol, @synonyms = symbol, synonyms
167: @string = string
168: end
# File lib/action_dispatch/http/mime_type.rb, line 106
106: def parse(accept_header)
107: if accept_header !~ /,/
108: [Mime::Type.lookup(accept_header)]
109: else
110: # keep track of creation order to keep the subsequent sort stable
111: list = []
112: accept_header.split(/,/).each_with_index do |header, index|
113: params, q = header.split(/;\s*q=/)
114: if params
115: params.strip!
116: list << AcceptItem.new(index, params, q) unless params.empty?
117: end
118: end
119: list.sort!
120:
121: # Take care of the broken text/xml entry by renaming or deleting it
122: text_xml = list.index("text/xml")
123: app_xml = list.index(Mime::XML.to_s)
124:
125: if text_xml && app_xml
126: # set the q value to the max of the two
127: list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
128:
129: # make sure app_xml is ahead of text_xml in the list
130: if app_xml > text_xml
131: list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
132: app_xml, text_xml = text_xml, app_xml
133: end
134:
135: # delete text_xml from the list
136: list.delete_at(text_xml)
137:
138: elsif text_xml
139: list[text_xml].name = Mime::XML.to_s
140: end
141:
142: # Look for more specific XML-based types and sort them ahead of app/xml
143:
144: if app_xml
145: idx = app_xml
146: app_xml_type = list[app_xml]
147:
148: while(idx < list.length)
149: type = list[idx]
150: break if type.q < app_xml_type.q
151: if type.name =~ /\+xml$/
152: list[app_xml], list[idx] = list[idx], list[app_xml]
153: app_xml = idx
154: end
155: idx += 1
156: end
157: end
158:
159: list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
160: list
161: end
162: end
# File lib/action_dispatch/http/mime_type.rb, line 97
97: def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
98: Mime.const_set(symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms))
99:
100: SET << Mime.const_get(symbol.to_s.upcase)
101:
102: ([string] + mime_type_synonyms).each { |str| LOOKUP[str] = SET.last } unless skip_lookup
103: ([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last }
104: 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 lib/action_dispatch/http/mime_type.rb, line 93
93: def register_alias(string, symbol, extension_synonyms = [])
94: register(string, symbol, [], extension_synonyms, true)
95: end
# File lib/action_dispatch/http/mime_type.rb, line 190
190: def ==(mime_type)
191: return false if mime_type.blank?
192: (@synonyms + [ self ]).any? do |synonym|
193: synonym.to_s == mime_type.to_s || synonym.to_sym == mime_type.to_sym
194: end
195: end
# File lib/action_dispatch/http/mime_type.rb, line 182
182: def ===(list)
183: if list.is_a?(Array)
184: (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) }
185: else
186: super
187: end
188: end
# File lib/action_dispatch/http/mime_type.rb, line 197
197: def =~(mime_type)
198: return false if mime_type.blank?
199: regexp = Regexp.new(Regexp.quote(mime_type.to_s))
200: (@synonyms + [ self ]).any? do |synonym|
201: synonym.to_s =~ regexp
202: end
203: end
# File lib/action_dispatch/http/mime_type.rb, line 211
211: def html?
212: @@html_types.include?(to_sym) || @string =~ /html/
213: end
# File lib/action_dispatch/http/mime_type.rb, line 170
170: def to_s
171: @string
172: end
# File lib/action_dispatch/http/mime_type.rb, line 174
174: def to_str
175: to_s
176: end
# File lib/action_dispatch/http/mime_type.rb, line 178
178: def to_sym
179: @symbol || @string.to_sym
180: end
Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See ActionController::RequestForgeryProtection.
# File lib/action_dispatch/http/mime_type.rb, line 207
207: def verify_request?
208: @@browser_generated_types.include?(to_sym)
209: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.