module Rack::Mime
Constants
- MIME_TYPES
-
List of most common mime-types, selected various sources according to their usefulness in a webserving scope for Ruby users.
To amend this list with your local mime.types list you can use:
require 'webrick/httputils' list = WEBrick::HTTPUtils.load_mime_types('/etc/mime.types') Rack::Mime::MIME_TYPES.merge!(list)
N.B. On Ubuntu the mime.types file does not include the leading period, so users may need to modify the data before merging into the hash.
Public Class Methods
Returns true if the given value is a mime match for the given mime match specification, false otherwise.
Rack::Mime.match?('text/html', 'text/*') => true Rack::Mime.match?('text/plain', '*') => true Rack::Mime.match?('text/html', 'application/json') => false
# File lib/rack/mime.rb, line 30 def match?(value, matcher) v1, v2 = value.split('/', 2) m1, m2 = matcher.split('/', 2) (m1 == '*' || v1 == m1) && (m2.nil? || m2 == '*' || m2 == v2) end
Returns String with mime type if found, otherwise use fallback
. ext
should be filename extension in the ‘.ext’ format that
File.extname(file) returns.
fallback
may be any object
Also see the documentation for MIME_TYPES
Usage:
Rack::Mime.mime_type('.foo')
This is a shortcut for:
Rack::Mime::MIME_TYPES.fetch('.foo', 'application/octet-stream')
# File lib/rack/mime.rb, line 18 def mime_type(ext, fallback = 'application/octet-stream') MIME_TYPES.fetch(ext.to_s.downcase, fallback) end