Project

multi_mime

0.01
No commit activity in last 3 years
No release in over 3 years
A generic swappable back-end for MIME type detection
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0.14
>= 0
~> 10.0
~> 0.19
>= 0
 Project Readme

MultiMime

A generic swappable back-end for MIME type detection

Lots of ruby libraries utilize MIME type detection in some form. In order to best support multiple MIME type detection libraries, multi_mime is a general-purpose, swappable, MIME type detection backend library.

eg MultiJson for MIME types.

Installation

gem 'multi_mime', '~> 1.1'
require 'multi_mime'

Features / Usage Examples

Part of utilizing various MIME type libraries is the need for a common interface. MultiMime provides just that with following methods.

MultiMime.type_for('text/html') # 'text/html'
# alias :by_type

MultiMime.type_for_extension('.html') # 'text/html'
# alias :by_extension

MultiMime.type_for_path('/usr/local/foo/bar/foo.html') # 'text/html'
# alias :by_path

MultiMime.type_for_file(File.open('foo.html', 'w')) # 'text/html'
# alias :by_file

MultiMime tries to have intelligent defaulting. That is, if you have any of the supported engines already loaded, it will utilize them before attempting to load any. When loading, libraries are ordered in the same order as Supported Mime Engines. Lets try using MultiMime with Rack::Mime loaded, then switch it to MIME::Types.

require 'rack/mime' # true

MultiMime.default_adapter # ':rack_mime'

MultiMime.adapter # MultiMime::Adapters::RackMime

MultiMime.reset_adapter # MultiMime::Adapters::RackMime

MultiMime.adapter = :mime_types # `:mime_types`

require 'mime/types' # false (eg loaded)

MultiMime.adapter # MultiMime::Adapters::MimeTypes

MultiMime.type_for_extension('.json') # 'application/json'

When MultiMime fails to load the specified adapter, it'll throw MultiMime::AdapterError which inherits from ArgumentError.

Badges

Gem Version Build Status Code Quality Code Coverage Gittip

Supported MIME Engines

Supported Ruby Versions

This library aims to support and is tested against the following Ruby implementations:

  • Ruby 2.3.0
  • Ruby 2.2.0
  • Ruby 2.1.0
  • Ruby 2.0.0
  • Ruby 1.9.3
  • JRuby
  • Rubinius

Credits

Inspiration:

Cribbed: