Method: JSON::LD::API#initialize

Defined in:
lib/json/ld/api.rb

#initialize(input, context, options = {}) {|api| ... } ⇒ API

Initialize the API, reading in any document and setting global options

If set to true, the JSON-LD processor will use the expanded rdf:type IRI as the property instead of ‘@type` when converting from RDF.

Parameters:

  • input (String, #read, Hash, Array)
  • context (String, #read, , Hash, Array)

    An external context to use additionally to the context embedded in input when expanding the input.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :base (Boolean)

    The Base IRI to use when expanding the document. This overrides the value of input if it is a IRI. If not specified and input is not an IRI, the base IRI defaults to the current document IRI if in a browser context, or the empty string if there is no document context.

  • :compactArrays (Boolean) — default: true

    If set to true, the JSON-LD processor replaces arrays with just one element with that element during compaction. If set to false, all arrays will remain arrays even if they have just one element.

  • :conformanceCallback (Proc)

    The purpose of this option is to instruct the processor about whether or not it should continue processing. If the value is null, the processor should ignore any key-value pair associated with any recoverable conformance issue and continue processing. More details about this feature can be found in the ConformanceCallback section.

  • :flatten (Boolean, String, RDF::URI)

    If set to a value that is not false, the JSON-LD processor must modify the output of the Compaction Algorithm or the Expansion Algorithm by coalescing all properties associated with each subject via the Flattening Algorithm. The value of ‘flatten must` be either an IRI value representing the name of the graph to flatten, or true. If the value is true, then the first graph encountered in the input document is selected and flattened.

  • :optimize (Boolean) — default: false

    If set to true, the JSON-LD processor is allowed to optimize the output of the Compaction Algorithm to produce even compacter representations. The algorithm for compaction optimization is beyond the scope of this specification and thus not defined. Consequently, different implementations MAY implement different optimization algorithms. (Presently, this is a noop).

  • :useNativeDatatypes (Boolean) — default: true
  • :useRdfType (Boolean) — default: false

    If set to true, the JSON-LD processor will try to convert datatyped literals to JSON native types instead of using the expanded object form when converting from RDF. xsd:boolean values will be converted to true or false. xsd:integer and xsd:double values will be converted to JSON numbers.

Yields:

  • (api)

Yield Parameters:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/json/ld/api.rb', line 53

def initialize(input, context, options = {}, &block)
  @options = {:compactArrays => true}.merge(options)
  @value = case input
  when Array, Hash then input.dup
  when IO, StringIO then JSON.parse(input.read)
  when String
    content = nil
    RDF::Util::File.open_file(input) {|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