Parent

Namespace

Class Index [+]

Quicksearch

Mime::Type

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

Constants

TRAILING_STAR_REGEXP
Q_SEPARATOR_REGEXP

Attributes

symbol[R]

Public Class Methods

lookup(string) click to toggle source
    # File lib/action_dispatch/http/mime_type.rb, line 87
87:       def lookup(string)
88:         LOOKUP[string]
89:       end
lookup_by_extension(extension) click to toggle source
    # File lib/action_dispatch/http/mime_type.rb, line 91
91:       def lookup_by_extension(extension)
92:         EXTENSION_LOOKUP[extension.to_s]
93:       end
new(string, symbol = nil, synonyms = []) click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 207
207:     def initialize(string, symbol = nil, synonyms = [])
208:       @symbol, @synonyms = symbol, synonyms
209:       @string = string
210:     end
parse(accept_header) click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 110
110:       def parse(accept_header)
111:         if accept_header !~ /,/
112:           accept_header = accept_header.split(Q_SEPARATOR_REGEXP).first
113:           if accept_header =~ TRAILING_STAR_REGEXP
114:             parse_data_with_trailing_star($1)
115:           else
116:             [Mime::Type.lookup(accept_header)]
117:           end
118:         else
119:           # keep track of creation order to keep the subsequent sort stable
120:           list, index = [], 0
121:           accept_header.split(/,/).each do |header|
122:             params, q = header.split(Q_SEPARATOR_REGEXP)
123:             if params.present?
124:               params.strip!
125: 
126:               if params =~ TRAILING_STAR_REGEXP
127:                 parse_data_with_trailing_star($1).each do |m|
128:                   list << AcceptItem.new(index, m.to_s, q)
129:                   index += 1
130:                 end
131:               else
132:                 list << AcceptItem.new(index, params, q)
133:                 index += 1
134:               end
135:             end
136:           end
137:           list.sort!
138: 
139:           # Take care of the broken text/xml entry by renaming or deleting it
140:           text_xml = list.index("text/xml")
141:           app_xml = list.index(Mime::XML.to_s)
142: 
143:           if text_xml && app_xml
144:             # set the q value to the max of the two
145:             list[app_xml].q = [list[text_xml].q, list[app_xml].q].max
146: 
147:             # make sure app_xml is ahead of text_xml in the list
148:             if app_xml > text_xml
149:               list[app_xml], list[text_xml] = list[text_xml], list[app_xml]
150:               app_xml, text_xml = text_xml, app_xml
151:             end
152: 
153:             # delete text_xml from the list
154:             list.delete_at(text_xml)
155: 
156:           elsif text_xml
157:             list[text_xml].name = Mime::XML.to_s
158:           end
159: 
160:           # Look for more specific XML-based types and sort them ahead of app/xml
161: 
162:           if app_xml
163:             idx = app_xml
164:             app_xml_type = list[app_xml]
165: 
166:             while(idx < list.length)
167:               type = list[idx]
168:               break if type.q < app_xml_type.q
169:               if type.name =~ /\+xml$/
170:                 list[app_xml], list[idx] = list[idx], list[app_xml]
171:                 app_xml = idx
172:               end
173:               idx += 1
174:             end
175:           end
176: 
177:           list.map! { |i| Mime::Type.lookup(i.name) }.uniq!
178:           list
179:         end
180:       end
parse_data_with_trailing_star(input) click to toggle source

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 lib/action_dispatch/http/mime_type.rb, line 187
187:       def parse_data_with_trailing_star(input)
188:         Mime::SET.select { |m| m =~ input }
189:       end
register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false) click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 101
101:       def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], skip_lookup = false)
102:         Mime.const_set(symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms))
103: 
104:         SET << Mime.const_get(symbol.to_s.upcase)
105: 
106:         ([string] + mime_type_synonyms).each { |str| LOOKUP[str] = SET.last } unless skip_lookup
107:         ([symbol] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext.to_s] = SET.last }
108:       end
register_alias(string, symbol, extension_synonyms = []) click to toggle source

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 97
97:       def register_alias(string, symbol, extension_synonyms = [])
98:         register(string, symbol, [], extension_synonyms, true)
99:       end
unregister(symbol) click to toggle source

This method is opposite of register method.

Usage:

Mime::Type.unregister(:mobile)

     # File lib/action_dispatch/http/mime_type.rb, line 196
196:       def unregister(symbol)
197:         symbol = symbol.to_s.upcase
198:         mime = Mime.const_get(symbol)
199:         Mime.instance_eval { remove_const(symbol) }
200: 
201:         SET.delete_if { |v| v.eql?(mime) }
202:         LOOKUP.delete_if { |k,v| v.eql?(mime) }
203:         EXTENSION_LOOKUP.delete_if { |k,v| v.eql?(mime) }
204:       end

Public Instance Methods

==(mime_type) click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 236
236:     def ==(mime_type)
237:       return false if mime_type.blank?
238:       (@synonyms + [ self ]).any? do |synonym|
239:         synonym.to_s == mime_type.to_s || synonym.to_sym == mime_type.to_sym
240:       end
241:     end
===(list) click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 228
228:     def ===(list)
229:       if list.is_a?(Array)
230:         (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) }
231:       else
232:         super
233:       end
234:     end
=~(mime_type) click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 243
243:     def =~(mime_type)
244:       return false if mime_type.blank?
245:       regexp = Regexp.new(Regexp.quote(mime_type.to_s))
246:       (@synonyms + [ self ]).any? do |synonym|
247:         synonym.to_s =~ regexp
248:       end
249:     end
html?() click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 257
257:     def html?
258:       @@html_types.include?(to_sym) || @string =~ /html/
259:     end
ref() click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 224
224:     def ref
225:       to_sym || to_s
226:     end
to_s() click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 212
212:     def to_s
213:       @string
214:     end
to_str() click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 216
216:     def to_str
217:       to_s
218:     end
to_sym() click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 220
220:     def to_sym
221:       @symbol
222:     end
verify_request?() click to toggle source

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 253
253:     def verify_request?
254:       @@browser_generated_types.include?(to_sym)
255:     end

Private Instance Methods

method_missing(method, *args) click to toggle source
     # File lib/action_dispatch/http/mime_type.rb, line 266
266:       def method_missing(method, *args)
267:         if method.to_s =~ /(\w+)\?$/
268:           $1.downcase.to_sym == to_sym
269:         else
270:           super
271:         end
272:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.