Class: Converter
- Inherits:
-
Object
- Object
- Converter
- Defined in:
- lib/csv2rdf/converter.rb
Overview
Abstract super-class for all converters.
Usage
Usually, a converter will be used as follows:
converter = RatingConverter.new("path/to/file.csv", "path/to/output_file.nt", context_object)
converter.convert
converter.serialize
Instance Attribute Summary collapse
-
#log ⇒ Object
The Logger object, defaults to STDOUT.
Class Method Summary collapse
-
.german_to_english_float(float) ⇒ Object
Convert the string of a German-style float (“1,59”) into an actual float.
-
.ja_nein(string) ⇒ Object
Convert a string into a boolean For string.downcase == “ja” the method will return TRUE, for all other values it will return FALSE.
-
.name2uri(name, capitalize = FALSE) ⇒ Object
Turn any string into a URI component, useful for creating URIs from names and titles.
Instance Method Summary collapse
-
#convert ⇒ Object
The actual conversion takes place in this method.
-
#initialize(csv_in, out_file, context = nil) ⇒ Converter
constructor
Attributes.
-
#serialize ⇒ Object
After calling
convert
,serialize
will write thegraph
to the desired output path (rdf_out
) as an N-Triples file (www.w3.org/TR/n-triples/).
Constructor Details
#initialize(csv_in, out_file, context = nil) ⇒ Converter
Attributes
-
csv_in
- Path to the CSV file that is going to be converted. Is tested for existence and type, might raise anIOError
. -
out_file
- Path to the desired output file. The containing folder is tested for existence, might raise anIOError
. -
context
- Optionally a context Hash, can be used to pass information to the converter. -
log
- the Logger object, defaults to STDOUT
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/csv2rdf/converter.rb', line 29 def initialize(csv_in, out_file, context=nil) unless (File.exists?(csv_in)) raise IOError, "file '#{csv_in}' does not exist" end unless (File.file?(csv_in)) raise IOError, "'#{csv_in}' is not a regular file" end unless (File.exists?(File.dirname(out_file))) raise IOError, "directory '#{File.dirname(out_file)}' does not exist" end @csv_in = csv_in @rdf_out = out_file @context = context @graph = RDF::Graph.new @log = Logger.new(STDOUT) @log.info("#{self.class}: converting #{@csv_in} to #{@rdf_out}") end |
Instance Attribute Details
#log ⇒ Object
The Logger object, defaults to STDOUT
18 19 20 |
# File 'lib/csv2rdf/converter.rb', line 18 def log @log end |
Class Method Details
.german_to_english_float(float) ⇒ Object
Convert the string of a German-style float (“1,59”) into an actual float. If passed an actual float, it will just return it.
Attributes
-
float
- the string to be converted
Examples
Converter.german_to_english_float("1,59")
=> 1.59
83 84 85 |
# File 'lib/csv2rdf/converter.rb', line 83 def Converter.german_to_english_float(float) return float.to_s.gsub(",", ".").to_f end |
.ja_nein(string) ⇒ Object
Convert a string into a boolean For string.downcase == “ja” the method will return TRUE, for all other values it will return FALSE
Attributes
-
string
- the string to be converted
Examples
Converter.ja_nein("ja")
=> true
Converter.ja_nein("Ja")
=> true
Converter.ja_nein("nein")
=> false
Converter.ja_nein("hurtz")
=> false
106 107 108 |
# File 'lib/csv2rdf/converter.rb', line 106 def Converter.ja_nein(string) return string.downcase.eql?("ja") end |
.name2uri(name, capitalize = FALSE) ⇒ Object
Turn any string into a URI component, useful for creating URIs from names and titles. The conversion is based on ActiveSupport’s parameterize
method.
Attributes
-
name
- the string to be converted -
capitalize
- if TRUE, the individual components of the name will be capitalized
Examples
Converter.name2uri("Knud Möller")
=> "knud-moller"
Converter.name2uri("Knud Möller", TRUE)
=> "Knud-Moller"
63 64 65 66 67 68 69 |
# File 'lib/csv2rdf/converter.rb', line 63 def Converter.name2uri(name, capitalize=FALSE) name = name.parameterize.downcase if capitalize name = name.split("-").map { |x| x.capitalize }.join("-") end return name end |
Instance Method Details
#convert ⇒ Object
The actual conversion takes place in this method. Implementations in sub-classes need to build the output graph on the graph
instance variable.
113 114 115 |
# File 'lib/csv2rdf/converter.rb', line 113 def convert raise "method #{self.class.name}#convert() is not implemented!" end |
#serialize ⇒ Object
After calling convert
, serialize
will write the graph
to the desired output path (rdf_out
) as an N-Triples file (www.w3.org/TR/n-triples/).
120 121 122 |
# File 'lib/csv2rdf/converter.rb', line 120 def serialize RDF::Writer.open(@rdf_out) { |writer| writer << @graph } end |