Module: RDF2JSON
- Defined in:
- lib/rdf2json/rdf2json.rb
Overview
Module that contains a class for transforming RDF N-Triples/N-Quads into JSON/JSON-LD as well as a command line interface implementation for user interactions.
Defined Under Namespace
Classes: Converter
Class Method Summary collapse
-
.cli ⇒ Object
Command line interface; reads parameters, outputs help, or proceeds with the transformation of RDF N-Triples/N-Quads into JSON/JSON-LD.
Class Method Details
.cli ⇒ Object
Command line interface; reads parameters, outputs help, or proceeds with the transformation of RDF N-Triples/N-Quads into JSON/JSON-LD.
15 16 17 18 19 20 21 22 23 24 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 |
# File 'lib/rdf2json/rdf2json.rb', line 15 def self.cli = {} parser = OptionParser.new { |opts| opts. = 'Usage: rdf2json [options] --input filename.nt --output filename.json' opts.separator '' opts.separator 'Description: Reads RDF N-Triple/N-Quads that are sorted by subject and' opts.separator ' append a JSON/JSON-LD document per line in a designated' opts.separator ' output file.' opts.separator '' opts.separator 'Notes:' opts.separator ' Sorting on Mac OS X & Linux:' opts.separator ' sort -k 1,1 UNSORTED.EXT > SORTED.EXT' opts.separator '' opts.separator ' More information on the --minimize parameter:' opts.separator ' https://github.com/joejimbo/rdf2json' opts.separator '' opts.separator 'Required:' opts.on('-i', '--input FILE', 'Input file for the conversion; either RDF N-Triples or N-Quads.') { |file| [:input] = file } opts.on('-o', '--output FILE', 'Output file to which JSON-LD/JSON is appended.') { |file| [:output] = file } opts.separator '' opts.separator 'Options:' opts.on('-m', '--minimize', 'Minimize JSON-LD to plain (semantically untyped) JSON.') { |minimize| [:minimize] = true } opts.on('-n', '--namespace [NAMESPACE]', 'Alternative name for JSON-LD\'s "@id" key; replaces it; turns on --minimize') { |namespace| [:minimize] = true [:namespace] = namespace } opts.on('-p', '--prefix [PREFIX]', 'Prefix that should be removed from keys; requires --minimize.') { |prefix| [:prefix] = prefix } opts.on('-t', '--triples', 'Input file is in RDF N-Triples format.') { |triples| [:ntriples] = true } opts.on('-q', '--quads', 'Input file is in RDF N-Quads format.') { |quads| [:nquads] = true } opts.separator '' opts.separator 'Common options:' opts.on_tail('-h', '--help', 'Show this message.') { |help| puts opts exit } } begin parser.parse! rescue puts parser exit 1 end unless .has_key?(:input) and .has_key?(:output) then puts 'Error: Requires --input and --output parameters.' puts '' puts parser exit 2 end if .has_key?(:ntriples) and .has_key?(:nquads) then puts 'Error: both --triples and --quads parameters were used.' puts ' Only one of the parameters may be provided for explicitly' puts ' setting the input fileformat.' puts '' puts parser exit 3 end extension = File.extname([:input]) if .has_key?(:ntriples) then input_format = :ntriples elsif .has_key?(:nquads) then input_format = :nquads elsif extension == '.nt' then input_format = :ntriples elsif extension == '.nq' then input_format = :nquads else puts 'Error: Cannot determine input file format by filename extension.' puts ' Recognized fileformat extensions are .nt and .nq for N-Triples' puts ' and N-Quads respectively. Use --triples or --quads options to' puts ' explicitly set the input fileformat (ignores filename extension' puts ' when one of those options is given.' puts '' puts parser exit 4 end output_format = :jsonld output_format = :json if [:minimize] unless File.exist?([:input]) then puts 'Error: Input file (--input parameter) does not seem to exist.' puts '' puts parser exit 6 end begin # Why instantiate a Converter instance here? Well, for implementing parallelization later: Converter.new([:input], [:output], input_format, output_format, [:namespace], [:prefix]).convert rescue Interrupt # The user hit Ctrl-C, which is okay and does not need error reporting. exit 0 end end |