Module: HTTP::MimeType

Defined in:
lib/http/mime_type.rb,
lib/http/mime_type/json.rb,
lib/http/mime_type/adapter.rb

Overview

MIME type encode/decode adapters

Defined Under Namespace

Classes: Adapter, JSON

Class Method Summary collapse

Class Method Details

.[](type) ⇒ Class

Returns adapter associated with MIME type

Parameters:

  • type (#to_s)

Returns:

  • (Class)

Raises:

  • (Error)

    if no adapter found



39
40
41
# File 'lib/http/mime_type.rb', line 39

def [](type)
  adapters[normalize type] || raise(UnsupportedMimeTypeError, "Unknown MIME type: #{type}")
end

.normalize(type) ⇒ String

Resolves type by shortcut if possible

Parameters:

  • type (#to_s)

Returns:

  • (String)


60
61
62
# File 'lib/http/mime_type.rb', line 60

def normalize(type)
  aliases.fetch type, type.to_s
end

.register_adapter(type, adapter) ⇒ void

This method returns an undefined value.

Associate MIME type with adapter

Examples:


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

Parameters:

  • type (#to_s)
  • adapter (#encode, #decode)


30
31
32
# File 'lib/http/mime_type.rb', line 30

def register_adapter(type, adapter)
  adapters[type.to_s] = adapter
end

.register_alias(type, shortcut) ⇒ void

This method returns an undefined value.

Register a shortcut for MIME type

Examples:


HTTP::MimeType.register_alias 'application/json', :json

Parameters:

  • type (#to_s)
  • shortcut (#to_sym)


52
53
54
# File 'lib/http/mime_type.rb', line 52

def register_alias(type, shortcut)
  aliases[shortcut.to_sym] = type.to_s
end