Class: HTTP::Accept::MediaTypes::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/http/accept/media_types.rb

Overview

Map a set of mime types to objects.

Constant Summary collapse

WILDCARD =
"*/*".freeze

Instance Method Summary collapse

Constructor Details

#initializeMap

Returns a new instance of Map.



38
39
40
# File 'lib/http/accept/media_types.rb', line 38

def initialize
	@media_types = {}
end

Instance Method Details

#<<(object) ⇒ Object

Add a converter to the collection. A converter can be anything that responds to #content_type.



68
69
70
71
72
73
74
75
76
# File 'lib/http/accept/media_types.rb', line 68

def << object
	type, subtype = object.content_type.split('/')
	
	@media_types[WILDCARD] = object if @media_types.empty?
	@media_types["#{type}/*"] ||= object
	@media_types["#{type}/#{subtype}"] ||= object
	
	return self
end

#for(media_types) ⇒ Object

Given a list of content types (e.g. from browser_preferred_content_types), return the best converter.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/http/accept/media_types.rb', line 52

def for(media_types)
	media_types.each do |media_range|
		mime_type = case media_range
			when String then media_range
			else media_range.mime_type
		end
		
		if object = @media_types[mime_type]
			return object, media_range
		end
	end
	
	return nil
end

#freezeObject



42
43
44
45
46
47
48
49
# File 'lib/http/accept/media_types.rb', line 42

def freeze
	unless frozen?
		@media_types.freeze
		@media_types.each{|key,value| value.freeze}
		
		super
	end
end