| 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 47
47: def lookup(string)
48: LOOKUP[string]
49: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 51
51: def lookup_by_extension(extension)
52: EXTENSION_LOOKUP[extension]
53: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 122
122: def initialize(string, symbol = nil, synonyms = [])
123: @symbol, @synonyms = symbol, synonyms
124: @string = string
125: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 70
70: def parse(accept_header)
71: # keep track of creation order to keep the subsequent sort stable
72: list = []
73: accept_header.split(/,/).each_with_index do |header, index|
74: params = header.split(/;\s*q=/)
75: list << AcceptItem.new(index, *params) unless params.empty?
76: end
77: list.sort!
78:
79: # Take care of the broken text/xml entry by renaming or deleting it
80: text_xml = list.index("text/xml")
81: app_xml = list.index(Mime::XML.to_s)
82:
83: if text_xml && app_xml
84: # set the q value to the max of the two
85: list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
86:
87: # make sure app_xml is ahead of text_xml in the list
88: if app_xml > text_xml
89: list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
90: app_xml, text_xml = text_xml, app_xml
91: end
92:
93: # delete text_xml from the list
94: list.delete_at(text_xml)
95:
96: elsif text_xml
97: list[text_xml].name = Mime::XML.to_s
98: end
99:
100: # Look for more specific xml-based types and sort them ahead of app/xml
101:
102: if app_xml
103: idx = app_xml
104: app_xml_type = list[app_xml]
105:
106: while(idx < list.length)
107: type = list[idx]
108: break if type.q < app_xml_type.q
109: if type.name =~ /\+xml$/
110: list[app_xml], list[idx] = list[idx], list[app_xml]
111: app_xml = idx
112: end
113: idx += 1
114: end
115: end
116:
117: list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
118: list
119: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 61
61: def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
62: Mime.instance_eval { const_set symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms) }
63:
64: SET << Mime.const_get(symbol.to_s.upcase)
65:
66: ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } unless skip_lookup
67: ([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last }
68: 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 57
57: def register_alias(string, symbol, extension_synonyms = [])
58: register(string, symbol, [], extension_synonyms, true)
59: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 147
147: def ==(mime_type)
148: (@synonyms + [ self ]).any? { |synonym| synonym.to_s == mime_type.to_s } if mime_type
149: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 139
139: def ===(list)
140: if list.is_a?(Array)
141: (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) }
142: else
143: super
144: end
145: end
# File vendor/rails/actionpack/lib/action_controller/mime_type.rb, line 127
127: def to_s
128: @string
129: end