MIME-Type Conversion
Learn how to use metaprogramming for MIME library goals of supporting text in character sets other than ASCII.
If we write a web service, we’ll probably need to validate and convert MIME types to their file extension. For example, when a request comes into the server with an Accept header of application/javascript
, we must know how to handle this MIME type and render a .js
template.
To tackle this problem in most languages, we would store the MIME data in a map and consult the keyspace for MIME-type conversions. This can become tedious with large datasets where we would need to convert the data by hand into a format representable within our programs. Fortunately, Elixir makes this easy for us with just a touch of metaprogramming. So let’s experiment with it a bit.
Making use of existing datasets
Instead of writing a bunch of code by hand, we’ll take a publicly available MIME-type dataset and generate function heads to perform conversions. Our solution will require only ten lines of code ...