Module: Puppet::Network::FormatHandler
- Defined in:
- lib/puppet/network/format_handler.rb
Defined Under Namespace
Classes: FormatError
Constant Summary collapse
- ALL_MEDIA_TYPES =
'*/*'
Class Method Summary collapse
- .create(*args, &block) ⇒ Object
- .create_serialized_formats(name, options = {}, &block) ⇒ Object
- .format(name) ⇒ Object
- .format_by_extension(ext) ⇒ Object
- .format_for(name) ⇒ Object
-
.format_to_canonical_name(format) ⇒ Object
Return a format name given: * a format name * a mime-type * a format instance.
- .format_to_canonical_name_or_nil(format) ⇒ Object private
-
.formats ⇒ Object
Provide a list of all formats.
-
.mime(mimetype) ⇒ Object
Return a format capable of handling the provided mime type.
-
.most_suitable_formats_for(accepted, supported) ⇒ Array<Puppet::Network::Format>
private
Determine which of the accepted formats should be used given what is supported.
Class Method Details
.create(*args, &block) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/puppet/network/format_handler.rb', line 14 def self.create(*args, &block) instance = Puppet::Network::Format.new(*args, &block) @formats[instance.name] = instance instance end |
.create_serialized_formats(name, options = {}, &block) ⇒ Object
21 22 23 24 25 |
# File 'lib/puppet/network/format_handler.rb', line 21 def self.create_serialized_formats(name, = {}, &block) ["application/x-#{name}", "application/#{name}", "text/x-#{name}", "text/#{name}"].each { |mime_type| create name, { :mime => mime_type }.update(), &block } end |
.format(name) ⇒ Object
27 28 29 |
# File 'lib/puppet/network/format_handler.rb', line 27 def self.format(name) @formats[name.to_s.downcase.intern] end |
.format_by_extension(ext) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/puppet/network/format_handler.rb', line 36 def self.format_by_extension(ext) @formats.each do |_name, format| return format if format.extension == ext end nil end |
.format_for(name) ⇒ Object
31 32 33 34 |
# File 'lib/puppet/network/format_handler.rb', line 31 def self.format_for(name) name = format_to_canonical_name(name) format(name) end |
.format_to_canonical_name(format) ⇒ Object
Return a format name given:
* a format name
* a mime-type
* a format instance
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/puppet/network/format_handler.rb', line 58 def self.format_to_canonical_name(format) case format when Puppet::Network::Format out = format when %r{\w+/\w+} out = mime(format) else out = format(format) end if out.nil? raise ArgumentError, _("No format matches the given format name or mime-type (%{format})") % { format: format } end out.name end |
.format_to_canonical_name_or_nil(format) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
103 104 105 106 107 |
# File 'lib/puppet/network/format_handler.rb', line 103 def self.format_to_canonical_name_or_nil(format) format_to_canonical_name(format) rescue ArgumentError nil end |
.formats ⇒ Object
Provide a list of all formats.
44 45 46 |
# File 'lib/puppet/network/format_handler.rb', line 44 def self.formats @formats.keys end |
.mime(mimetype) ⇒ Object
Return a format capable of handling the provided mime type.
49 50 51 52 |
# File 'lib/puppet/network/format_handler.rb', line 49 def self.mime(mimetype) mimetype = mimetype.to_s.downcase @formats.values.find { |format| format.mime == mimetype } end |
.most_suitable_formats_for(accepted, supported) ⇒ Array<Puppet::Network::Format>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determine which of the accepted formats should be used given what is supported.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/puppet/network/format_handler.rb', line 86 def self.most_suitable_formats_for(accepted, supported) accepted.collect do |format| format.to_s.sub(/;q=.*$/, '') end.filter_map do |format| if format == ALL_MEDIA_TYPES supported.first else format_to_canonical_name_or_nil(format) end end.find_all do |format| supported.include?(format) end.collect do |format| format_for(format) end end |