module Puppet::Network::FormatHandler

Constants

ALL_MEDIA_TYPES

Public Class Methods

create(*args, &block) click to toggle source
   # File lib/puppet/network/format_handler.rb
13 def self.create(*args, &block)
14   instance = Puppet::Network::Format.new(*args, &block)
15 
16   @formats[instance.name] = instance
17   instance
18 end
create_serialized_formats(name,options = {},&block) click to toggle source
   # File lib/puppet/network/format_handler.rb
20 def self.create_serialized_formats(name,options = {},&block)
21   ["application/x-#{name}", "application/#{name}", "text/x-#{name}", "text/#{name}"].each { |mime_type|
22     create name, {:mime => mime_type}.update(options), &block
23   }
24 end
format(name) click to toggle source
   # File lib/puppet/network/format_handler.rb
26 def self.format(name)
27   @formats[name.to_s.downcase.intern]
28 end
format_by_extension(ext) click to toggle source
   # File lib/puppet/network/format_handler.rb
35 def self.format_by_extension(ext)
36   @formats.each do |name, format|
37     return format if format.extension == ext
38   end
39   nil
40 end
format_for(name) click to toggle source
   # File lib/puppet/network/format_handler.rb
30 def self.format_for(name)
31   name = format_to_canonical_name(name)
32   format(name)
33 end
format_to_canonical_name(format) click to toggle source

Return a format name given:

* a format name
* a mime-type
* a format instance
   # File lib/puppet/network/format_handler.rb
57 def self.format_to_canonical_name(format)
58   case format
59   when Puppet::Network::Format
60     out = format
61   when %r{\w+/\w+}
62     out = mime(format)
63   else
64     out = format(format)
65   end
66 
67   if out.nil?
68     raise ArgumentError, _("No format matches the given format name or mime-type (%{format})") % {format: format}
69   end
70 
71   out.name
72 end
format_to_canonical_name_or_nil(format) click to toggle source

@api private

    # File lib/puppet/network/format_handler.rb
102 def self.format_to_canonical_name_or_nil(format)
103   format_to_canonical_name(format)
104 rescue ArgumentError
105   nil
106 end
formats() click to toggle source

Provide a list of all formats.

   # File lib/puppet/network/format_handler.rb
43 def self.formats
44   @formats.keys
45 end
mime(mimetype) click to toggle source

Return a format capable of handling the provided mime type.

   # File lib/puppet/network/format_handler.rb
48 def self.mime(mimetype)
49   mimetype = mimetype.to_s.downcase
50   @formats.values.find { |format| format.mime == mimetype }
51 end
most_suitable_formats_for(accepted, supported) click to toggle source

Determine which of the accepted formats should be used given what is supported.

@param accepted [Array<String, Symbol>] the accepted formats in a form a

that generally conforms to an HTTP Accept header. Any quality specifiers
are ignored and instead the formats are simply in strict preference order
(most preferred is first)

@param supported [Array<Symbol>] the names of the supported formats (the

most preferred format is first)

@return [Array<Puppet::Network::Format>] the most suitable formats that

are both accepted and supported

@api private

   # File lib/puppet/network/format_handler.rb
85 def self.most_suitable_formats_for(accepted, supported)
86   accepted.collect do |format|
87     format.to_s.sub(/;q=.*$/, '')
88   end.collect do |format|
89     if format == ALL_MEDIA_TYPES
90       supported.first
91     else
92       format_to_canonical_name_or_nil(format)
93     end
94   end.compact.find_all do |format|
95     supported.include?(format)
96   end.collect do |format|
97     format_for(format)
98   end
99 end