json-ld 0.1.6 → 0.1.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,6 @@
1
+ === 0.1.6.1
2
+ * Merge relative contexts from 0.1.5.2.
3
+
1
4
  === 0.1.6
2
5
  * Added flattening API, and updated algorithm.
3
6
  * Fixed framing issues using updated flattening.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.6.1
@@ -368,7 +368,8 @@ module JSON::LD
368
368
  # @param [Proc] callback (&block)
369
369
  # Alternative to using block, with same parameteres.
370
370
  # @param [Hash{Symbol => Object}] options
371
- # @option options [Boolean] :notType don't use @type for rdf:type
371
+ # @option options [Boolean] :useRdfType (false) use rdf:type instead of @type
372
+ # @option options [Boolean] :useNativeDatatypes FIXME
372
373
  # @yield jsonld
373
374
  # @yieldparam [Hash] jsonld
374
375
  # The JSON-LD document in expanded form
@@ -13,6 +13,11 @@ module JSON::LD
13
13
  # @attr_reader [RDF::URI]
14
14
  attr_reader :base
15
15
 
16
+ # The base IRI of the context, if loaded remotely.
17
+ #
18
+ # @attr [RDF::URI]
19
+ attr :context_base, true
20
+
16
21
  # A list of current, in-scope mappings from term to IRI.
17
22
  #
18
23
  # @attr [Hash{String => String}]
@@ -132,18 +137,26 @@ module JSON::LD
132
137
  raise JSON::LD::InvalidContext::Syntax, "Failed to parse remote context at #{context}: #{e.message}" if @options[:validate]
133
138
  self.dup
134
139
  end
135
- when String, nil
140
+ when nil
141
+ debug("parse") {"nil"}
142
+ # Load context document, if it is a string
143
+ ec = EvaluationContext.new(options)
144
+ when String
136
145
  debug("parse") {"remote: #{context}"}
137
146
  # Load context document, if it is a string
138
147
  ec = nil
139
148
  begin
140
- RDF::Util::File.open_file(context.to_s) {|f| ec = parse(f)}
149
+ url = expand_iri(context, :base => context_base || base, :position => :subject)
150
+ ecdup = self.dup
151
+ ecdup.context_base = url # Set context_base for recursive remote contexts
152
+ RDF::Util::File.open_file(url) {|f| ec = ecdup.parse(f)}
141
153
  ec.provided_context = context
154
+ ec.context_base = url
142
155
  debug("parse") {"=> provided_context: #{context.inspect}"}
143
156
  ec
144
157
  rescue Exception => e
145
- debug("parse") {"Failed to retrieve @context from remote document at #{context}: #{e.message}"}
146
- raise JSON::LD::InvalidContext::LoadError, "Failed to parse remote context at #{context}: #{e.message}", e.backtrace if @options[:validate]
158
+ debug("parse") {"Failed to retrieve @context from remote document at #{context.inspect}: #{e.message}"}
159
+ raise JSON::LD::InvalidContext::LoadError, "Failed to retrieve remote context at #{context.inspect}: #{e.message}", e.backtrace if @options[:validate]
147
160
  self.dup
148
161
  end
149
162
  when Array
@@ -467,6 +480,8 @@ module JSON::LD
467
480
  # @param [Hash{Symbol => Object}] options
468
481
  # @option options [:subject, :predicate, :object, :datatype] position
469
482
  # Useful when determining how to serialize.
483
+ # @option options [RDF::URI] base (self.base)
484
+ # Base IRI to use when expanding relative IRIs.
470
485
  #
471
486
  # @return [RDF::URI, String] IRI or String, if it's a keyword
472
487
  # @raise [RDF::ReaderError] if the iri cannot be expanded
@@ -476,7 +491,7 @@ module JSON::LD
476
491
  prefix, suffix = iri.split(':', 2)
477
492
  return mapping(iri) if mapping(iri) # If it's an exact match
478
493
  debug("expand_iri") {"prefix: #{prefix.inspect}, suffix: #{suffix.inspect}, vocab: #{vocab.inspect}"} unless options[:quiet]
479
- base = [:subject, :object].include?(options[:position]) ? self.base : nil
494
+ base = [:subject, :object].include?(options[:position]) ? options.fetch(:base, self.base) : nil
480
495
  prefix = prefix.to_s
481
496
  case
482
497
  when prefix == '_' && suffix then bnode(suffix)
@@ -654,13 +669,13 @@ module JSON::LD
654
669
  # @param [Hash, String] value
655
670
  # Value (literal or IRI) to be expanded
656
671
  # @param [Hash{Symbol => Object}] options
657
- # @option options [Boolean] :native (true) use native representations
672
+ # @option options [Boolean] :useNativeTypes (true) use native representations
658
673
  #
659
674
  # @return [Hash] Object representation of value
660
675
  # @raise [RDF::ReaderError] if the iri cannot be expanded
661
676
  # @see http://json-ld.org/spec/latest/json-ld-api/#value-expansion
662
677
  def expand_value(property, value, options = {})
663
- options = {:native => true}.merge(options)
678
+ options = {:useNativeTypes => true}.merge(options)
664
679
  depth(options) do
665
680
  debug("expand_value") {"property: #{property.inspect}, value: #{value.inspect}, coerce: #{coerce(property).inspect}"}
666
681
  value = RDF::Literal(value) if RDF::Literal(value).has_datatype?
@@ -681,7 +696,7 @@ module JSON::LD
681
696
  when RDF::XSD.double.to_s
682
697
  {"@value" => value.to_s, "@type" => RDF::XSD.double.to_s}
683
698
  else
684
- if options[:native]
699
+ if options[:useNativeTypes]
685
700
  # Unless there's coercion, to not modify representation
686
701
  {"@value" => (value.is_a?(RDF::Literal::Boolean) ? value.object : value)}
687
702
  else
@@ -695,7 +710,7 @@ module JSON::LD
695
710
  {"@value" => RDF::Literal::Double.new(value, :canonicalize => true).to_s, "@type" => RDF::XSD.double.to_s}
696
711
  when RDF::XSD.integer.to_s, nil
697
712
  # Unless there's coercion, to not modify representation
698
- if options[:native]
713
+ if options[:useNativeTypes]
699
714
  {"@value" => value.is_a?(RDF::Literal::Integer) ? value.object : value}
700
715
  else
701
716
  {"@value" => value.to_s, "@type" => RDF::XSD.integer.to_s}
@@ -714,7 +729,7 @@ module JSON::LD
714
729
  when RDF::XSD.double.to_s
715
730
  {"@value" => RDF::Literal::Double.new(value, :canonicalize => true).to_s, "@type" => RDF::XSD.double.to_s}
716
731
  when nil
717
- if options[:native]
732
+ if options[:useNativeTypes]
718
733
  # Unless there's coercion, to not modify representation
719
734
  {"@value" => value.is_a?(RDF::Literal::Double) ? value.object : value}
720
735
  else
@@ -64,8 +64,8 @@ module JSON::LD
64
64
  value = graph[:nodes][subject] ||= {'@id' => subject}
65
65
 
66
66
  # If property is http://www.w3.org/1999/02/22-rdf-syntax-ns#type
67
- # and the notType option is not true
68
- if statement.predicate == RDF.type && !@options[:notType]
67
+ # and the useRdfType option is not true
68
+ if statement.predicate == RDF.type && !@options[:useRdfType]
69
69
  object = ec.expand_iri(statement.object).to_s
70
70
  debug("@type") { object.inspect}
71
71
  # append the string representation of object to the array value for the key @type, creating
@@ -78,10 +78,11 @@ module JSON::LD
78
78
  key = ec.expand_iri(statement.predicate).to_s
79
79
  (value[key] ||= []) << {"@list" => []}
80
80
  else
81
- # Otherwise, let key be the string representation of predicate and let object representation
82
- # be object represented in expanded form as described in Value Expansion.
81
+ # Otherwise, let key be the string representation of predicate and
82
+ # let object representation be object represented in expanded form as
83
+ # described in Value Expansion.
83
84
  key = ec.expand_iri(statement.predicate).to_s
84
- object = ec.expand_value(key, statement.object, :native => false)
85
+ object = ec.expand_value(key, statement.object, @options)
85
86
  if blank_node?(object)
86
87
  # if object is an Unnamed Node, set as the head element in the listMap
87
88
  # entry for object
@@ -32,7 +32,7 @@ describe JSON::LD::EvaluationContext do
32
32
 
33
33
  it "fails given a missing remote @context" do
34
34
  RDF::Util::File.stub(:open_file).with("http://example.com/context").and_raise(IOError)
35
- lambda {subject.parse("http://example.com/context")}.should raise_error(JSON::LD::InvalidContext, /Failed to parse remote context/)
35
+ lambda {subject.parse("http://example.com/context")}.should raise_error(JSON::LD::InvalidContext, /Failed to retrieve remote context/)
36
36
  end
37
37
 
38
38
  it "creates mappings" do
@@ -49,6 +49,18 @@ describe JSON::LD::EvaluationContext do
49
49
  ec = subject.parse(StringIO.new("{}"))
50
50
  ec.mappings.should produce({}, @debug)
51
51
  end
52
+
53
+ it "parses a referenced context at a relative URI" do
54
+ c1 = StringIO.new(%({"@context": "context"}))
55
+ RDF::Util::File.stub(:open_file).with("http://example.com/c1").and_yield(c1)
56
+ RDF::Util::File.stub(:open_file).with("http://example.com/context").and_yield(@ctx)
57
+ ec = subject.parse("http://example.com/c1")
58
+ ec.mappings.should produce({
59
+ "name" => "http://xmlns.com/foaf/0.1/name",
60
+ "homepage" => "http://xmlns.com/foaf/0.1/homepage",
61
+ "avatar" => "http://xmlns.com/foaf/0.1/avatar"
62
+ }, @debug)
63
+ end
52
64
  end
53
65
 
54
66
  context "EvaluationContext" do
@@ -65,6 +65,14 @@ describe JSON::LD::API do
65
65
  it "integer" do
66
66
  input = %(@prefix ex: <http://example.com/> . ex:a ex:b 1 .)
67
67
  serialize(input).should produce([{
68
+ '@id' => "http://example.com/a",
69
+ "http://example.com/b" => [{"@value" => 1}]
70
+ }], @debug)
71
+ end
72
+
73
+ it "integer (non-native)" do
74
+ input = %(@prefix ex: <http://example.com/> . ex:a ex:b 1 .)
75
+ serialize(input, :useNativeTypes => false).should produce([{
68
76
  '@id' => "http://example.com/a",
69
77
  "http://example.com/b" => [{"@value" => "1","@type" => "http://www.w3.org/2001/XMLSchema#integer"}]
70
78
  }], @debug)
@@ -73,6 +81,14 @@ describe JSON::LD::API do
73
81
  it "boolean" do
74
82
  input = %(@prefix ex: <http://example.com/> . ex:a ex:b true .)
75
83
  serialize(input).should produce([{
84
+ '@id' => "http://example.com/a",
85
+ "http://example.com/b" => [{"@value" => true}]
86
+ }], @debug)
87
+ end
88
+
89
+ it "boolean (non-native)" do
90
+ input = %(@prefix ex: <http://example.com/> . ex:a ex:b true .)
91
+ serialize(input, :useNativeTypes => false).should produce([{
76
92
  '@id' => "http://example.com/a",
77
93
  "http://example.com/b" => [{"@value" => "true","@type" => "http://www.w3.org/2001/XMLSchema#boolean"}]
78
94
  }], @debug)
@@ -89,13 +105,21 @@ describe JSON::LD::API do
89
105
  it "double" do
90
106
  input = %(@prefix ex: <http://example.com/> . ex:a ex:b 1.0e0 .)
91
107
  serialize(input).should produce([{
108
+ '@id' => "http://example.com/a",
109
+ "http://example.com/b" => [{"@value" => 1.0E0}]
110
+ }], @debug)
111
+ end
112
+
113
+ it "double (non-native)" do
114
+ input = %(@prefix ex: <http://example.com/> . ex:a ex:b 1.0e0 .)
115
+ serialize(input, :useNativeTypes => false).should produce([{
92
116
  '@id' => "http://example.com/a",
93
117
  "http://example.com/b" => [{"@value" => "1.0E0","@type" => "http://www.w3.org/2001/XMLSchema#double"}]
94
118
  }], @debug)
95
119
  end
96
120
  end
97
121
 
98
- context "datatyped" do
122
+ context "datatyped (non-native)" do
99
123
  {
100
124
  :integer => 1,
101
125
  :unsignedInteger => 1,
@@ -110,7 +134,7 @@ describe JSON::LD::API do
110
134
  @prefix ex: <http://example.com/> .
111
135
  ex:a ex:b "#{v}"^^xsd:#{t} .
112
136
  )
113
- serialize(input).should produce([{
137
+ serialize(input, :useNativeTypes => false).should produce([{
114
138
  '@id' => "http://example.com/a",
115
139
  "http://example.com/b" => [{"@value" => "#{v}","@type" => "http://www.w3.org/2001/XMLSchema##{t}"}]
116
140
  }], @debug)
@@ -307,10 +331,10 @@ describe JSON::LD::API do
307
331
  end
308
332
  end
309
333
 
310
- context "notType option" do
334
+ context "useRdfType option" do
311
335
  it "uses @type if set to false" do
312
336
  input = %(@prefix ex: <http://example.com/> . ex:a a ex:b .)
313
- serialize(input, :notType => false).should produce([{
337
+ serialize(input, :useRdfType => false).should produce([{
314
338
  '@id' => "http://example.com/a",
315
339
  "@type" => ["http://example.com/b"]
316
340
  }], @debug)
@@ -318,7 +342,7 @@ describe JSON::LD::API do
318
342
 
319
343
  it "does not use @type if set to true" do
320
344
  input = %(@prefix ex: <http://example.com/> . ex:a a ex:b .)
321
- serialize(input, :notType => true).should produce([{
345
+ serialize(input, :useRdfType => true).should produce([{
322
346
  '@id' => "http://example.com/a",
323
347
  RDF.type.to_s => [{"@id" => "http://example.com/b"}]
324
348
  }], @debug)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-ld
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.6.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-11 00:00:00.000000000 Z
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdf