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
- ==
- ===
- =~
- browser_generated?
- html?
- lookup
- lookup_by_extension
- new
- parse
- register
- register_alias
- to_s
- to_str
- to_sym
- unverifiable_types
- verify_request?
Public Class methods
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 65
65: def lookup(string)
66: LOOKUP[string]
67: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 69
69: def lookup_by_extension(extension)
70: EXTENSION_LOOKUP[extension]
71: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 147
147: def initialize(string, symbol = nil, synonyms = [])
148: @symbol, @synonyms = symbol, synonyms
149: @string = string
150: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 88
88: def parse(accept_header)
89: if accept_header !~ /,/
90: [Mime::Type.lookup(accept_header)]
91: else
92: # keep track of creation order to keep the subsequent sort stable
93: list = []
94: accept_header.split(/,/).each_with_index do |header, index|
95: params, q = header.split(/;\s*q=/)
96: if params
97: params.strip!
98: list << AcceptItem.new(index, params, q) unless params.empty?
99: end
100: end
101: list.sort!
102:
103: # Take care of the broken text/xml entry by renaming or deleting it
104: text_xml = list.index("text/xml")
105: app_xml = list.index(Mime::XML.to_s)
106:
107: if text_xml && app_xml
108: # set the q value to the max of the two
109: list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
110:
111: # make sure app_xml is ahead of text_xml in the list
112: if app_xml > text_xml
113: list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
114: app_xml, text_xml = text_xml, app_xml
115: end
116:
117: # delete text_xml from the list
118: list.delete_at(text_xml)
119:
120: elsif text_xml
121: list[text_xml].name = Mime::XML.to_s
122: end
123:
124: # Look for more specific XML-based types and sort them ahead of app/xml
125:
126: if app_xml
127: idx = app_xml
128: app_xml_type = list[app_xml]
129:
130: while(idx < list.length)
131: type = list[idx]
132: break if type.q < app_xml_type.q
133: if type.name =~ /\+xml$/
134: list[app_xml], list[idx] = list[idx], list[app_xml]
135: app_xml = idx
136: end
137: idx += 1
138: end
139: end
140:
141: list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
142: list
143: end
144: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 79
79: def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
80: Mime.instance_eval { const_set symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms) }
81:
82: SET << Mime.const_get(symbol.to_s.upcase)
83:
84: ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } unless skip_lookup
85: ([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last }
86: 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.
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 75
75: def register_alias(string, symbol, extension_synonyms = [])
76: register(string, symbol, [], extension_synonyms, true)
77: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 33
33: def self.unverifiable_types
34: ActiveSupport::Deprecation.warn("unverifiable_types is deprecated and has no effect", caller)
35: @@unverifiable_types
36: end
Public Instance methods
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 172
172: def ==(mime_type)
173: return false if mime_type.blank?
174: (@synonyms + [ self ]).any? do |synonym|
175: synonym.to_s == mime_type.to_s || synonym.to_sym == mime_type.to_sym
176: end
177: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 164
164: def ===(list)
165: if list.is_a?(Array)
166: (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) }
167: else
168: super
169: end
170: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 179
179: def =~(mime_type)
180: return false if mime_type.blank?
181: regexp = Regexp.new(Regexp.quote(mime_type.to_s))
182: (@synonyms + [ self ]).any? do |synonym|
183: synonym.to_s =~ regexp
184: end
185: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 197
197: def browser_generated?
198: @@browser_generated_types.include?(to_sym)
199: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 193
193: def html?
194: @@html_types.include?(to_sym) || @string =~ /html/
195: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 152
152: def to_s
153: @string
154: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 156
156: def to_str
157: to_s
158: end
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 160
160: def to_sym
161: @symbol || @string.to_sym
162: end
Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See ActionController::RequestForgeryProtection.
[ show source ]
# File actionpack/lib/action_controller/mime_type.rb, line 189
189: def verify_request?
190: browser_generated?
191: end