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
12 def self.create(*args, &block)
13   instance = Puppet::Network::Format.new(*args, &block)
14 
15   @formats[instance.name] = instance
16   instance
17 end
create_serialized_formats(name,options = {},&block) click to toggle source
   # File lib/puppet/network/format_handler.rb
19 def self.create_serialized_formats(name,options = {},&block)
20   ["application/x-#{name}", "application/#{name}", "text/x-#{name}", "text/#{name}"].each { |mime_type|
21     create name, {:mime => mime_type}.update(options), &block
22   }
23 end
format(name) click to toggle source
   # File lib/puppet/network/format_handler.rb
25 def self.format(name)
26   @formats[name.to_s.downcase.intern]
27 end
format_by_extension(ext) click to toggle source
   # File lib/puppet/network/format_handler.rb
34 def self.format_by_extension(ext)
35   @formats.each do |name, format|
36     return format if format.extension == ext
37   end
38   nil
39 end
format_for(name) click to toggle source
   # File lib/puppet/network/format_handler.rb
29 def self.format_for(name)
30   name = format_to_canonical_name(name)
31   format(name)
32 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
56 def self.format_to_canonical_name(format)
57   case format
58   when Puppet::Network::Format
59     out = format
60   when %r{\w+/\w+}
61     out = mime(format)
62   else
63     out = format(format)
64   end
65 
66   if out.nil?
67     raise ArgumentError, _("No format matches the given format name or mime-type (%{format})") % {format: format}
68   end
69 
70   out.name
71 end
format_to_canonical_name_or_nil(format) click to toggle source

@api private

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

Provide a list of all formats.

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

Return a format capable of handling the provided mime type.

   # File lib/puppet/network/format_handler.rb
47 def self.mime(mimetype)
48   mimetype = mimetype.to_s.downcase
49   @formats.values.find { |format| format.mime == mimetype }
50 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
84 def self.most_suitable_formats_for(accepted, supported)
85   accepted.collect do |format|
86     format.to_s.sub(/;q=.*$/, '')
87   end.collect do |format|
88     if format == ALL_MEDIA_TYPES
89       supported.first
90     else
91       format_to_canonical_name_or_nil(format)
92     end
93   end.compact.find_all do |format|
94     supported.include?(format)
95   end.collect do |format|
96     format_for(format)
97   end
98 end