Class: JSON::LD::Format
- Inherits:
-
RDF::Format
- Object
- RDF::Format
- JSON::LD::Format
- Defined in:
- lib/json/ld/format.rb
Overview
JSON-LD format specification.
Constant Summary collapse
- LD_FORMATS =
Specify how to execute CLI commands for each supported format. Derived formats (e.g., YAML-LD) define their own entrypoints.
{ jsonld: { expand: lambda { |input, **| JSON::LD::API.(input, serializer: JSON::LD::API.method(:serializer), **) }, compact: lambda { |input, **| JSON::LD::API.compact(input, [:context], serializer: JSON::LD::API.method(:serializer), **) }, flatten: lambda { |input, **| JSON::LD::API.flatten(input, [:context], serializer: JSON::LD::API.method(:serializer), **) }, frame: lambda { |input, **| JSON::LD::API.frame(input, [:frame], serializer: JSON::LD::API.method(:serializer), **) } } }
Class Method Summary collapse
-
.cli_commands ⇒ Hash{Symbol => Hash}
Hash of CLI commands appropriate for this format:.
-
.cli_exec(command, files, output: $stdin, **options) ⇒ Object
Execute the body of a CLI command, generic for each different API method based on definitions on LD_FORMATS.
-
.detect(sample) ⇒ Boolean
Sample detection to see if it matches JSON-LD.
-
.name ⇒ Object
Override normal format name.
-
.to_sym ⇒ Object
Override normal symbol generation.
Class Method Details
.cli_commands ⇒ Hash{Symbol => Hash}
Hash of CLI commands appropriate for this format:
-
‘expand` => API.expand
-
‘compact` => API.compact
-
‘flatten` => API.flatten
-
‘frame` => API.frame
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/json/ld/format.rb', line 124 def self.cli_commands { expand: { description: "Expand JSON-LD or parsed RDF", parse: false, help: "expand [--context <context-file>] files ...", filter: { output_format: LD_FORMATS.keys }, # Only shows output format set lambda: lambda do |files, **| = .merge(expandContext: .delete(:context)) if .key?(:context) cli_exec(:expand, files, **) end, option_use: { context: :removed } }, compact: { description: "Compact JSON-LD or parsed RDF", parse: false, filter: { output_format: LD_FORMATS.keys }, # Only shows output format set help: "compact --context <context-file> files ...", lambda: lambda do |files, **| raise ArgumentError, "Compacting requires a context" unless [:context] cli_exec(:compact, files, **) end, options: [ RDF::CLI::Option.new( symbol: :context, datatype: RDF::URI, control: :url2, use: :required, on: ["--context CONTEXT"], description: "Context to use when compacting." ) { |arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg)) } ] }, flatten: { description: "Flatten JSON-LD or parsed RDF", parse: false, help: "flatten [--context <context-file>] files ...", filter: { output_format: LD_FORMATS.keys }, # Only shows output format set lambda: lambda do |files, **| cli_exec(:compact, files, **) end, options: [ RDF::CLI::Option.new( symbol: :context, datatype: RDF::URI, control: :url2, use: :required, on: ["--context CONTEXT"], description: "Context to use when compacting." ) { |arg| RDF::URI(arg) }, RDF::CLI::Option.new( symbol: :createAnnotations, datatype: TrueClass, default: false, control: :checkbox, on: ["--[no-]create-annotations"], description: "Unfold embedded nodes which can be represented using `@annotation`." ) ] }, frame: { description: "Frame JSON-LD or parsed RDF", parse: false, help: "frame --frame <frame-file> files ...", filter: { output_format: LD_FORMATS.keys }, # Only shows output format set lambda: lambda do |files, **| raise ArgumentError, "Framing requires a frame" unless [:frame] cli_exec(:compact, files, **) end, option_use: { context: :removed }, options: [ RDF::CLI::Option.new( symbol: :frame, datatype: RDF::URI, control: :url2, use: :required, on: ["--frame FRAME"], description: "Frame to use when serializing." ) { |arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg)) } ] } } end |
.cli_exec(command, files, output: $stdin, **options) ⇒ Object
Execute the body of a CLI command, generic for each different API method based on definitions on LD_FORMATS.
Expands the input, or transforms from an RDF format based on the ‘:format` option, and then executes the appropriate command based on `:output_format` and does appropriate output serialization.
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 |
# File 'lib/json/ld/format.rb', line 83 def self.cli_exec(command, files, output: $stdin, **) output.set_encoding(Encoding::UTF_8) if output.respond_to?(:set_encoding) && RUBY_PLATFORM == "java" [:base] ||= [:base_uri] # Parse using input format, serialize using output format in_fmt = LD_FORMATS[.fetch(:format, :jsonld)] out_fmt = LD_FORMATS[.fetch(:output_format, :jsonld)] if in_fmt # Input is a JSON-LD based source (or derived) if files.empty? # If files are empty, either use options[:evaluate] or STDIN input = [:evaluate] ? StringIO.new([:evaluate]) : $stdin input.set_encoding(.fetch(:encoding, Encoding::UTF_8)) = in_fmt[:expand].call(input, serializer: nil, **) output.puts out_fmt[command].call(, expanded: true, **) else files.each do |file| = in_fmt[:expand].call(file, serializer: nil, **) output.puts out_fmt[command].call(, expanded: true, **) end end else # Turn RDF into JSON-LD first RDF::CLI.parse(files, **) do |reader| JSON::LD::API.fromRdf(reader, serializer: nil, **) do || output.puts out_fmt[command].call(, expanded: true, **) end end end end |
.detect(sample) ⇒ Boolean
Sample detection to see if it matches JSON-LD
Use a text sample to detect the format of an input file. Sub-classes implement a matcher sufficient to detect probably format matches, including disambiguating between other similar formats.
43 44 45 46 47 |
# File 'lib/json/ld/format.rb', line 43 def self.detect(sample) !!sample.match(/\{\s*"@(id|context|type)"/m) && # Exclude CSVW metadata !sample.include?("http://www.w3.org/ns/csvw") end |
.name ⇒ Object
Override normal format name
218 219 220 |
# File 'lib/json/ld/format.rb', line 218 def self.name "JSON-LD" end |
.to_sym ⇒ Object
Override normal symbol generation
212 213 214 |
# File 'lib/json/ld/format.rb', line 212 def self.to_sym :jsonld end |