MIME type encode/decode adapters
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 36 def [](type) adapters[normalize type] || raise(Error, "Unknown MIME type: #{type}") end
Resolves type by shortcut if possible
@param [#to_s] type @return [String]
# File lib/http/mime_type.rb, line 57 def normalize(type) aliases.fetch type, type.to_s end
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 27 def register_adapter(type, adapter) adapters[type.to_s] = adapter end
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 49 def register_alias(type, shortcut) aliases[shortcut.to_sym] = type.to_s end