How To Get A List Of All Currently Registered MIME Types With Rails

Objective

To learn how to find answers to your own questions by browsing the Rails source code.

Steps

Step 1

Go to the Rails repository.

Step 2

Search for Mime in the This Repository search field. Or browse to https://github.com/rails/rails/search?utf8=%E2%9C%93&q=Mime.

Step 3

The second search result is actionpack/lib/action_dispatch/http/mime_types.rb. You can see all the registered Mime types in this file.

# Build list of Mime types for HTTP responses
# http://www.iana.org/assignments/media-types/

Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml )
Mime::Type.register "text/plain", :text, [], %w(txt)
Mime::Type.register "text/javascript", :js, %w( application/javascript application/x-javascript )
Mime::Type.register "text/css", :css
Mime::Type.register "text/calendar", :ics
Mime::Type.register "text/csv", :csv
Mime::Type.register "text/vcard", :vcf

Mime::Type.register "image/png", :png, [], %w(png)
Mime::Type.register "image/jpeg", :jpeg, [], %w(jpg jpeg jpe pjpeg)
Mime::Type.register "image/gif", :gif, [], %w(gif)
Mime::Type.register "image/bmp", :bmp, [], %w(bmp)
Mime::Type.register "image/tiff", :tiff, [], %w(tif tiff)
Mime::Type.register "image/svg+xml", :svg

Mime::Type.register "video/mpeg", :mpeg, [], %w(mpg mpeg mpe)

Mime::Type.register "application/xml", :xml, %w( text/xml application/x-xml )
Mime::Type.register "application/rss+xml", :rss
Mime::Type.register "application/atom+xml", :atom
Mime::Type.register "application/x-yaml", :yaml, %w( text/yaml )

Mime::Type.register "multipart/form-data", :multipart_form
Mime::Type.register "application/x-www-form-urlencoded", :url_encoded_form

# http://www.ietf.org/rfc/rfc4627.txt
# http://www.json.org/JSONRequest.html
Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest )

Mime::Type.register "application/pdf", :pdf, [], %w(pdf)
Mime::Type.register "application/zip", :zip, [], %w(zip)
Mime::Type.register "application/gzip", :gzip, %w(application/x-gzip), %w(gz)

Step 4

The first result is actionpack/test/dispatch/mime_type_test.rb. It uses Mime::LOOKUP. We can experiment in the rails console.

Mime::LOOKUP
 => {"text/html"=>#<Mime::Type:0x007fc4b3477500 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html", @hash=-1486337550136291455>, "application/xhtml+xml"=>#<Mime::Type:0x007fc4b3477500 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html", @hash=-1486337550136291455>, "text/plain"=>#<Mime::Type:0x007fc4b3476da8 @synonyms=[], @symbol=:text, @string="text/plain", @hash=1399664015805064671>, "text/javascript"=>#<Mime::Type:0x007fc4b3476948 ... } 
 > Mime::LOOKUP.keys
 => ["text/html", "application/xhtml+xml", "text/plain", "text/javascript", "application/javascript", "application/x-javascript", "text/css", "text/calendar", "text/csv", "text/vcard", "image/png", "image/jpeg", "image/gif", "image/bmp", "image/tiff", "image/svg+xml", "video/mpeg", "application/xml", "text/xml", "application/x-xml", "application/rss+xml", "application/atom+xml", "application/x-yaml", "text/yaml", "multipart/form-data", "application/x-www-form-urlencoded", "application/json", "text/x-json", "application/jsonrequest", "application/pdf", "application/zip", "application/gzip", "application/x-gzip", "application/vnd.web-console.v2"] 

We also see that the test does a lookup. We can do the same in the rails console.

 Mime::Type.lookup('text/html')
 => #<Mime::Type:0x007feb45bfc2b8 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html", @hash=-2916850394213078872> 

Summary

In this article, you learned how to find answer to your questions by browsing the Rails source code.


Related Articles