Class: YAML_LD::API
- Inherits:
-
JSON::LD::API
- Object
- JSON::LD::API
- YAML_LD::API
- Defined in:
- lib/yaml_ld/api.rb
Overview
A YAML-LD processor based on JSON-LD.
Constant Summary collapse
- LINK_REL_CONTEXT =
The following constants are used to reduce object allocations
%w(rel http://www.w3.org/ns/yaml-ld#context).freeze
- LINK_REL_ALTERNATE =
%w(rel alternate).freeze
- LINK_TYPE_JSONLD =
%w(type application/ld+yaml).freeze
Class Method Summary collapse
-
.compact(input, context, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld| ... } ⇒ String
Compacts the given input according to the steps in the Compaction Algorithm.
-
.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **options) {|remote_document| ... } ⇒ Object
Default document loader for YAML_LD.
-
.expand(input, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld, RDF::URI| ... } ⇒ String
Expands the given input according to the steps in the Expansion Algorithm.
-
.flatten(input, context, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld| ... } ⇒ Object, Hash
This algorithm flattens an expanded YAML-LD document by collecting all properties of a node in a single object and labeling all blank nodes with blank node identifiers.
-
.frame(input, frame, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld| ... } ⇒ Object, Hash
Frames the given input using the frame according to the steps in the Framing Algorithm.
-
.fromRdf(input, useRdfType: false, useNativeTypes: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|jsonld| ... } ⇒ Object, Hash
Take an ordered list of RDF::Statements and turn them into a JSON-LD document.
-
.serializer(object, *args, **options) ⇒ Object
The default serializer for serialzing Ruby Objects to JSON.
-
.toRdf(input, expanded: false, documentLoader: self.method(:documentLoader), **options) {|statement| ... } ⇒ RDF::Enumerable
Processes the input according to the RDF Conversion Algorithm, calling the provided callback for each triple generated.
Class Method Details
.compact(input, context, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld| ... } ⇒ String
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 either returned or yielded, if a block is given.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/yaml_ld/api.rb', line 80 def self.compact(input, context, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **, &block) JSON::LD::API.compact(input, context, expanded: , documentLoader: documentLoader, serializer: serializer, **, &block) end |
.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **options) {|remote_document| ... } ⇒ Object
Default document loader for YAML_LD.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/yaml_ld/api.rb', line 239 def self.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **, &block) if url.respond_to?(:read) base_uri = [:base] base_uri ||= url.base_uri if url.respond_to?(:base_uri) content_type = [:content_type] content_type ||= url.content_type if url.respond_to?(:content_type) context_url = if url.respond_to?(:links) && url.links && # Any JSON type other than ld+json (content_type == 'application/json' || content_type.match?(%r(application/(^ld)+json))) link = url.links.find_link(JSON::LD::API::LINK_REL_CONTEXT) link.href if link elsif url.respond_to?(:links) && url.links && # Any YAML type content_type.match?(%r(application/(\w+\+)*yaml)) link = url.links.find_link(LINK_REL_CONTEXT) link.href if link end content = case content_type when nil, %r(application/(\w+\+)*yaml) # Parse YAML Psych.safe_load(url.read, aliases: true) else url.read end block.call(RemoteDocument.new(content, documentUrl: base_uri, contentType: content_type, contextUrl: context_url)) elsif url.to_s.match?(/\.yaml\w*$/) || content_type.to_s.match?(%r(application/(\w+\+)*yaml)) # Parse YAML block.call(RemoteDocument.new(Psych.load_file(url.to_s, aliases: true), documentUrl: base_uri, contentType: content_type, contextUrl: context_url)) else RDF::Util::File.open_file(url, **, &block) end end |
.expand(input, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld, RDF::URI| ... } ⇒ String
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
either returned or yielded
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/yaml_ld/api.rb', line 43 def self.(input, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **, &block) JSON::LD::API.(input, documentLoader: documentLoader, serializer: serializer, **, &block) end |
.flatten(input, context, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld| ... } ⇒ Object, Hash
This algorithm flattens an expanded YAML-LD document by collecting all properties of a node in a single object and labeling all blank nodes with blank node identifiers. This resulting uniform shape of the document, may drastically simplify the code required to process YAML-LD data in certain applications.
The resulting Array
is either returned, or yielded if a block is given.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/yaml_ld/api.rb', line 116 def self.flatten(input, context, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **, &block) JSON::LD::API.flatten(input, context, expanded: , documentLoader: documentLoader, serializer: serializer, **, &block) end |
.frame(input, frame, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld| ... } ⇒ Object, 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 either returned, or yielded if a block is given.
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/yaml_ld/api.rb', line 157 def self.frame(input, frame, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **, &block) JSON::LD::API.frame(input, frame, expanded: , documentLoader: documentLoader, serializer: serializer, **, &block) end |
.fromRdf(input, useRdfType: false, useNativeTypes: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|jsonld| ... } ⇒ Object, Hash
Take an ordered list of RDF::Statements and turn them into a JSON-LD document.
The resulting Array
is either returned or yielded, if a block is given.
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/yaml_ld/api.rb', line 212 def self.fromRdf(input, useRdfType: false, useNativeTypes: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **, &block) JSON::LD::API.fromRdf(input, useRdfType: useRdfType, useNativeTypes: useNativeTypes, documentLoader: documentLoader, serializer: serializer, **, &block) end |
.serializer(object, *args, **options) ⇒ Object
The default serializer for serialzing Ruby Objects to JSON.
Defaults to MultiJson.dump
289 290 291 292 |
# File 'lib/yaml_ld/api.rb', line 289 def self.serializer(object, *args, **) # de-alias any objects to avoid the use of aliases and anchors "%YAML 1.2\n" + Psych.dump(object, **) end |
.toRdf(input, expanded: false, documentLoader: self.method(:documentLoader), **options) {|statement| ... } ⇒ RDF::Enumerable
Processes the input according to the RDF Conversion Algorithm, calling the provided callback for each triple generated.
182 183 184 185 186 187 188 189 190 |
# File 'lib/yaml_ld/api.rb', line 182 def self.toRdf(input, expanded: false, documentLoader: self.method(:documentLoader), **, &block) JSON::LD::API.toRdf(input, expanded: , documentLoader: documentLoader, **, &block) end |