Sha256: 61462ed1c5d55b9f3e26e174869005c6b081ee9e06a205887d6f8243f559082f

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require 'mime/types'

module Swagger
  # Class representing Media Types (commonly known as MIME Types).
  # @see http://en.wikipedia.org/wiki/Internet_media_type
  class MimeType < String
    extend Forwardable
    def_delegators :@mime_type, :media_type, :sub_type

    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'
    }.freeze

    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)
      raise ArgumentError, "Unknown mime type or suffix: #{mime_type_name}" if @mime_type.nil?
      super @mime_type_name
    end

    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
        raise NotImplementedError, "Parser support for #{mime_type} is not implemented"
      end
    end

    private

    def base_type(mime_type_name)
      media_type, _sub_type, _tree, suffix, _parameters = MIME_TYPE_FORMAT.match mime_type_name
      return MIME::Types["#{media_type}/#{suffix}"].first if media_type && suffix
      nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
swagger-core-0.3.0 lib/swagger/mime_type.rb