Class: RDF::CSV::Reader

Inherits:
Reader
  • Object
show all
Defined in:
lib/rdf/csv/reader.rb

Overview

A Tabular Data to RDF parser in Ruby.

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ Reader

Initializes the RDF::CSV Reader instance.

Options Hash (options):

  • :metadata (Metadata, Hash)

    extracted when file opened

  • :user_metadata (Metadata, Hash)

    user supplied metadata, merged on top of extracted metadata

Yields:

  • (reader)

    ‘self`

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored

Raises:

  • (RDF::ReaderError)

    if the CSV document cannot be loaded



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, options = {}, &block)
  options[:base_uri] ||= options[:base]
  super do
    @options[:base] ||= base_uri.to_s if base_uri
    # Construct metadata from that passed from file open, along with information from the file.
     = .new(options[: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(.new(options[:user_metadata])) if options[: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

#metadataMetadata (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

Yields:

  • (reader)

Yield Parameters:

Yield Returns:

  • (void)

    ignored



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, options = {}, &block)
  Util::File.open_file(filename, options) do |file|
    # load link metadata, if available
     = if file.respond_to?(:links)
      link = file.links.find_link(%w(rel describedby))
      .open(link, options)
    end

    # Otherwise, look for metadata based on filename
     ||= .open("#{filename}-metadata.json", options)

    # Otherwise, look for metadata in directory
     ||= .open(RDF::URI(filename).join("metadata.json"), options)

    if 
      # Merge options
      .merge!(options[:metadata]) if options[:metadata]
    else
      # Just use options
       = options[:metadata]
    end

    # Return an open CSV with possible block
    RDF::CSV::Reader.new(file, options.merge(metadata: ), &block)
  end
end

Instance Method Details

#each_statement(&block) ⇒ Object

See Also:

  • Reader#each_statement


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
    .expanded_annotation_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.expanded_annotation_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, 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

See Also:

  • Reader#each_triple


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