Method: RDF::Format.each

Defined in:
lib/rdf/format.rb

.each(file_name: nil, file_extension: nil, content_type: nil, has_reader: false, has_writer: false, sample: nil, all_if_none: true, **options) {|klass| ... } ⇒ Enumerator

Enumerates known RDF serialization format classes.

Given options from for, it returns just those formats that match the specified criteria.

Examples:

finding all formats that have a writer supporting text/html

RDF::Format.each(content_type: 'text/html', has_writer: true).to_a
  #=> RDF::RDFa::Format

Parameters:

  • file_name (String, #to_s) (defaults to: nil)

    (nil)

  • file_extension (Symbol, #to_sym) (defaults to: nil)

    (nil)

  • content_type (String, #to_s) (defaults to: nil)

    (nil) Content type may include wildcard characters, which will select among matching formats. Note that content_type will be taken from a URL opened using Util::File.open_file.

  • has_reader (Boolean) (defaults to: false)

    (false) Only return a format having a reader.

  • has_writer (Boolean) (defaults to: false)

    (false) Only return a format having a writer.

  • sample (String, Proc) (defaults to: nil)

    (nil) A sample of input used for performing format detection. If we find no formats, or we find more than one, and we have a sample, we can perform format detection to find a specific format to use, in which case we pick the last one we find

  • all_if_none (Boolean) (defaults to: true)

    (true) Returns all formats if none match, otherwise no format. Note that having a sample overrides this, and will search through all formats, or all those filtered to find a sample that matches

Yields:

  • (klass)

Yield Parameters:

  • (Class)

Returns:

  • (Enumerator)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rdf/format.rb', line 79

def self.each(file_name: nil,
              file_extension: nil,
              content_type: nil,
              has_reader: false,
              has_writer: false,
              sample: nil,
              all_if_none: true,
              **options,
              &block)
  formats = case
  # Find a format based on the MIME content type:
  when content_type
    # @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
    # @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
    mime_type = content_type.to_s.split(';').first.to_s # remove any media type parameters

    # Ignore text/plain, a historical encoding for N-Triples, which is
    # problematic in format detection, as many web servers will serve
    # content by default text/plain.
    if (mime_type == 'text/plain' && sample) || mime_type == '*/*'
      # All content types
      @@subclasses
    elsif mime_type.end_with?('/*')
      # All content types that have the first part of the mime-type as a prefix
      prefix = mime_type[0..-3]
      content_types.map do |ct, fmts|
        ct.start_with?(prefix) ? fmts : []
      end.flatten.uniq
    else
      content_types[mime_type]
    end
  # Find a format based on the file name:
  when file_name
    ext = File.extname(RDF::URI(file_name).path.to_s)[1..-1].to_s
    file_extensions[ext.to_sym]
  # Find a format based on the file extension:
  when file_extension
    file_extensions[file_extension.to_sym]
  else
    all_if_none ? @@subclasses : nil
  end || (sample ? @@subclasses : []) # If we can sample, check all classes

  # Subset by available reader or writer
  formats = formats.select do |f|
    has_reader ? f.reader : (has_writer ? f.writer : true)
  end

  # If we have multiple formats and a sample, use that for format detection
  if formats.length != 1 && sample
    sample = case sample
    when Proc then sample.call.to_s
    else sample.dup.to_s
    end.dup.force_encoding(Encoding::ASCII_8BIT)
    # Given a sample, perform format detection across the appropriate formats, choosing the last that matches
    # Return last format that has a positive detection
    formats = formats.select {|f| f.detect(sample)}
  end
  formats.each(&block)
end