json-ld 1.1.7 → 1.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,7 +36,7 @@ module JSON::LD
36
36
  return nil if element.has_key?('@null')
37
37
 
38
38
  if element.keys.any? {|k| %w(@id @value).include?(k)}
39
- result = context.compact_value(property, element, :depth => @depth)
39
+ result = context.compact_value(property, element, depth: @depth)
40
40
  unless result.is_a?(Hash)
41
41
  debug("") {"=> scalar result: #{result.inspect}"}
42
42
  return result
@@ -52,11 +52,11 @@ module JSON::LD
52
52
 
53
53
  if %w(@id @type).include?(expanded_property)
54
54
  compacted_value = [expanded_value].flatten.compact.map do |expanded_type|
55
- depth {context.compact_iri(expanded_type, :vocab => (expanded_property == '@type'), :depth => @depth)}
55
+ depth {context.compact_iri(expanded_type, vocab: (expanded_property == '@type'), depth: @depth)}
56
56
  end
57
57
  compacted_value = compacted_value.first if compacted_value.length == 1
58
58
 
59
- al = context.compact_iri(expanded_property, :vocab => true, :quiet => true)
59
+ al = context.compact_iri(expanded_property, vocab: true, quiet: true)
60
60
  debug(expanded_property) {"result[#{al}] = #{compacted_value.inspect}"}
61
61
  result[al] = compacted_value
62
62
  next
@@ -76,7 +76,7 @@ module JSON::LD
76
76
  end
77
77
 
78
78
  unless compacted_value.empty?
79
- al = context.compact_iri('@reverse', :quiet => true)
79
+ al = context.compact_iri('@reverse', quiet: true)
80
80
  debug("") {"remainder: #{al} => #{compacted_value.inspect}"}
81
81
  result[al] = compacted_value
82
82
  end
@@ -90,7 +90,7 @@ module JSON::LD
90
90
 
91
91
  # Otherwise, if expanded property is @index, @value, or @language:
92
92
  if %w(@index @value @language).include?(expanded_property)
93
- al = context.compact_iri(expanded_property, :vocab => true, :quiet => true)
93
+ al = context.compact_iri(expanded_property, vocab: true, quiet: true)
94
94
  debug(expanded_property) {"#{al} => #{expanded_value.inspect}"}
95
95
  result[al] = expanded_value
96
96
  next
@@ -99,10 +99,10 @@ module JSON::LD
99
99
  if expanded_value == []
100
100
  item_active_property = depth do
101
101
  context.compact_iri(expanded_property,
102
- :value => expanded_value,
103
- :vocab => true,
104
- :reverse => inside_reverse,
105
- :depth => @depth)
102
+ value: expanded_value,
103
+ vocab: true,
104
+ reverse: inside_reverse,
105
+ depth: @depth)
106
106
  end
107
107
 
108
108
  iap = result[item_active_property] ||= []
@@ -113,10 +113,10 @@ module JSON::LD
113
113
  expanded_value.each do |expanded_item|
114
114
  item_active_property = depth do
115
115
  context.compact_iri(expanded_property,
116
- :value => expanded_item,
117
- :vocab => true,
118
- :reverse => inside_reverse,
119
- :depth => @depth)
116
+ value: expanded_item,
117
+ vocab: true,
118
+ reverse: inside_reverse,
119
+ depth: @depth)
120
120
  end
121
121
  container = context.container(item_active_property)
122
122
  value = list?(expanded_item) ? expanded_item['@list'] : expanded_item
@@ -126,10 +126,10 @@ module JSON::LD
126
126
  if list?(expanded_item)
127
127
  compacted_item = [compacted_item] unless compacted_item.is_a?(Array)
128
128
  unless container == '@list'
129
- al = context.compact_iri('@list', :vocab => true, :quiet => true)
129
+ al = context.compact_iri('@list', vocab: true, quiet: true)
130
130
  compacted_item = {al => compacted_item}
131
131
  if expanded_item.has_key?('@index')
132
- key = context.compact_iri('@index', :vocab => true, :quiet => true)
132
+ key = context.compact_iri('@index', vocab: true, quiet: true)
133
133
  compacted_item[key] = expanded_item['@index']
134
134
  end
135
135
  else
@@ -1,4 +1,3 @@
1
- require 'open-uri'
2
1
  require 'json'
3
2
  require 'bigdecimal'
4
3
 
@@ -107,7 +106,6 @@ module JSON::LD
107
106
  attr_accessor :context_base
108
107
 
109
108
  # Term definitions
110
- # @!attribute [r] term_definitions
111
109
  # @return [Hash{String => TermDefinition}]
112
110
  attr_reader :term_definitions
113
111
 
@@ -118,7 +116,6 @@ module JSON::LD
118
116
  #
119
117
  #
120
118
  # This adds a language to plain strings that aren't otherwise coerced
121
- # @!attribute [rw] default_language
122
119
  # @return [String]
123
120
  attr_reader :default_language
124
121
 
@@ -135,7 +132,6 @@ module JSON::LD
135
132
  # @return [Context] A context provided to us that we can use without re-serializing XXX
136
133
  attr_accessor :provided_context
137
134
 
138
- # @!attribute [r] namer
139
135
  # @return [BlankNodeNamer]
140
136
  attr_accessor :namer
141
137
 
@@ -206,7 +202,7 @@ module JSON::LD
206
202
  @base.canonicalize!
207
203
  @base.fragment = nil
208
204
  @base.query = nil
209
- raise JsonLdError::InvalidBaseIRI, "@base must be an absolute IRI: #{value.inspect}" unless @base.absolute?
205
+ raise JsonLdError::InvalidBaseIRI, "@base must be an absolute IRI: #{value.inspect}" unless @base.absolute? || !@options[:validate]
210
206
  @base
211
207
  else
212
208
  @base = nil
@@ -231,7 +227,7 @@ module JSON::LD
231
227
  value
232
228
  when String
233
229
  v = as_resource(value)
234
- raise JsonLdError::InvalidVocabMapping, "@value must be an absolute IRI: #{value.inspect}" if v.uri? && v.relative?
230
+ raise JsonLdError::InvalidVocabMapping, "@value must be an absolute IRI: #{value.inspect}" if v.uri? && v.relative? && @options[:validate]
235
231
  v
236
232
  when nil
237
233
  nil
@@ -296,7 +292,9 @@ module JSON::LD
296
292
  context_no_base.context_base = context.to_s
297
293
 
298
294
  begin
299
- @options[:documentLoader].call(context.to_s) do |remote_doc|
295
+ context_opts = @options.dup
296
+ context_opts.delete(:headers)
297
+ @options[:documentLoader].call(context.to_s, context_opts) do |remote_doc|
300
298
  # 3.2.5) Dereference context. If the dereferenced document has no top-level JSON object with an @context member, an invalid remote context has been detected and processing is aborted; otherwise, set context to the value of that member.
301
299
  jo = case remote_doc.document
302
300
  when String then JSON.parse(remote_doc.document)
@@ -353,6 +351,33 @@ module JSON::LD
353
351
  result
354
352
  end
355
353
 
354
+ ##
355
+ # Merge in a context, creating a new context with updates from `context`
356
+ #
357
+ # @param [Context] context
358
+ # @return [Context]
359
+ def merge(context)
360
+ c = self.dup.merge!(context)
361
+ c.instance_variable_set(:@term_definitions, context.term_definitions.dup)
362
+ c
363
+ end
364
+
365
+ ##
366
+ # Update context with definitions from `context`
367
+ #
368
+ # @param [Context] context
369
+ # @return [self]
370
+ def merge!(context)
371
+ # FIXME: if new context removes the default language, this won't do anything
372
+ self.default_language = context.default_language if context.default_language
373
+ self.vocab = context.vocab if context.vocab
374
+ self.base = context.base if context.base
375
+
376
+ # Merge in Term Definitions
377
+ term_definitions.merge!(context.term_definitions)
378
+ self
379
+ end
380
+
356
381
  ##
357
382
  # Create Term Definition
358
383
  #
@@ -415,7 +440,7 @@ module JSON::LD
415
440
  type
416
441
  when String
417
442
  begin
418
- expand_iri(type, :vocab => true, :documentRelative => false, :local_context => local_context, :defined => defined)
443
+ expand_iri(type, vocab: true, documentRelative: false, local_context: local_context, defined: defined)
419
444
  rescue JsonLdError::InvalidIRIMapping
420
445
  raise JsonLdError::InvalidTypeMapping, "invalid mapping for '@type': #{type.inspect} on term #{term.inspect}"
421
446
  end
@@ -437,10 +462,10 @@ module JSON::LD
437
462
 
438
463
  # Otherwise, set the IRI mapping of definition to the result of using the IRI Expansion algorithm, passing active context, the value associated with the @reverse key for value, true for vocab, true for document relative, local context, and defined. If the result is not an absolute IRI, i.e., it contains no colon (:), an invalid IRI mapping error has been detected and processing is aborted.
439
464
  definition.id = expand_iri(value['@reverse'],
440
- :vocab => true,
441
- :documentRelative => true,
442
- :local_context => local_context,
443
- :defined => defined)
465
+ vocab: true,
466
+ documentRelative: true,
467
+ local_context: local_context,
468
+ defined: defined)
444
469
  raise JsonLdError::InvalidIRIMapping, "non-absolute @reverse IRI: #{definition.id} on term #{term.inspect}" unless
445
470
  definition.id.is_a?(RDF::URI) && definition.id.absolute?
446
471
 
@@ -456,10 +481,10 @@ module JSON::LD
456
481
  raise JsonLdError::InvalidIRIMapping, "expected value of @id to be a string: #{value['@id'].inspect} on term #{term.inspect}" unless
457
482
  value['@id'].is_a?(String)
458
483
  definition.id = expand_iri(value['@id'],
459
- :vocab => true,
460
- :documentRelative => true,
461
- :local_context => local_context,
462
- :defined => defined)
484
+ vocab: true,
485
+ documentRelative: true,
486
+ local_context: local_context,
487
+ defined: defined)
463
488
  raise JsonLdError::InvalidKeywordAlias, "expected value of @id to not be @context on term #{term.inspect}" if
464
489
  definition.id == '@context'
465
490
  elsif term.include?(':')
@@ -877,7 +902,7 @@ module JSON::LD
877
902
  preferred_values = []
878
903
  preferred_values << '@reverse' if tl_value == '@reverse'
879
904
  if %w(@id @reverse).include?(tl_value) && value.is_a?(Hash) && value.has_key?('@id')
880
- t_iri = compact_iri(value['@id'], :vocab => true, :document_relative => true)
905
+ t_iri = compact_iri(value['@id'], vocab: true, document_relative: true)
881
906
  if (r_td = term_definitions[t_iri]) && r_td.id == value['@id']
882
907
  preferred_values.concat(%w(@vocab @id @none))
883
908
  else
@@ -965,20 +990,20 @@ module JSON::LD
965
990
  # @raise [RDF::ReaderError] if the iri cannot be expanded
966
991
  # @see http://json-ld.org/spec/latest/json-ld-api/#value-expansion
967
992
  def expand_value(property, value, options = {})
968
- options = {:useNativeTypes => false}.merge(options)
993
+ options = {useNativeTypes: false}.merge(options)
969
994
  depth(options) do
970
995
  debug("expand_value") {"property: #{property.inspect}, value: #{value.inspect}"}
971
996
 
972
997
  # If the active property has a type mapping in active context that is @id, return a new JSON object containing a single key-value pair where the key is @id and the value is the result of using the IRI Expansion algorithm, passing active context, value, and true for document relative.
973
998
  if (td = term_definitions.fetch(property, TermDefinition.new(property))) && td.type_mapping == '@id'
974
999
  debug("") {"as relative IRI: #{value.inspect}"}
975
- return {'@id' => expand_iri(value, :documentRelative => true).to_s}
1000
+ return {'@id' => expand_iri(value, documentRelative: true).to_s}
976
1001
  end
977
1002
 
978
1003
  # If active property has a type mapping in active context that is @vocab, return a new JSON object containing a single key-value pair where the key is @id and the value is the result of using the IRI Expansion algorithm, passing active context, value, true for vocab, and true for document relative.
979
1004
  if td.type_mapping == '@vocab'
980
1005
  debug("") {"as vocab IRI: #{value.inspect}"}
981
- return {'@id' => expand_iri(value, :vocab => true, :documentRelative => true).to_s}
1006
+ return {'@id' => expand_iri(value, vocab: true, documentRelative: true).to_s}
982
1007
  end
983
1008
 
984
1009
  value = RDF::Literal(value) if
@@ -1063,12 +1088,12 @@ module JSON::LD
1063
1088
  when coerce(property) == '@vocab' && value.has_key?('@id') && num_members == 1
1064
1089
  # Compact an @id coercion
1065
1090
  debug("") {" (@id & coerce & vocab)"}
1066
- compact_iri(value['@id'], :vocab => true)
1091
+ compact_iri(value['@id'], vocab: true)
1067
1092
  when value.has_key?('@id')
1068
1093
  debug("") {" (@id)"}
1069
1094
  # return value as is
1070
1095
  value
1071
- when value['@type'] && expand_iri(value['@type'], :vocab => true) == coerce(property)
1096
+ when value['@type'] && expand_iri(value['@type'], vocab: true) == coerce(property)
1072
1097
  # Compact common datatype
1073
1098
  debug("") {" (@type & coerce) == #{coerce(property)}"}
1074
1099
  value['@value']
@@ -15,7 +15,7 @@ module JSON::LD
15
15
  # Ensure output objects have keys ordered properly
16
16
  # @return [Array, Hash]
17
17
  def expand(input, active_property, context, options = {})
18
- options = {:ordered => true}.merge(options)
18
+ options = {ordered: true}.merge(options)
19
19
  debug("expand") {"input: #{input.inspect}, active_property: #{active_property.inspect}, context: #{context.inspect}"}
20
20
  result = case input
21
21
  when Array
@@ -49,7 +49,7 @@ module JSON::LD
49
49
  keys.each do |key|
50
50
  # For each key and value in element, ordered lexicographically by key:
51
51
  value = input[key]
52
- expanded_property = context.expand_iri(key, :vocab => true, :depth => @depth)
52
+ expanded_property = context.expand_iri(key, vocab: true, depth: @depth)
53
53
 
54
54
  # If expanded property is null or it neither contains a colon (:) nor it is a keyword, drop key by continuing to the next key.
55
55
  next if expanded_property.is_a?(RDF::URI) && expanded_property.relative?
@@ -78,7 +78,7 @@ module JSON::LD
78
78
  "value of @id must be a string: #{value.inspect}" unless value.is_a?(String)
79
79
 
80
80
  # Otherwise, set expanded value to the result of using the IRI Expansion algorithm, passing active context, value, and true for document relative.
81
- context.expand_iri(value, :documentRelative => true, :depth => @depth).to_s
81
+ context.expand_iri(value, documentRelative: true, depth: @depth).to_s
82
82
  when '@type'
83
83
  # If expanded property is @type and value is neither a string nor an array of strings, an invalid type value error has been detected and processing is aborted. Otherwise, set expanded value to the result of using the IRI Expansion algorithm, passing active context, true for vocab, and true for document relative to expand the value or each of its items.
84
84
  debug("@type") {"value: #{value.inspect}"}
@@ -88,11 +88,11 @@ module JSON::LD
88
88
  value.map do |v|
89
89
  raise JsonLdError::InvalidTypeValue,
90
90
  "@type value must be a string or array of strings: #{v.inspect}" unless v.is_a?(String)
91
- context.expand_iri(v, :vocab => true, :documentRelative => true, :quiet => true, :depth => @depth).to_s
91
+ context.expand_iri(v, vocab: true, documentRelative: true, quiet: true, depth: @depth).to_s
92
92
  end
93
93
  end
94
94
  when String
95
- context.expand_iri(value, :vocab => true, :documentRelative => true, :quiet => true, :depth => @depth).to_s
95
+ context.expand_iri(value, vocab: true, documentRelative: true, quiet: true, depth: @depth).to_s
96
96
  when Hash
97
97
  # For framing
98
98
  raise JsonLdError::InvalidTypeValue,
@@ -298,7 +298,7 @@ module JSON::LD
298
298
  raise JsonLdError::InvalidLanguageTaggedValue,
299
299
  "when @language is used, @value must be a string: #{@value.inspect}"
300
300
  elsif !output_object.fetch('@type', "").is_a?(String) ||
301
- !context.expand_iri(output_object.fetch('@type', ""), :vocab => true, :depth => @depth).is_a?(RDF::URI)
301
+ !context.expand_iri(output_object.fetch('@type', ""), vocab: true, depth: @depth).is_a?(RDF::URI)
302
302
  # Otherwise, if the result has a @type member and its value is not an IRI, an invalid typed value error has been detected and processing is aborted.
303
303
  raise JsonLdError::InvalidTypedValue,
304
304
  "value of @type must be an IRI: #{output_object['@type'].inspect}"
@@ -338,7 +338,7 @@ module JSON::LD
338
338
  else
339
339
  # Otherwise, unless the value is a number, expand the value according to the Value Expansion rules, passing active property.
340
340
  return nil if input.nil? || active_property.nil? || active_property == '@graph'
341
- context.expand_value(active_property, input, :depth => @depth)
341
+ context.expand_value(active_property, input, depth: @depth)
342
342
  end
343
343
 
344
344
  debug {" => #{result.inspect}"}
@@ -16,7 +16,7 @@ module RDF
16
16
  @properties.delete(subject.to_s) if recalc
17
17
  @properties[subject.to_s] ||= begin
18
18
  hash = Hash.new
19
- self.query(:subject => subject) do |statement|
19
+ self.query(subject: subject) do |statement|
20
20
  pred = statement.predicate.to_s
21
21
 
22
22
  hash[pred] ||= []
@@ -28,7 +28,7 @@ module RDF
28
28
 
29
29
  # Get type(s) of subject, returns a list of symbols
30
30
  def type_of(subject)
31
- query(:subject => subject, :predicate => RDF.type).map {|st| st.object}
31
+ query(subject: subject, predicate: RDF.type).map {|st| st.object}
32
32
  end
33
33
  end
34
34
 
@@ -167,7 +167,7 @@ module JSON::LD
167
167
  end
168
168
  end
169
169
 
170
- debug("node_map") {node_map.to_json(JSON_STATE)}
170
+ debug("node_map") {node_map.to_json(JSON_STATE) rescue 'malformed json'}
171
171
  end
172
172
  end
173
173
  end
@@ -9,21 +9,21 @@ module JSON::LD
9
9
  # RDF::Format.for("etc/foaf.ld")
10
10
  # RDF::Format.for(:file_name => "etc/foaf.json")
11
11
  # RDF::Format.for(:file_name => "etc/foaf.ld")
12
- # RDF::Format.for(:file_extension => "json")
13
- # RDF::Format.for(:file_extension => "ld")
12
+ # RDF::Format.for(file_extension: "json")
13
+ # RDF::Format.for(file_extension: "ld")
14
14
  # RDF::Format.for(:content_type => "application/json")
15
15
  #
16
16
  # @example Obtaining serialization format MIME types
17
17
  # RDF::Format.content_types #=> {"application/json" => [JSON::LD::Format]}
18
18
  #
19
19
  # @example Obtaining serialization format file extension mappings
20
- # RDF::Format.file_extensions #=> {:json => "application/json"}
20
+ # RDF::Format.file_extensions #=> {json: "application/json"}
21
21
  #
22
22
  # @see http://www.w3.org/TR/rdf-testcases/#ntriples
23
23
  class Format < RDF::Format
24
24
  content_type 'application/ld+json',
25
- :extension => :jsonld,
26
- :alias => 'application/x-ld+json'
25
+ extension: :jsonld,
26
+ alias: 'application/x-ld+json'
27
27
  content_encoding 'utf-8'
28
28
 
29
29
  reader { JSON::LD::Reader }
@@ -21,8 +21,8 @@ module JSON::LD
21
21
  parent, property = options[:parent], options[:property]
22
22
  debug("frame") {"state: #{state.inspect}"}
23
23
  debug("frame") {"nodes: #{nodes.keys.inspect}"}
24
- debug("frame") {"frame: #{frame.to_json(JSON_STATE)}"}
25
- debug("frame") {"parent: #{parent.to_json(JSON_STATE)}"}
24
+ debug("frame") {"frame: #{frame.to_json(JSON_STATE) rescue 'malformed json'}"}
25
+ debug("frame") {"parent: #{parent.to_json(JSON_STATE) rescue 'malformed json'}"}
26
26
  debug("frame") {"property: #{property.inspect}"}
27
27
  # Validate the frame
28
28
  validate_frame(state, frame)
@@ -41,12 +41,12 @@ module JSON::LD
41
41
  matches.keys.kw_sort.each do |id|
42
42
  element = matches[id]
43
43
  # If the active property is null, set the map of embeds in state to an empty map
44
- state = state.merge(:embeds => {}) if property.nil?
44
+ state = state.merge(embeds: {}) if property.nil?
45
45
 
46
46
  output = {'@id' => id}
47
47
 
48
48
  # prepare embed meta info
49
- embedded_node = {:parent => parent, :property => property}
49
+ embedded_node = {parent: parent, property: property}
50
50
 
51
51
  # If embedOn is true, and id is in map of embeds from state
52
52
  if embed && (existing = state[:embeds].fetch(id, nil))
@@ -340,7 +340,7 @@ module JSON::LD
340
340
  unless state[:embeds].has_key?(sid)
341
341
  debug("frame") {"embed element #{sid.inspect}"}
342
342
  # Embed full element, if it isn't already embedded
343
- embed = {:parent => output, :property => property}
343
+ embed = {parent: output, property: property}
344
344
  state[:embeds][sid] = embed
345
345
 
346
346
  # Recurse into element
@@ -71,7 +71,7 @@ module JSON::LD
71
71
  list, list_nodes = [], []
72
72
 
73
73
  # If property equals rdf:rest, the value associated to the usages member of node has exactly 1 entry, node has a rdf:first and rdf:rest property, both of which have as value an array consisting of a single element, and node has no other members apart from an optional @type member whose value is an array with a single item equal to rdf:List, node represents a well-formed list node. Continue with the following steps:
74
- debug("list element?") {node.to_json(JSON_STATE)}
74
+ debug("list element?") {node.to_json(JSON_STATE) rescue 'malformed json'}
75
75
  while property == RDF.rest.to_s &&
76
76
  node_usages_map[node['@id']].uniq.length == 1 &&
77
77
  blank_node?(node) &&
@@ -118,7 +118,7 @@ module JSON::LD
118
118
  node.delete(:usages)
119
119
  result << node unless node_reference?(node)
120
120
  end
121
- debug("fromRdf") {result.to_json(JSON_STATE)}
121
+ debug("fromRdf") {result.to_json(JSON_STATE) rescue 'malformed json'}
122
122
  result
123
123
  end
124
124
  end
@@ -1,5 +1,3 @@
1
- require 'open-uri'
2
-
3
1
  module JSON::LD
4
2
  ##
5
3
  # A JSON-LD parser in Ruby.
@@ -128,7 +128,7 @@ module JSON::LD
128
128
  end
129
129
 
130
130
  compacted = nil
131
- JSON::LD::API.expand(node_definition, :expandContext => @context) do |expanded|
131
+ JSON::LD::API.expand(node_definition, expandContext: @context) do |expanded|
132
132
  compacted = JSON::LD::API.compact(expanded, @context)
133
133
  end
134
134
  compacted.delete_if {|k, v| k == '@context'}
@@ -169,9 +169,9 @@ module JSON::LD
169
169
  when Hash
170
170
  if node_reference?(obj)
171
171
  reference_map[obj['id']] ||= Resource.new(obj,
172
- :context => @context_name,
173
- :clean => false,
174
- :stub => true
172
+ context: @context_name,
173
+ clean: false,
174
+ stub: true
175
175
  )
176
176
  else
177
177
  obj.keys.each do |k|