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

Parameters:

  • input (String, #read, Hash, Array)
  • context (String, #read, Hash, Array, JSON::LD::Context)

    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 (String, #to_s)

    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. If not specified, and a base IRI is found from input, options will be modified with this value.

  • :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.

  • :documentLoader (Proc)

    The callback of the loader to be used to retrieve remote documents and contexts. If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor’s built-in loader must be used. See documentLoader for the method signature.

  • :expandContext (String, #read, Hash, Array, JSON::LD::Context)

    A context that is used to initialize the active context when expanding a document.

  • :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.

  • :processingMode (String) — default: "json-ld-1.0"

    If set to “json-ld-1.0”, the JSON-LD processor must produce exactly the same results as the algorithms defined in this specification. If set to another value, the JSON-LD processor is allowed to extend or modify the algorithms defined in this specification to enable application-specific optimizations. The definition of such optimizations is beyond the scope of this specification and thus not defined. Consequently, different implementations may implement different optimizations. Developers must not define modes beginning with json-ld as they are reserved for future versions of this specification.

  • :produceGeneralizedRdf (String) — default: false

    Unless the produce generalized RDF flag is set to true, RDF triples containing a blank node predicate are excluded from output.

  • :useNativeTypes (Boolean) — default: false

    If set to true, the JSON-LD processor will use native datatypes for expression xsd:integer, xsd:boolean, and xsd:double values, otherwise, it will use the expanded form.

  • :useRdfType (Boolean) — default: false

    If set to true, the JSON-LD processor will treat rdf:type like a normal property instead of using ‘@type`.

  • :rename_bnodes (Boolean) — default: true

    Rename bnodes as part of expansion, or keep them the same.

Yields:

  • (api)

Yield Parameters:



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
# File 'lib/json/ld/api.rb', line 77

def initialize(input, context, options = {}, &block)
  @options = {:compactArrays => true}.merge(options)
  @options[:validate] = true if @options[:processingMode] == "json-ld-1.0"
  @options[:documentLoader] ||= self.class.method(:documentLoader)
  options[:rename_bnodes] ||= true
  @namer = options[:rename_bnodes] ? BlankNodeNamer.new("b") : BlankNodeMapper.new
  @value = case input
  when Array, Hash then input.dup
  when IO, StringIO
    @options = {:base => input.base_uri}.merge(@options) if input.respond_to?(:base_uri)
    JSON.parse(input.read)
  when String
    remote_doc = @options[:documentLoader].call(input, @options)

    @options = {:base => remote_doc.documentUrl}.merge(@options)
    context = context ? [context, remote_doc.contextUrl].compact : remote_doc.contextUrl

    case remote_doc.document
    when String then JSON.parse(remote_doc.document)
    else remote_doc.document
    end
  end

  # Update calling context :base option, if not defined
  options[:base] ||= @options[:base] if @options[:base]
  @context = Context.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