| Class | Mime::Type |
| In: |
vendor/rails/actionpack/lib/action_controller/mime_type.rb
|
| Parent: | 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 vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 51
51: def lookup(string)
52: LOOKUP[string]
53: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 55
55: def lookup_by_extension(extension)
56: EXTENSION_LOOKUP[extension]
57: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 129
129: def initialize(string, symbol = nil, synonyms = [])
130: @symbol, @synonyms = symbol, synonyms
131: @string = string
132: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 74
74: def parse(accept_header)
75: # keep track of creation order to keep the subsequent sort stable
76: list = []
77: accept_header.split(/,/).each_with_index do |header, index|
78: params, q = header.split(/;\s*q=/)
79: if params
80: params.strip!
81: list << AcceptItem.new(index, params, q) unless params.empty?
82: end
83: end
84: list.sort!
85:
86: # Take care of the broken text/xml entry by renaming or deleting it
87: text_xml = list.index("text/xml")
88: app_xml = list.index(Mime::XML.to_s)
89:
90: if text_xml && app_xml
91: # set the q value to the max of the two
92: list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
93:
94: # make sure app_xml is ahead of text_xml in the list
95: if app_xml > text_xml
96: list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
97: app_xml, text_xml = text_xml, app_xml
98: end
99:
100: # delete text_xml from the list
101: list.delete_at(text_xml)
102:
103: elsif text_xml
104: list[text_xml].name = Mime::XML.to_s
105: end
106:
107: # Look for more specific XML-based types and sort them ahead of app/xml
108:
109: if app_xml
110: idx = app_xml
111: app_xml_type = list[app_xml]
112:
113: while(idx < list.length)
114: type = list[idx]
115: break if type.q < app_xml_type.q
116: if type.name =~ /\+xml$/
117: list[app_xml], list[idx] = list[idx], list[app_xml]
118: app_xml = idx
119: end
120: idx += 1
121: end
122: end
123:
124: list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
125: list
126: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 65
65: def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
66: Mime.instance_eval { const_set symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms) }
67:
68: SET << Mime.const_get(symbol.to_s.upcase)
69:
70: ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } unless skip_lookup
71: ([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last }
72: 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 vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 61
61: def register_alias(string, symbol, extension_synonyms = [])
62: register(string, symbol, [], extension_synonyms, true)
63: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 154
154: def ==(mime_type)
155: return false if mime_type.blank?
156: (@synonyms + [ self ]).any? do |synonym|
157: synonym.to_s == mime_type.to_s || synonym.to_sym == mime_type.to_sym
158: end
159: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 146
146: def ===(list)
147: if list.is_a?(Array)
148: (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) }
149: else
150: super
151: end
152: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 167
167: def html?
168: @@html_types.include?(to_sym) || @string =~ /html/
169: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 134
134: def to_s
135: @string
136: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 138
138: def to_str
139: to_s
140: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 142
142: def to_sym
143: @symbol || @string.to_sym
144: end
Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See ActionController::RequestForgerProtection.
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 163
163: def verify_request?
164: !@@unverifiable_types.include?(to_sym)
165: end