Module: Pancake::MimeTypes

Defined in:
lib/pancake/mime_types.rb

Defined Under Namespace

Classes: Type

Class Method Summary collapse

Class Method Details

.group(name) ⇒ Enumerable<Pancake::MimeTypes::Type>

Pancake::MimeTypes are managed by grouping them together. Each group may consist of many mime types by extension By Accessing a group that doesn’t yet exist, the group is created and initialized with the matching type with extension A group may have many mime type / accept types associated with it

mime types in the specified group. If the group does not exist, or has not been accessed, the group will be created on the fly with the first mime type that matches the group name

Examples:

Pancake::MimeTypes.group(:html)

Parameters:

  • name (String, Symbol)
    • The name of the group

Returns:

See Also:

Author:

  • Daniel Neighman



71
72
73
# File 'lib/pancake/mime_types.rb', line 71

def self.group(name)
  groups[name.to_s]
end

.group_as(name, *exts) ⇒ Object

Creates a group of mime types. Any group of mimes can be arbitrarily grouped under the specified name. The group is initilized with the mime type whose extension matches the name, and any further extensions have their mime types added to the group

create/append associate with this mime type

Parameters:

  • name (String, Symbol)
    • the name of the group to

  • exts (List of String, Symbol)
    • a list of extensions to

See Also:

Author:

  • Daniel Neighman



89
90
91
92
93
94
# File 'lib/pancake/mime_types.rb', line 89

def self.group_as(name, *exts)
  exts.each do |ext|
    group(name) << type_by_extension(ext) unless group(name).include?(type_by_extension(ext))
  end
  group(name)
end

.groupsSet<Pancake::MimeTypes::Type>

Pancake manages mime types by grouping them together.

objects associated with this group

Returns:

See Also:

Author:

  • Daniel Neighman



47
48
49
# File 'lib/pancake/mime_types.rb', line 47

def self.groups
  @groups || reset! && @groups
end

.negotiate_accept_type(type, *provided) ⇒ Array

Negotiates the type and group that the accept_type string matches request symbols of the included groups to use to try and match this accept type header.

:text)
result # => [:html, <#Pancake::MimeTypes::Type for html>]

Examples:

accept_type = "text/xml,text/html"
result = Pancake::MimeTypes.negotiate_accept_type(accept_type, :html,

Parameters:

  • type (String)

    the accept_type header string from the

  • provided (list of String, Symbol)
    • A list of strings /

Returns:

  • (Array)

    An array with the group name, and mime type

Author:

  • Daniel Neighman



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/pancake/mime_types.rb', line 161

def self.negotiate_accept_type(type, *provided)
  accepted_types = Rack::AcceptMediaTypes.new(type)
  provided = provided.flatten
  key = [accepted_types, provided]
  return negotiated_accept_types[key] if negotiated_accept_types[key]

  accepted_type = nil

  if accepted_types.include?("*/*")
    if provided.include?(:any)
      name = :any
      if at = accepted_types.detect{|t| t != "*/*"}
        at
      elsif t = provided.detect{|a| a != :any}
        at = group(t).first.type_strings.first
      else
        at = "text/html"
      end
    else
      name = provided.first
      accepted_type = group(name).first
      at = accepted_type.type_strings.first
    end
    accepted_type = group(name).first
    negotiated_accept_types[key] = [name, at, accepted_type]
    return negotiated_accept_types[key]
  end

  # Check to see if any accepted types match
  accepted_types.each do |at|
    provided.flatten.each do |name|
      accepted_type = match_content_type(at, name)
      if accepted_type
        if name == :any
          if at = accepted_types.detect{|a| a != "*/*"}
            return [name, at, accepted_type]
          elsif at = provided.flatten.detect{|p| p != :any}
            return [name, at.type_strings.first, accepted_type]
          else
            return [name, "text/html", accept_type]
          end
        end
        at = accepted_type.type_strings.first if at == "*/*"
        if accepted_types.join.size > 4096
        # Don't save the key if it's larger than 4 k.
        # This could hit a dos attack if it's repeatedly hit
        # with anything large
        negotiated_accept_types[key] = [name, at, accepted_type]
      end
      return [name, at, accepted_type]
      end
    end
  end
  nil
end

.negotiate_by_extension(ext, *provided) ⇒ Symbol, ...

Negotiates the content type based on the extension and the provided groups to see if there is a match.

name of a mime group

parameter returned is the group name that matched. The second, is the Content-Type to respond to the client with, the third, the raw pancake mime type

Parameters:

  • ext (String)

    The extension to negotiate

  • provided (Symbol)

    A list of symbols each of which is the

Returns:

See Also:



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/pancake/mime_types.rb', line 232

def self.negotiate_by_extension(ext, *provided)
  provided = provided.flatten
  key = [ext, provided]
  return negotiated_accept_types[key] if negotiated_accept_types[key]

  result = nil
  provided.each do |name|
    if name == :any
      at = group(ext.to_sym).first
      return nil if at.nil?
      result = [name, at.type_strings.first, at]
      negotiated_accept_types[key] = result
      return result
    end
    group(name).each do |type|
      if type.extension == ext
        result = [name, type.type_strings.first, type]
        negotiated_accept_types[key] = result
        return result
      end
    end
  end
  result
end

.reset!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the Pancake::MimeType cache and re-creates the Pancake::MimeTypes default types and groups Good for use in specs



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pancake/mime_types.rb', line 100

def self.reset!
  @types = []
  @negotiated_accept_types = nil
  @groups = Hash.new do |h,k|
    k = k.to_s
    h[k] = []
    t = Pancake::MimeTypes.type_by_extension(k)
    h[k] << t unless t.nil?
    h[k]
  end
  reset_mime_types!
  reset_mime_groups!
end

.reset_mime_groups!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used in specs to reset the default mime groups of pancake::MimeTypes



135
136
137
138
139
140
141
142
# File 'lib/pancake/mime_types.rb', line 135

def self.reset_mime_groups!
  # html
  group_as(:html, "html", "htm", "xhtml")
  group_as(:text, "text", "txt")
  type_by_extension("xml").type_strings << "text/xml"

  group_as(:svg, "svgz")
end

.reset_mime_types!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used in specs to reset the mime types back to their corresponding originals in Rack::Mime::MIME_TYPES



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pancake/mime_types.rb', line 117

def self.reset_mime_types!
  # Setup the mime types based on the rack mime types
  Type.new("any")
  Rack::Mime::MIME_TYPES.each do |ext, type|
    ext =~ /\.(.*)$/
    e = $1
    t = type_by_extension(ext)
    if t
      t.type_strings << type
   else
      t = Type.new(e, type)
    end
  end
end

.type_by_extension(ext) ⇒ Pancake::MimeTypes::Type?

Finds a Pancake::MimeTypes::Type object by the provided extension

type object or nil if there were none found that match the provided extension

Examples:

Pancake::MimeTypes.type_by_extension("html") # => A
Pancake::MimeTypes::Type object for the .html extension

Parameters:

  • ext (String, Symbol)
    • The extension to look for

Returns:

See Also:

Author:

  • Daniel Neighman



34
35
36
37
# File 'lib/pancake/mime_types.rb', line 34

def self.type_by_extension(ext)
  ext = ext.to_s
  types.detect{|t| t.extension == ext}
end

.typesObject

A collection of all the mime types that pancake knows about

Pancake::MimeTypes::Type objects that pancake knows about. To add a new type to the collection, simply create a new Pancake::MimeTypes::Type

See Also:

Author:

  • Daniel Neighman



15
16
17
# File 'lib/pancake/mime_types.rb', line 15

def self.types
  @types || reset! && @types
end