lib/json/ld/api.rb in json-ld-1.1.0 vs lib/json/ld/api.rb in json-ld-1.1.1

- old
+ new

@@ -78,11 +78,10 @@ @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 - content_type = 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) JSON.parse(input.read) @@ -217,12 +216,11 @@ # Initialize input using frame as context API.new(expanded_input, context, options) do debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE)}"} # Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object. - node_map = Hash.ordered - node_map['@default'] = Hash.ordered + node_map = {'@default' => {}} self.generate_node_map(value, node_map) default_graph = node_map['@default'] node_map.keys.kw_sort.reject {|k| k == '@default'}.each do |graph_name| graph = node_map[graph_name] @@ -279,11 +277,10 @@ # The framed JSON-LD document # @raise [InvalidFrame] # @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm def self.frame(input, frame, options = {}) result = nil - match_limit = 0 framing_state = { :embed => true, :explicit => false, :omitDefault => false, :embeds => nil, @@ -317,11 +314,11 @@ debug(".frame") {"raw frame: #{frame.to_json(JSON_STATE)}"} debug(".frame") {"expanded frame: #{expanded_frame.to_json(JSON_STATE)}"} debug(".frame") {"expanded input: #{value.to_json(JSON_STATE)}"} # Get framing nodes from expanded input, replacing Blank Node identifiers as necessary - all_nodes = Hash.ordered + all_nodes = {} old_dbg, @options[:debug] = @options[:debug], nil depth do generate_node_map(value, all_nodes) end @options[:debug] = old_dbg @@ -358,28 +355,32 @@ # See options in {JSON::LD::API#initialize} # Options passed to {JSON::LD::API.expand} # @option options [Boolean] :produceGeneralizedRdf (false) # If true, output will include statements having blank node predicates, otherwise they are dropped. # @raise [JsonLdError] - # @return [Array<RDF::Statement>] if no block given # @yield statement # @yieldparam [RDF::Statement] statement def self.toRdf(input, options = {}, &block) - results = [] - results.extend(RDF::Enumerable) + unless block_given? + results = [] + results.extend(RDF::Enumerable) + self.toRdf(input, options) do |stmt| + results << stmt + end + return results + end # Expand input to simplify processing - expanded_input = API.expand(input, options) + expanded_input = API.expand(input, options.merge(:ordered => false)) API.new(expanded_input, nil, options) do # 1) Perform the Expansion Algorithm on the JSON-LD input. # This removes any existing context to allow the given context to be cleanly applied. debug(".toRdf") {"expanded input: #{expanded_input.to_json(JSON_STATE)}"} # Generate _nodeMap_ - node_map = Hash.ordered - node_map['@default'] = Hash.ordered + node_map = {'@default' => {}} generate_node_map(expanded_input, node_map) debug(".toRdf") {"node map: #{node_map.to_json(JSON_STATE)}"} # Start generating statements node_map.each do |graph_name, graph| @@ -388,10 +389,10 @@ # Drop results for graphs which are named with relative IRIs if graph_name.is_a?(RDF::URI) && !graph_name.absolute debug(".toRdf") {"drop relative graph_name: #{statement.to_ntriples}"} next end - graph_to_rdf(graph).each do |statement| + graph_to_rdf(graph) do |statement| next if statement.predicate.node? && !options[:produceGeneralizedRdf] # Drop results with relative IRIs relative = statement.to_a.any? do |r| case r when RDF::URI