tripod 0.10.10 → 0.10.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17e09d56a300cfbce5e1e35feb3be1821a129eee
4
- data.tar.gz: 8b149e9c78c869ec7f307e84d2e609840b0271fe
3
+ metadata.gz: ba073a508bbb8401fee9818d86cab1d1d97dd506
4
+ data.tar.gz: 89d5cab48aa1e06f603bddd46ccc2d98b8fc592e
5
5
  SHA512:
6
- metadata.gz: 8c32107cb6bb6d0065f5584e08be54f54ac08222a63ebf8a9e4dbf88ccbcc01425bfb9e61d0c4f4aa938d76cf9439b22f44ee6387f80cb03f537f44b4f8e549e
7
- data.tar.gz: 42054adedd9b7975e3ddef60678b7118f596e75982ff6d7cfd731c9ed745767bb382cc9f9326e552cdf98935efc7ab8afc1236ec8bc4aee9b2a492244b71ea13
6
+ metadata.gz: 7ff8a8d538b37f697433d2c1edebec5d80cfd092976b395812de0277bc8abb526d65df706dd8dcc1f68bf0d6c481c57327b290acd2e09b162bbde40ab53a0644
7
+ data.tar.gz: 5decb3639f621abcfecd3e82380ba710b9941937748b2ce39290f22795f941a3958fa9a53facf91c0635be2363bc1bb77bd7aa64fb711bd38de02553ae2e4693
@@ -161,8 +161,8 @@ module Tripod::Finders
161
161
  def _create_and_hydrate_resources_from_sparql(select_sparql, opts={})
162
162
  # TODO: Optimization?: if return_graph option is false, then don't do this next line?
163
163
  uris_and_graphs = _select_uris_and_graphs(select_sparql, :uri_variable => opts[:uri_variable], :graph_variable => opts[:graph_variable])
164
- ntriples_string = _raw_describe_select_results(select_sparql, :uri_variable => opts[:uri_variable]) # this defaults to ntriples
165
- graph = _rdf_graph_from_ntriples_string(ntriples_string, graph=nil)
164
+ construct_query = _construct_query_for_uris_and_graphs(uris_and_graphs)
165
+ graph = _graph_of_triples_from_construct_or_describe(construct_query)
166
166
  _resources_from_graph(graph, uris_and_graphs)
167
167
  end
168
168
 
@@ -186,6 +186,16 @@ module Tripod::Finders
186
186
  "
187
187
  end
188
188
 
189
+ # Generate a CONSTRUCT query for the given uri and graph pairs.
190
+ def _construct_query_for_uris_and_graphs(uris_and_graphs)
191
+ value_pairs = uris_and_graphs.map do |(uri, graph)|
192
+ u = RDF::URI.new(uri).to_base
193
+ g = graph ? RDF::URI.new(graph).to_base : 'UNDEF'
194
+ "(#{u} #{g})"
195
+ end
196
+ query = "CONSTRUCT { ?uri ?p ?o . #{ self.all_triples_construct("?uri") }} WHERE { GRAPH ?g { ?uri ?p ?o . #{ self.all_triples_where("?uri") } VALUES (?uri ?g) { #{ value_pairs.join(' ') } } } }"
197
+ end
198
+
189
199
  # For a select query, get a raw serialisation of the DESCRIPTION of the resources from the database.
190
200
  #
191
201
  # @option options [ String ] uri_variable The name of the uri variable in the query, if not 'uri'
@@ -207,7 +217,7 @@ module Tripod::Finders
207
217
  # uris from the graph, and just not create the resoruces with a graph
208
218
  # (but they won't be persistable).
209
219
 
210
- uris_and_graphs.each_pair do |u,g|
220
+ uris_and_graphs.each do |(u,g)|
211
221
 
212
222
  # instantiate a new resource
213
223
  g ||= {}
@@ -232,7 +242,7 @@ module Tripod::Finders
232
242
  resources
233
243
  end
234
244
 
235
- # based on the query passed in, build a hash of uris->graphs
245
+ # based on the query passed in, build an array of [uri, graph] pairs
236
246
  # @param [ String] sparql. The sparql query
237
247
  # @param [ Hash ] opts. A hash of options.
238
248
  #
@@ -241,16 +251,15 @@ module Tripod::Finders
241
251
  def _select_uris_and_graphs(sparql, opts={})
242
252
  select_results = Tripod::SparqlClient::Query.select(sparql)
243
253
 
244
- uris_and_graphs = {}
254
+ uri_variable = opts[:uri_variable] || 'uri'
255
+ graph_variable = opts[:graph_variable] || 'graph'
245
256
 
246
- select_results.each do |r|
247
- uri_variable = opts[:uri_variable] || 'uri'
248
- graph_variable = opts[:graph_variable] || 'graph'
249
- graph_value = r[graph_variable]["value"] if r[graph_variable]
250
- uris_and_graphs[ r[uri_variable]["value"] ] = graph_value || nil
257
+ select_results.reduce([]) do |memo, result|
258
+ u = result[uri_variable]['value']
259
+ g = result[graph_variable]['value'] if result[graph_variable]
260
+ memo << [u, g]
261
+ memo
251
262
  end
252
-
253
- uris_and_graphs
254
263
  end
255
264
 
256
265
  end
@@ -46,7 +46,7 @@ module Tripod::SparqlClient
46
46
  end
47
47
 
48
48
  # Tripod helper to turn a hash to a query string, allowing multiple params in arrays
49
- # e.g. :query=>'foo', :graph=>['bar', 'baz']
49
+ # e.g. :query=>'foo', :graph=>['bar', 'baz']
50
50
  # -> query=foo&graph=bar&graph=baz
51
51
  # based on the ActiveSupport implementation, but with different behaviour for arrays
52
52
  def self.to_query hash
@@ -93,13 +93,12 @@ module Tripod::SparqlClient
93
93
  #
94
94
  # @return [ true ]
95
95
  def self.update(sparql)
96
-
97
96
  begin
98
97
  RestClient::Request.execute(
99
98
  :method => :post,
100
99
  :url => Tripod.update_endpoint,
101
100
  :timeout => Tripod.timeout_seconds,
102
- :payload => sparql,
101
+ :payload => { update: sparql }.merge(Tripod.extra_endpoint_params),
103
102
  :headers => {
104
103
  :content_type => 'application/sparql-update'
105
104
  }
@@ -113,4 +112,4 @@ module Tripod::SparqlClient
113
112
 
114
113
  end
115
114
 
116
- end
115
+ end
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.10.10"
2
+ VERSION = "0.10.11"
3
3
  end
@@ -15,6 +15,13 @@ describe Tripod::Finders do
15
15
  b
16
16
  end
17
17
 
18
+ let!(:john) do
19
+ j = Person.new('http://example.com/id/john', 'http://example.com/another_graph')
20
+ j.name = "john"
21
+ j
22
+ end
23
+
24
+
18
25
  describe '.find' do
19
26
 
20
27
  before do
@@ -196,34 +203,65 @@ describe Tripod::Finders do
196
203
 
197
204
  describe '.find_by_sparql' do
198
205
 
199
- before do
200
- # save these into the db
201
- bill.save!
202
- ric.save!
206
+ before { [bill, ric, john].each(&:save!) }
207
+
208
+ context 'given a simple SELECT query, returning uri and graph' do
209
+ let(:query) { 'SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o } }' }
210
+
211
+ it 'returns an array of matching resources' do
212
+ res = Person.find_by_sparql(query)
213
+ res.should =~ [bill, ric, john]
214
+ end
203
215
  end
204
216
 
205
- it 'returns an array of resources which match those in the db' do
206
- res = Person.find_by_sparql('SELECT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o } } ORDER BY ?uri')
207
- res.length.should == 2
208
- res.should include bill
209
- res.should include ric
217
+ context 'given a simple SELECT query, returning uri only' do
218
+ let(:query) { 'SELECT DISTINCT ?uri WHERE { ?uri ?p ?o }' }
210
219
 
211
- r = res.last
212
- r.name.should == "ric"
213
- r.knows.should == [RDF::URI.new("http://example.com/id/bill")]
220
+ it 'returns an array of matching resources' do
221
+ res = Person.find_by_sparql(query)
222
+ res.should =~ [bill, ric, john]
223
+ end
214
224
  end
215
225
 
216
- it 'uses the uri and graph variables if supplied' do
217
- res = Person.find_by_sparql('SELECT ?bob ?geoff WHERE { GRAPH ?geoff { ?bob ?p ?o } }', :uri_variable => 'bob', :graph_variable => 'geoff')
218
- res.length.should == 2
226
+ context 'given a SELECT query and named uri and graph variables' do
227
+ let(:query) { 'SELECT DISTINCT ?x ?y WHERE { GRAPH ?y { ?x ?p ?o } }' }
228
+ let(:opts) { {:uri_variable => 'x', :graph_variable => 'y'} }
229
+
230
+ it 'returns an array of matching resources' do
231
+ res = Person.find_by_sparql(query, opts)
232
+ res.should =~ [bill, ric, john]
233
+ end
219
234
  end
220
235
 
221
- it "returns non-new records" do
222
- res = Person.find_by_sparql('SELECT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o } }')
223
- res.first.new_record?.should be false
236
+ context 'given a SELECT query containing a graph restriction' do
237
+ let(:query) { 'SELECT DISTINCT ?uri ?graph WHERE { GRAPH <http://example.com/graph> { ?uri ?p ?o } }' }
238
+
239
+ it 'only returns matching resources' do
240
+ res = Person.find_by_sparql(query)
241
+ res.should =~ [bill, ric]
242
+ end
224
243
  end
225
244
 
226
- end
245
+ context 'given a SELECT query containing multiple graph restrictions' do
246
+ let(:query) { 'SELECT DISTINCT ?uri ?graph WHERE {
247
+ { GRAPH <http://example.com/graph> { ?uri ?p ?o } }
248
+ UNION
249
+ { GRAPH <http://example.com/another_graph> { ?uri ?p ?o } }
250
+ }' }
251
+
252
+ it "should return matching resources" do
253
+ res = Person.find_by_sparql(query)
254
+ res.should =~ [bill, ric, john]
255
+ end
256
+ end
227
257
 
258
+ context 'given a SELECT query which returns no resources' do
259
+ let(:query) { 'SELECT ?uri WHERE { ?uri ?p ?o . <http://example.com/minotaur> ?p ?o . }' }
228
260
 
261
+ it 'returns an empty array' do
262
+ res = Person.find_by_sparql(query)
263
+ res.empty?.should == true
264
+ end
265
+ end
266
+ end
229
267
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ module Tripod::SparqlClient
4
+ describe Update do
5
+ describe '.update' do
6
+ context 'given a valid SPARQL query' do
7
+ let(:uri) { RDF::URI.new("http://example.com/me") }
8
+ let(:query) { "INSERT DATA { GRAPH <http://example.com/graph/foo> { #{uri.to_base} <http://example.com/hello> \"world\" . } }" }
9
+
10
+ it 'should return true' do
11
+ Update.update(query).should == true
12
+ end
13
+
14
+ it 'should execute the update' do
15
+ Update.update(query)
16
+ Resource.find(uri).should_not be_nil
17
+ end
18
+
19
+ context 'and some additional endpoint params' do
20
+ it 'should include the additional params in the query payload'
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tripod
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.10
4
+ version: 0.10.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ric Roberts
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-02-02 00:00:00.000000000 Z
13
+ date: 2015-02-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
@@ -276,6 +276,7 @@ files:
276
276
  - spec/tripod/repository_spec.rb
277
277
  - spec/tripod/resource_spec.rb
278
278
  - spec/tripod/serialization_spec.rb
279
+ - spec/tripod/sparql_client_spec.rb
279
280
  - spec/tripod/sparql_query_spec.rb
280
281
  - spec/tripod/state_spec.rb
281
282
  - spec/tripod/streaming_spec.rb
@@ -327,6 +328,7 @@ test_files:
327
328
  - spec/tripod/repository_spec.rb
328
329
  - spec/tripod/resource_spec.rb
329
330
  - spec/tripod/serialization_spec.rb
331
+ - spec/tripod/sparql_client_spec.rb
330
332
  - spec/tripod/sparql_query_spec.rb
331
333
  - spec/tripod/state_spec.rb
332
334
  - spec/tripod/streaming_spec.rb