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.
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, **, &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 |