module HTTP::MimeType
MIME type encode/decode adapters
Public Class Methods
[](type)
click to toggle source
Returns adapter associated with MIME type
@param [#to_s] type @raise [Error] if no adapter found @return [Class]
# File lib/http/mime_type.rb, line 35 def [](type) adapters[normalize type] || raise(Error, "Unknown MIME type: #{type}") end
normalize(type)
click to toggle source
Resolves type by shortcut if possible
@param [#to_s] type @return [String]
# File lib/http/mime_type.rb, line 56 def normalize(type) aliases.fetch type, type.to_s end
register_adapter(type, adapter)
click to toggle source
Associate MIME type with adapter
@example
module JsonAdapter class << self def encode(obj) # encode logic here end def decode(str) # decode logic here end end end HTTP::MimeType.register_adapter 'application/json', MyJsonAdapter
@param [#to_s] type @param [#encode, decode] adapter @return [void]
# File lib/http/mime_type.rb, line 26 def register_adapter(type, adapter) adapters[type.to_s] = adapter end
register_alias(type, shortcut)
click to toggle source
Register a shortcut for MIME type
@example
HTTP::MimeType.register_alias 'application/json', :json
@param [#to_s] type @param [#to_sym] shortcut @return [void]
# File lib/http/mime_type.rb, line 48 def register_alias(type, shortcut) aliases[shortcut.to_sym] = type.to_s end