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
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 112 113 114 115 |
# File 'lib/json/ld/api.rb', line 81 def initialize(input, context, = {}, &block) @options = {:compactArrays => true}.merge() @options[:validate] = true if @options[:processingMode] == "json-ld-1.0" @options[:documentLoader] ||= self.class.method(:documentLoader) [:rename_bnodes] ||= true @namer = [:unique_bnodes] ? BlankNodeUniqer.new : ([: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 [: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 |