json-ld 0.1.5.1 → 0.1.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5.1
1
+ 0.1.5.2
data/lib/json/ld/api.rb CHANGED
@@ -288,7 +288,8 @@ module JSON::LD
288
288
  # @param [Proc] callback (&block)
289
289
  # Alternative to using block, with same parameteres.
290
290
  # @param [Hash{Symbol => Object}] options
291
- # @option options [Boolean] :notType don't use @type for rdf:type
291
+ # @option options [Boolean] :useRdfType (false) use rdf:type instead of @type
292
+ # @option options [Boolean] :useNativeDatatypes FIXME
292
293
  # @yield jsonld
293
294
  # @yieldparam [Hash] jsonld
294
295
  # 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)
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 = self.base unless [:predicate, :datatype].include?(options[:position])
494
+ base = options.fetch(:base, self.base) unless [:predicate, :datatype].include?(options[:position])
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[:subjects][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)
data/spec/suite_helper.rb CHANGED
@@ -26,27 +26,32 @@ module RDF::Util
26
26
  path = filename_or_url[5..-1]
27
27
  Kernel.open(path.to_s, &block)
28
28
  when /^#{REMOTE_PATH}/
29
- #puts "attempt to open #{filename_or_url} locally"
30
- if response = ::File.open(filename_or_url.to_s.sub(REMOTE_PATH, LOCAL_PATH))
31
- #puts "use #{filename_or_url} locally"
32
- case filename_or_url.to_s
33
- when /\.jsonld$/
34
- def response.content_type; 'application/ld+json'; end
35
- when /\.sparql$/
36
- def response.content_type; 'application/sparql-query'; end
37
- end
29
+ begin
30
+ #puts "attempt to open #{filename_or_url} locally"
31
+ if response = ::File.open(filename_or_url.to_s.sub(REMOTE_PATH, LOCAL_PATH))
32
+ #puts "use #{filename_or_url} locally"
33
+ case filename_or_url.to_s
34
+ when /\.jsonld$/
35
+ def response.content_type; 'application/ld+json'; end
36
+ when /\.sparql$/
37
+ def response.content_type; 'application/sparql-query'; end
38
+ end
38
39
 
39
- if block_given?
40
- begin
41
- yield response
42
- ensure
43
- response.close
40
+ if block_given?
41
+ begin
42
+ yield response
43
+ ensure
44
+ response.close
45
+ end
46
+ else
47
+ response
44
48
  end
45
49
  else
46
- response
50
+ Kernel.open(filename_or_url.to_s, &block)
47
51
  end
48
- else
49
- Kernel.open(filename_or_url.to_s, &block)
52
+ rescue Errno::ENOENT
53
+ # Not there, don't run tests
54
+ StringIO.new("")
50
55
  end
51
56
  else
52
57
  end
data/spec/suite_spec.rb CHANGED
@@ -6,61 +6,62 @@ describe JSON::LD do
6
6
  describe "test suite" do
7
7
  require 'suite_helper'
8
8
 
9
- m = Fixtures::JSONLDTest::Manifest.each.to_a.first
10
- describe m.name do
11
- m.entries.each do |m2|
12
- describe m2.name do
13
- m2.entries.each do |t|
14
- next if t.is_a?(Fixtures::JSONLDTest::NormalizeTest)
15
- specify "#{File.basename(t.inputDocument.to_s)}: #{t.name}" do
16
- begin
17
- t.debug = ["test: #{t.inspect}", "source: #{t.input.read}"]
18
- case t
19
- when Fixtures::JSONLDTest::CompactTest
20
- t.debug << "context: #{t.extra.read}" if t.extraDocument
21
- result = JSON::LD::API.compact(t.input, t.extra, nil,
22
- :base => t.base_uri,
23
- :debug => t.debug)
24
- expected = JSON.load(t.expect)
25
- result.should produce(expected, t.debug)
26
- when Fixtures::JSONLDTest::ExpandTest
27
- t.debug << "context: #{t.extra.read}" if t.extraDocument
28
- result = JSON::LD::API.expand(t.input, nil, nil,
29
- :base => t.base_uri,
30
- :debug => t.debug)
31
- expected = JSON.load(t.expect)
32
- result.should produce(expected, t.debug)
33
- when Fixtures::JSONLDTest::FrameTest
34
- t.debug << "frame: #{t.extra.read}" if t.extraDocument
35
- result = JSON::LD::API.frame(t.input, t.extra, nil,
36
- :base => t.inputDocument,
37
- :debug => t.debug)
38
- expected = JSON.load(t.expect)
39
- result.should produce(expected, t.debug)
40
- when Fixtures::JSONLDTest::FromRDFTest
41
- repo = RDF::Repository.load(t.inputDocument)
42
- result = JSON::LD::API.fromRDF(repo.each_statement.to_a, nil,
43
- :debug => t.debug)
44
- expected = JSON.load(t.expect)
45
- result.should produce(expected, t.debug)
46
- when Fixtures::JSONLDTest::ToRDFTest
47
- quads = []
48
- JSON::LD::API.toRDF(t.input, nil, nil,
49
- :base => t.inputDocument,
50
- :debug => t.debug) do |statement|
51
- quads << to_quad(statement)
52
- end
9
+ if m = Fixtures::JSONLDTest::Manifest.each.to_a.first
10
+ describe m.name do
11
+ m.entries.each do |m2|
12
+ describe m2.name do
13
+ m2.entries.each do |t|
14
+ next if t.is_a?(Fixtures::JSONLDTest::NormalizeTest)
15
+ specify "#{File.basename(t.inputDocument.to_s)}: #{t.name}" do
16
+ begin
17
+ t.debug = ["test: #{t.inspect}", "source: #{t.input.read}"]
18
+ case t
19
+ when Fixtures::JSONLDTest::CompactTest
20
+ t.debug << "context: #{t.extra.read}" if t.extraDocument
21
+ result = JSON::LD::API.compact(t.input, t.extra, nil,
22
+ :base => t.base_uri,
23
+ :debug => t.debug)
24
+ expected = JSON.load(t.expect)
25
+ result.should produce(expected, t.debug)
26
+ when Fixtures::JSONLDTest::ExpandTest
27
+ t.debug << "context: #{t.extra.read}" if t.extraDocument
28
+ result = JSON::LD::API.expand(t.input, nil, nil,
29
+ :base => t.base_uri,
30
+ :debug => t.debug)
31
+ expected = JSON.load(t.expect)
32
+ result.should produce(expected, t.debug)
33
+ when Fixtures::JSONLDTest::FrameTest
34
+ t.debug << "frame: #{t.extra.read}" if t.extraDocument
35
+ result = JSON::LD::API.frame(t.input, t.extra, nil,
36
+ :base => t.inputDocument,
37
+ :debug => t.debug)
38
+ expected = JSON.load(t.expect)
39
+ result.should produce(expected, t.debug)
40
+ when Fixtures::JSONLDTest::FromRDFTest
41
+ repo = RDF::Repository.load(t.inputDocument)
42
+ result = JSON::LD::API.fromRDF(repo.each_statement.to_a, nil,
43
+ :debug => t.debug)
44
+ expected = JSON.load(t.expect)
45
+ result.should produce(expected, t.debug)
46
+ when Fixtures::JSONLDTest::ToRDFTest
47
+ quads = []
48
+ JSON::LD::API.toRDF(t.input, nil, nil,
49
+ :base => t.inputDocument,
50
+ :debug => t.debug) do |statement|
51
+ quads << to_quad(statement)
52
+ end
53
53
 
54
- quads.sort.join("").should produce(t.expect.read, t.debug)
55
- else
56
- pending("unkown test type #{t.inspect}")
54
+ quads.sort.join("").should produce(t.expect.read, t.debug)
55
+ else
56
+ pending("unkown test type #{t.inspect}")
57
+ end
58
+ rescue JSON::LD::ProcessingError => e
59
+ fail("Processing error: #{e.message}")
60
+ rescue JSON::LD::InvalidContext => e
61
+ fail("Invalid Context: #{e.message}")
62
+ rescue JSON::LD::InvalidFrame => e
63
+ fail("Invalid Frame: #{e.message}")
57
64
  end
58
- rescue JSON::LD::ProcessingError => e
59
- fail("Processing error: #{e.message}")
60
- rescue JSON::LD::InvalidContext => e
61
- fail("Invalid Context: #{e.message}")
62
- rescue JSON::LD::InvalidFrame => e
63
- fail("Invalid Frame: #{e.message}")
64
65
  end
65
66
  end
66
67
  end
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.5.1
4
+ version: 0.1.5.2
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-08-07 00:00:00.000000000 Z
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdf