Class: Swagger::MimeType

Inherits:
String
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/swagger/mime_type.rb

Overview

Class representing Media Types (commonly known as MIME Types).

Constant Summary collapse

MIME_TYPE_FORMAT =
/(\w+)\/(\w+\.)?([\w\.]+)(\+\w+)?\s*(;.*)?/
COMMON_ALIASES =
{
  txt:    'text/plain',
  text:   'text/plain',
  json:   'application/json',
  xml:    'application/xml',
  binary: 'application/octet-stream'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mime_type_name) ⇒ MimeType



20
21
22
23
24
25
# File 'lib/swagger/mime_type.rb', line 20

def initialize(mime_type_name)
  @mime_type_name = mime_type_name.to_s
  @mime_type = MIME::Types[@mime_type_name].first || base_type(@mime_type_name)
  fail ArgumentError, "Unknown mime type or suffix: #{mime_type_name}" if @mime_type.nil?
  super @mime_type_name
end

Class Method Details

.parser_for(mime_type) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/swagger/mime_type.rb', line 27

def self.parser_for(mime_type)
  mime_type = COMMON_ALIASES[mime_type] if COMMON_ALIASES.key? mime_type
  case mime_type
  when 'application/json'
    return JSON
  else
    fail NotImplementedError, "Parser support for #{mime_type} is not implemented"
  end
end