1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# frozen_string_literal: true
module HTTP
# MIME type encode/decode adapters
module MimeType
class << self
# 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]
def register_adapter(type, adapter)
adapters[type.to_s] = adapter
end
# Returns adapter associated with MIME type
#
# @param [#to_s] type
# @raise [Error] if no adapter found
# @return [Class]
def [](type)
adapters[normalize type] || raise(Error, "Unknown MIME type: #{type}")
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]
def register_alias(type, shortcut)
aliases[shortcut.to_sym] = type.to_s
end
# Resolves type by shortcut if possible
#
# @param [#to_s] type
# @return [String]
def normalize(type)
aliases.fetch type, type.to_s
end
private
# :nodoc:
def adapters
@adapters ||= {}
end
# :nodoc:
def aliases
@aliases ||= {}
end
end
end
end
# built-in mime types
require "http/mime_type/json"
|