Sha256: bca3459dc3c4d39e3c24f8f2529ece8e0fbd0f441cd79c3369ddf76ec312cc07

Contents?: true

Size: 1.48 KB

Versions: 32

Compression:

Stored size: 1.48 KB

Contents

require 'restfully/media_type/abstract_media_type'

module Restfully
  module MediaType
    class << self
      def catalog
        @catalog ||= Set.new
      end

      def find(*types)
        return nil if types.compact.empty?

        found = {}

        catalog.each{ |media_type|
          match = media_type.supports?(*types)
          found[match] = media_type unless match.nil?
        }
        
        if found.empty?
          nil
        else
          found.sort{|a, b| a[0].length <=> b[0].length }.last[1]
        end
      end


      def unregister(media_type)
        catalog.delete(media_type)
        build_index
        self
      end

      def register(media_type)
        if media_type.signature.empty?
          raise ArgumentError, "The given MediaType (#{media_type}) has no signature"
        end
        if media_type.parser.nil?
          raise ArgumentError, "The given MediaType (#{media_type}) has no parser"
        end
        unregister(media_type).catalog.add(media_type)
      end

      # Reset the catalog to the default list of media types
      def reset
        %w{
          wildcard
          application_json
          application_x_www_form_urlencoded
          grid5000
        }.each do |m|
          require "restfully/media_type/#{m}"
          register MediaType.const_get(m.camelize)
        end
      end

      def build_index
        # @index = []
        # catalog.each do |media_type|
        #
        # end
      end


    end

    reset

  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
restfully-1.3.0 lib/restfully/media_type.rb
restfully-1.2.0 lib/restfully/media_type.rb
restfully-1.1.1 lib/restfully/media_type.rb
restfully-1.1.0 lib/restfully/media_type.rb
restfully-1.0.8 lib/restfully/media_type.rb
restfully-1.0.7 lib/restfully/media_type.rb
restfully-1.0.6 lib/restfully/media_type.rb
restfully-1.0.5 lib/restfully/media_type.rb
restfully-1.0.4 lib/restfully/media_type.rb
restfully-1.0.3 lib/restfully/media_type.rb
restfully-1.0.2 lib/restfully/media_type.rb
restfully-1.0.1 lib/restfully/media_type.rb
restfully-1.0.0 lib/restfully/media_type.rb
restfully-1.0.0.rc2 lib/restfully/media_type.rb
restfully-1.0.0.rc1 lib/restfully/media_type.rb
restfully-0.8.8 lib/restfully/media_type.rb
restfully-0.8.7 lib/restfully/media_type.rb
restfully-0.8.6 lib/restfully/media_type.rb
restfully-0.8.5 lib/restfully/media_type.rb
restfully-0.8.4 lib/restfully/media_type.rb