Class: JSON::LD::API
- Inherits:
-
Object
- Object
- JSON::LD::API
- Defined in:
- lib/json/ld/api.rb
Overview
A JSON-LD processor implementing the JsonLdProcessor interface.
This API provides a clean mechanism that enables developers to convert JSON-LD data into a a variety of output formats that are easier to work with in various programming languages. If a JSON-LD API is provided in a programming environment, the entirety of the following API must be implemented.
Constant Summary collapse
- OPEN_OPTS =
Options used for open_file
{ :headers => %w(Accept: application/ld+json, application/json) }
Instance Attribute Summary collapse
-
#context ⇒ JSON::LD::EvaluationContext
Input evaluation context.
-
#input ⇒ String, ...
Current input.
-
#namer ⇒ JSON::LD::BlankNodeNamer
readonly
Current Blank Node Namer.
-
#value ⇒ String, ...
Current input.
Class Method Summary collapse
-
.compact(input, context, callback = nil, options = {}) {|jsonld| ... } ⇒ Hash
Compacts the given input according to the steps in the Compaction Algorithm.
-
.expand(input, context = nil, callback = nil, options = {}) {|jsonld| ... } ⇒ Array<Hash>
Expands the given input according to the steps in the Expansion Algorithm.
-
.flatten(input, graph, context, callback = nil, options = {}) {|jsonld| ... } ⇒ Array<Hash>
Flattens the given input according to the steps in the Flattening Algorithm.
-
.frame(input, frame, callback = nil, options = {}) {|jsonld| ... } ⇒ Array<Hash>
Frames the given input using the frame according to the steps in the Framing Algorithm.
-
.fromRDF(input, callback = nil, options = {}) {|jsonld| ... } ⇒ Array<Hash>
Take an ordered list of RDF::Statements and turn them into a JSON-LD document.
-
.toRDF(input, context = nil, callback = nil, options = {}) {|statement| ... } ⇒ Array<RDF::Statement>
Processes the input according to the RDF Conversion Algorithm, calling the provided callback for each triple generated.
Instance Method Summary collapse
-
#initialize(input, context, options = {}) {|api| ... } ⇒ API
constructor
Initialize the API, reading in any document and setting global options.
Methods included from Frame
#cleanup_preserve, #frame, #remove_dependents
Methods included from Utils
#blank_node?, #index?, #list?, #node?, #node_reference?, #value?
Methods included from FromRDF
Methods included from Flatten
Methods included from ToRDF
#add_quad, #node, #parse_list, #statements
Methods included from Compact
Methods included from Expand
Constructor Details
#initialize(input, context, options = {}) {|api| ... } ⇒ API
Initialize the API, reading in any document and setting global options
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/json/ld/api.rb', line 73 def initialize(input, context, = {}, &block) @options = {:compactArrays => true}.merge() = {:rename_bnodes => true}.merge() @namer = [:rename_bnodes] ? BlankNodeNamer.new("t") : BlankNodeMapper.new @value = case input when Array, Hash then input.dup when IO, StringIO then JSON.parse(input.read) when String content = nil @options = {:base => input}.merge(@options) RDF::Util::File.open_file(input, OPEN_OPTS) {|f| content = JSON.parse(f.read)} content end @context = EvaluationContext.new(@options) @context = @context.parse(context) if context if block_given? case block.arity when 0, -1 then instance_eval(&block) else block.call(self) end end end |
Instance Attribute Details
#context ⇒ JSON::LD::EvaluationContext
Input evaluation context
40 41 42 |
# File 'lib/json/ld/api.rb', line 40 def context @context end |
#input ⇒ String, ...
Current input
35 |
# File 'lib/json/ld/api.rb', line 35 attr_accessor :value |
#namer ⇒ JSON::LD::BlankNodeNamer (readonly)
Current Blank Node Namer
45 46 47 |
# File 'lib/json/ld/api.rb', line 45 def namer @namer end |
#value ⇒ String, ...
Current input
35 36 37 |
# File 'lib/json/ld/api.rb', line 35 def value @value end |
Class Method Details
.compact(input, context, callback = nil, options = {}) {|jsonld| ... } ⇒ Hash
Compacts the given input according to the steps in the Compaction Algorithm. The input must be copied, compacted and returned if there are no errors. If the compaction fails, an appropirate exception must be thrown.
If no context is provided, the input document is compacted using the top-level context of the document
The resulting ‘Hash` is returned via the provided callback.
Note that for Ruby, if the callback is not provided and a block is given, it will be yielded
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 |
# File 'lib/json/ld/api.rb', line 163 def self.compact(input, context, callback = nil, = {}) = result = nil # 1) Perform the Expansion Algorithm on the JSON-LD input. # This removes any existing context to allow the given context to be cleanly applied. = API.(input, nil, nil, .merge(:debug => nil)) API.new(, context, ) do debug(".compact") {"expanded input: #{.to_json(JSON_STATE)}"} result = compact(value, nil) # xxx) Add the given context to the output result = case result when Hash then self.context.serialize.merge(result) when Array kwgraph = self.context.compact_iri('@graph', :quiet => true) self.context.serialize.merge(kwgraph => result) when String kwid = self.context.compact_iri('@id', :quiet => true) self.context.serialize.merge(kwid => result) end end callback.call(result) if callback yield result if block_given? result end |
.expand(input, context = nil, callback = nil, options = {}) {|jsonld| ... } ⇒ Array<Hash>
Expands the given input according to the steps in the Expansion Algorithm. The input must be copied, expanded and returned if there are no errors. If the expansion fails, an appropriate exception must be thrown.
The resulting ‘Array` is returned via the provided callback.
Note that for Ruby, if the callback is not provided and a block is given, it will be yielded
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/json/ld/api.rb', line 120 def self.(input, context = nil, callback = nil, = {}) result = nil API.new(input, context, ) do |api| result = api.(api.value, nil, api.context) end # If, after the algorithm outlined above is run, the resulting element is an # JSON object with just a @graph property, element is set to the value of @graph's value. result = result['@graph'] if result.is_a?(Hash) && result.keys == %w(@graph) # Finally, if element is a JSON object, it is wrapped into an array. result = [result].compact unless result.is_a?(Array) callback.call(result) if callback yield result if block_given? result end |
.flatten(input, graph, context, callback = nil, options = {}) {|jsonld| ... } ⇒ Array<Hash>
Flattens the given input according to the steps in the Flattening Algorithm. The input must be flattened and returned if there are no errors. If the flattening fails, an appropriate exception must be thrown.
The resulting ‘Array` is returned via the provided callback.
Note that for Ruby, if the callback is not provided and a block is given, it will be yielded. If there is no block, the value will be returned.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/json/ld/api.rb', line 215 def self.flatten(input, graph, context, callback = nil, = {}) result = nil graph ||= '@merged' # Expand input to simplify processing = API.(input) # Initialize input using frame as context API.new(, nil, ) do debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE)}"} # Generate _nodeMap_ node_map = Hash.ordered self.generate_node_map(value, node_map, (graph.to_s == '@merged' ? '@merged' : '@default')) result = [] # If nodeMap has no property graph, return result, otherwise set definitions to its value. definitions = node_map.fetch(graph.to_s, {}) # Foreach property and value of definitions definitions.keys.sort.each do |prop| value = definitions[prop] result << value end result end callback.call(result) if callback yield result if block_given? result end |
.frame(input, frame, callback = nil, options = {}) {|jsonld| ... } ⇒ Array<Hash>
Frames the given input using the frame according to the steps in the Framing Algorithm. The input is used to build the framed output and is returned if there are no errors. If there are no matches for the frame, null must be returned. Exceptions must be thrown if there are errors.
The resulting ‘Array` is returned via the provided callback.
Note that for Ruby, if the callback is not provided and a block is given, it will be yielded
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/json/ld/api.rb', line 283 def self.frame(input, frame, callback = nil, = {}) result = nil match_limit = 0 framing_state = { :embed => true, :explicit => false, :omitDefault => false, :embeds => nil, } framing_state[:embed] = [:embed] if .has_key?(:embed) framing_state[:explicit] = [:explicit] if .has_key?(:explicit) framing_state[:omitDefault] = [:omitDefault] if .has_key?(:omitDefault) # de-reference frame to create the framing object frame = case frame when Hash then frame.dup when IO, StringIO then JSON.parse(frame.read) when String content = nil RDF::Util::File.open_file(frame, OPEN_OPTS) {|f| content = JSON.parse(f.read)} content end # Expand frame to simplify processing = API.(frame) # Expand input to simplify processing = API.(input) # Initialize input using frame as context API.new(, nil, ) do #debug(".frame") {"context from frame: #{context.inspect}"} #debug(".frame") {"expanded frame: #{expanded_frame.to_json(JSON_STATE)}"} #debug(".frame") {"expanded input: #{value.to_json(JSON_STATE)}"} # Get framing nodes from expanded input, replacing Blank Node identifiers as necessary all_nodes = Hash.ordered depth do generate_node_map(value, all_nodes, '@merged') end @node_map = all_nodes['@merged'] debug(".frame") {"node_map: #{@node_map.to_json(JSON_STATE)}"} result = [] frame(framing_state, @node_map, [0], result, nil) debug(".frame") {"after frame: #{result.to_json(JSON_STATE)}"} # Initalize context from frame @context = depth {@context.parse(frame['@context'])} # Compact result compacted = depth {compact(result, nil)} compacted = [compacted] unless compacted.is_a?(Array) # Add the given context to the output kwgraph = context.compact_iri('@graph', :quiet => true) result = context.serialize.merge({kwgraph => compacted}) debug(".frame") {"after compact: #{result.to_json(JSON_STATE)}"} result = cleanup_preserve(result) end callback.call(result) if callback yield result if block_given? result end |
.fromRDF(input, callback = nil, options = {}) {|jsonld| ... } ⇒ Array<Hash>
Take an ordered list of RDF::Statements and turn them into a JSON-LD document.
The resulting ‘Array` is returned via the provided callback.
Note that for Ruby, if the callback is not provided and a block is given, it will be yielded
404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/json/ld/api.rb', line 404 def self.fromRDF(input, callback = nil, = {}, &block) = {:useNativeTypes => true}.merge() result = nil API.new(nil, nil, ) do |api| result = api.from_statements(input) end callback ||= block if block_given? callback.call(result) if callback result end |
.toRDF(input, context = nil, callback = nil, options = {}) {|statement| ... } ⇒ Array<RDF::Statement>
Processes the input according to the RDF Conversion Algorithm, calling the provided callback for each triple generated.
Note that for Ruby, if the callback is not provided and a block is given, it will be yielded
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/json/ld/api.rb', line 366 def self.toRDF(input, context = nil, callback = nil, = {}, &block) results = [] API.new(input, context, ) do |api| # 1) Perform the Expansion Algorithm on the JSON-LD input. # This removes any existing context to allow the given context to be cleanly applied. result = api.(api.value, nil, api.context) api.send(:debug, ".expand") {"expanded input: #{result.to_json(JSON_STATE)}"} # Start generating statements api.statements("", result, nil, nil, nil) do |statement| callback ||= block if block_given? if callback callback.call(statement) else results << statement end end end results end |