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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/json/ld/api.rb', line 90 def initialize(input, context, = {}, &block) @options = {compactArrays: true, rename_bnodes: true}.merge!() @options[:validate] = true if @options[:processingMode] == "json-ld-1.0" @options[:documentLoader] ||= self.class.method(:documentLoader) @namer = [:unique_bnodes] ? BlankNodeUniqer.new : (@options[:rename_bnodes] ? BlankNodeNamer.new("b") : BlankNodeMapper.new) # For context via Link header context_ref = nil @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) # if input impelements #links, attempt to get a contextUrl from that link content_type = input.respond_to?(:content_type) ? input.content_type : "application/json" context_ref = if content_type.start_with?('application/json') && input.respond_to?(:links) link = input.links.find_link(%w(rel http://www.w3.org/ns/json-ld#context)) link.href if link end validate_input(input) if [:validate] MultiJson.load(input.read, ) when String remote_doc = @options[:documentLoader].call(input, @options) @options = {base: remote_doc.documentUrl}.merge!(@options) context_ref = remote_doc.contextUrl case remote_doc.document when String validate_input(remote_doc.document) if [:validate] MultiJson.load(remote_doc.document, ) else remote_doc.document end end # Update calling context :base option, if not defined [:base] ||= @options[:base] if @options[:base] # If not provided, first use context from document, or from a Link header context ||= (@value['@context'] if @value.is_a?(Hash)) || context_ref @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 |