Class: RDF::CSV::Reader
- Inherits:
-
Reader
- Object
- Reader
- RDF::CSV::Reader
- Defined in:
- lib/rdf/csv/reader.rb
Overview
A Tabular Data to RDF parser in Ruby.
Instance Attribute Summary collapse
-
#metadata ⇒ Metadata
readonly
Metadata associated with the CSV.
Class Method Summary collapse
-
.open(filename, options = {}) {|reader| ... } ⇒ Object
Open a CSV file or URI.
Instance Method Summary collapse
- #each_statement(&block) ⇒ Object
- #each_triple(&block) ⇒ Object
-
#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ Reader
constructor
Initializes the RDF::CSV Reader instance.
Constructor Details
#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ Reader
Initializes the RDF::CSV Reader instance.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rdf/csv/reader.rb', line 64 def initialize(input = $stdin, = {}, &block) [:base_uri] ||= [:base] super do [:base] ||= base_uri.to_s if base_uri # Construct metadata from that passed from file open, along with information from the file. = Metadata.new([:metadata]).table_data(base_uri, input) # Merge any user-supplied metadata # SPEC CONFUSION: Note issue described in https://github.com/w3c/csvw/issues/76#issuecomment-65914880 .merge(Metadata.new([:user_metadata])) if [:user_metadata] @doc = input.respond_to?(:read) ? input : StringIO.new(input.to_s) if block_given? case block.arity when 0 then instance_eval(&block) else block.call(self) end end end end |
Instance Attribute Details
#metadata ⇒ Metadata (readonly)
Metadata associated with the CSV
14 15 16 |
# File 'lib/rdf/csv/reader.rb', line 14 def end |
Class Method Details
.open(filename, options = {}) {|reader| ... } ⇒ Object
Open a CSV file or URI. Also attempts to load relevant metadata
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rdf/csv/reader.rb', line 25 def self.open(filename, = {}, &block) Util::File.open_file(filename, ) do |file| # load link metadata, if available = if file.respond_to?(:links) link = file.links.find_link(%w(rel describedby)) Metadata.open(link, ) end # Otherwise, look for metadata based on filename ||= Metadata.open("#{filename}-metadata.json", ) # Otherwise, look for metadata in directory ||= Metadata.open(RDF::URI(filename).join("metadata.json"), ) if # Merge options .merge!([:metadata]) if [:metadata] else # Just use options = [:metadata] end # Return an open CSV with possible block RDF::CSV::Reader.new(file, .merge(metadata: ), &block) end end |
Instance Method Details
#each_statement(&block) ⇒ Object
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 |
# File 'lib/rdf/csv/reader.rb', line 88 def each_statement(&block) if block_given? @callback = block # Output Table-Level RDF triples # SPEC FIXME: csvw:Table, not csv:Table add_triple(0, RDF::URI(.id), RDF.type, CSVW.Table) if .type? # Output other table-level metadata # SPEC AMBIGUITY(2RDF): # output all optional properties in DC space? (they're typically defined in CSVM space) # output all namespaced properties? # output all non-namespaced properties which aren't specifically defined in CSVM in DC space? # We assume to only output namesspaced-properties ..each do |prop, values| Array(value).each do |v| # Assume prop and value(s) are in RDF form? or expand here? add_triple(0, .uri, RDF::URI(prop), v) end end # SPEC CONFUSION(2RDF): # Where to output column-level, vs. cell-level metadata? .columns.each do |column| # SPEC FIXME: Output csvw:Column, if set add_triple(0, RDF::URI(column.uri), RDF.type, CSVW.Column) if column.type? column..each do |prop, values| Array(value).each do |v| # Assume prop and value(s) are in RDF form? or expand here? add_triple(0, RDF::URI(column.uri), RDF::URI(prop), v) end end end # Output Cell-Level RDF triples .rows.each do |row| # Output row-level metadata add_triple(row.rownum, RDF::URI(row.uri), CSVW.row, RDF::Literal::Integer(row.rownum)) add_triple(row.rownum, RDF::URI(row.uri), RDF.type, CSVW.Row) if row.type? row.columns.each_with_index do |column| add_triple("#{row.rownum}", RDF::URI(row.uri), RDF::URI(column.uri), column.rdf_value) end end end enum_for(:each_statement) end |
#each_triple(&block) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/rdf/csv/reader.rb', line 138 def each_triple(&block) if block_given? each_statement do |statement| block.call(*statement.to_triple) end end enum_for(:each_triple) end |