tripod 0.10.12 → 0.11.0

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: 9566499135930792bfe25a0ff05af6a4cf8f2dc2
4
- data.tar.gz: 3f8f7e3ab314f6d0dcc3ea2fdd914e2f0f05de2d
3
+ metadata.gz: 531f10bb018c574b829130163c072bf92da17bb5
4
+ data.tar.gz: 8d975d11b7b3a4e0d860910871e760b5bc6266ec
5
5
  SHA512:
6
- metadata.gz: 25efd029784318c71f547eecfcce2217a1223a74fa42e8ae2f2d45aa17c817e58314d78df7a6de9221bf1dc138b132e300f1372137a2f8d49f91def0278b8ca8
7
- data.tar.gz: 160760d6ac66d06f41de25de2836fb47ace3405881ca0e32a9b70bc01295f235aa6d7dbb0f1ac060b67b02c55fbbcacc396ceb54cc13183374e43b38cd87172a
6
+ metadata.gz: f25fe33e76090c1002bacf050eab8e47cd3b223d3547e21f8d60b1204464987623926e32a8b890a083aabcae5418844a83f057fe72640573df94f6cb5fb98a78
7
+ data.tar.gz: 5ef2a69e11203c68ce7f0a3b0b9406377614eed24f9e20b3c6a4320c5d009d511bfd33b3891f755b8f47ef1e53192d10fb5f79cd26eac8b12c40516569c0b3cb
@@ -42,7 +42,7 @@ module Tripod
42
42
  # e.g. my_criteria.where("?uri a <http://my-type>")
43
43
  #
44
44
  def where(filter)
45
- if filter.is_a?(String) # we gotta Sparql snippet
45
+ if filter.is_a?(String) # we got a Sparql snippet
46
46
  where_clauses << filter
47
47
  elsif filter.is_a?(Hash)
48
48
  filter.each_pair do |key, value|
@@ -54,6 +54,9 @@ module Tripod
54
54
  self
55
55
  end
56
56
 
57
+ def query_where_clauses
58
+ where_clauses.empty? ? ['?uri ?p ?o'] : where_clauses
59
+ end
57
60
  # takes a string and adds an extra clause to this criteria.
58
61
  # e.g. my_criteria.extras("LIMIT 10 OFFSET 20").extrass
59
62
  #
@@ -44,7 +44,12 @@ module Tripod
44
44
  sq = Tripod::SparqlQuery.new(self.as_query(opts))
45
45
  count_sparql = sq.as_count_query_str
46
46
  result = Tripod::SparqlClient::Query.select(count_sparql)
47
- result[0]["tripod_count_var"]["value"].to_i
47
+
48
+ if result.length > 0
49
+ result[0]["tripod_count_var"]["value"].to_i
50
+ else
51
+ return 0
52
+ end
48
53
  end
49
54
 
50
55
  # turn this criteria into a query
@@ -70,7 +75,7 @@ module Tripod
70
75
  select_query += "GRAPH <#{graph_uri}> { " if graph_uri
71
76
  end
72
77
 
73
- select_query += self.where_clauses.join(" . ")
78
+ select_query += self.query_where_clauses.join(" . ")
74
79
  select_query += " } "
75
80
  select_query += "} " if return_graph || graph_uri # close the graph clause
76
81
  select_query += self.extra_clauses.join(" ")
@@ -41,7 +41,7 @@ module Tripod::Finders
41
41
  graph_uri ||= self.get_graph_uri
42
42
  unless graph_uri
43
43
  # do a quick select to see what graph to use.
44
- select_query = "SELECT ?g WHERE { GRAPH ?g {<#{uri.to_s}> ?p ?o } } LIMIT 1"
44
+ select_query = "SELECT * WHERE { GRAPH ?g {<#{uri.to_s}> ?p ?o } } LIMIT 1"
45
45
  result = Tripod::SparqlClient::Query.select(select_query)
46
46
  if result.length > 0
47
47
  graph_uri = result[0]["g"]["value"]
@@ -90,7 +90,7 @@ module Tripod::Finders
90
90
  # execute a query to return all objects (restricted by this class's rdf_type if specified)
91
91
  # returns a criteria object
92
92
  def all
93
- where('?uri ?p ?o')
93
+ Tripod::Criteria.new(self)
94
94
  end
95
95
 
96
96
  def count
@@ -161,9 +161,15 @@ 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
- construct_query = _construct_query_for_uris_and_graphs(uris_and_graphs)
165
- graph = _graph_of_triples_from_construct_or_describe(construct_query)
166
- _resources_from_graph(graph, uris_and_graphs)
164
+
165
+ #there are no resources if there are no uris and graphs
166
+ if uris_and_graphs.empty?
167
+ []
168
+ else
169
+ construct_query = _construct_query_for_uris_and_graphs(uris_and_graphs)
170
+ graph = _graph_of_triples_from_construct_or_describe(construct_query)
171
+ _resources_from_graph(graph, uris_and_graphs)
172
+ end
167
173
  end
168
174
 
169
175
  # For a select query, generate a query which DESCRIBES all the results
@@ -250,10 +256,12 @@ module Tripod::Finders
250
256
  # @option options [ String ] graph_variable The name of the uri variable in thh query, if not 'graph'
251
257
  def _select_uris_and_graphs(sparql, opts={})
252
258
  select_results = Tripod::SparqlClient::Query.select(sparql)
253
-
259
+
254
260
  uri_variable = opts[:uri_variable] || 'uri'
255
261
  graph_variable = opts[:graph_variable] || 'graph'
256
262
 
263
+ return [] unless select_results.select{|r| r.keys.length > 0 }.any?
264
+
257
265
  select_results.reduce([]) do |memo, result|
258
266
  u = result[uri_variable]['value']
259
267
  g = result[graph_variable]['value'] if result[graph_variable]
data/lib/tripod/graphs.rb CHANGED
@@ -7,6 +7,11 @@ module Tripod::Graphs
7
7
  def graphs
8
8
  select_query = "SELECT DISTINCT ?g WHERE { GRAPH ?g {<#{uri.to_s}> ?p ?o } }"
9
9
  result = Tripod::SparqlClient::Query.select(select_query)
10
- result.map{|r| r["g"]["value"]}
10
+
11
+ if result.length > 0
12
+ result.select{|r| r.keys.length > 0 }.map{|r| r["g"]["value"] }
13
+ else
14
+ []
15
+ end
11
16
  end
12
17
  end
@@ -69,7 +69,11 @@ module Tripod::SparqlClient
69
69
  # @return [ Hash, String ]
70
70
  def self.select(query)
71
71
  query_response = self.query(query, "application/sparql-results+json")
72
- JSON.parse(query_response)["results"]["bindings"]
72
+ if query_response.length >0
73
+ JSON.parse(query_response)["results"]["bindings"]
74
+ else
75
+ []
76
+ end
73
77
  end
74
78
 
75
79
  def self._response_limit_options(response_limit_bytes)
@@ -1,3 +1,3 @@
1
1
  module Tripod
2
- VERSION = "0.10.12"
2
+ VERSION = "0.11.0"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -16,12 +16,15 @@ RSpec.configure do |config|
16
16
  config.before(:each) do
17
17
  WebMock.disable!
18
18
  # delete from all graphs.
19
- Tripod::SparqlClient::Update.update('
20
- # delete from default graph:
21
- DELETE {?s ?p ?o} WHERE {?s ?p ?o};
22
- # delete from named graphs:
23
- DELETE {graph ?g {?s ?p ?o}} WHERE {graph ?g {?s ?p ?o}};
24
- ')
19
+ # delete from default graph:
20
+ # Tripod::SparqlClient::Update.update('
21
+ # DELETE {?s ?p ?o} WHERE {?s ?p ?o}')
22
+ # # delete from named graphs:
23
+ # Tripod::SparqlClient::Update.update('
24
+ # DELETE {graph ?g {?s ?p ?o}} WHERE {graph ?g {?s ?p ?o}};
25
+ # ')
26
+ Tripod::SparqlClient::Update.update('drop default')
27
+ Tripod::SparqlClient::Update.update('drop all')
25
28
  end
26
29
 
27
30
  end
@@ -28,12 +28,12 @@ describe Tripod::Criteria do
28
28
 
29
29
  context "for a class with an rdf_type and graph" do
30
30
  it "should return a SELECT query based with an rdf type restriction" do
31
- person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . ?uri ?p ?o } }"
31
+ person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> } }"
32
32
  end
33
33
 
34
34
  context "with include_graph option set to false" do
35
35
  it "should not select graphs, but restrict to graph" do
36
- person_criteria.as_query(:return_graph => false).should == "SELECT DISTINCT ?uri WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . ?uri ?p ?o } }"
36
+ person_criteria.as_query(:return_graph => false).should == "SELECT DISTINCT ?uri WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> } }"
37
37
  end
38
38
  end
39
39
 
@@ -41,7 +41,7 @@ describe Tripod::Criteria do
41
41
  before { person_criteria.where("[pattern]") }
42
42
 
43
43
  it "should return a SELECT query with the extra restriction" do
44
- person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . ?uri ?p ?o . [pattern] } }"
44
+ person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . [pattern] } }"
45
45
  end
46
46
  end
47
47
 
@@ -49,7 +49,7 @@ describe Tripod::Criteria do
49
49
  before { person_criteria.graph("http://example.com/anothergraph") }
50
50
 
51
51
  it "should override the graph in the query" do
52
- person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/anothergraph> as ?graph) WHERE { GRAPH <http://example.com/anothergraph> { ?uri a <http://example.com/person> . ?uri ?p ?o } }"
52
+ person_criteria.as_query.should == "SELECT DISTINCT ?uri (<http://example.com/anothergraph> as ?graph) WHERE { GRAPH <http://example.com/anothergraph> { ?uri a <http://example.com/person> } }"
53
53
  end
54
54
  end
55
55
  end
@@ -66,10 +66,10 @@ describe Tripod::Criteria do
66
66
  end
67
67
 
68
68
  context "and extra restrictions" do
69
- before { resource_criteria.where("[pattern]") }
69
+ before { resource_criteria.where("?uri a <http://type>") }
70
70
 
71
71
  it "should return a SELECT query with the extra restrictions" do
72
- resource_criteria.as_query.should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o . [pattern] } }"
72
+ resource_criteria.as_query.should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri a <http://type> } }"
73
73
  end
74
74
  end
75
75
 
@@ -84,10 +84,10 @@ describe Tripod::Criteria do
84
84
 
85
85
  context "with extras" do
86
86
 
87
- before { resource_criteria.where("[pattern]").extras("LIMIT 10").extras("OFFSET 20") }
87
+ before { resource_criteria.where("?uri a <http://type>").extras("LIMIT 10").extras("OFFSET 20") }
88
88
 
89
89
  it "should add the extras on the end" do
90
- resource_criteria.as_query.should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri ?p ?o . [pattern] } } LIMIT 10 OFFSET 20"
90
+ resource_criteria.as_query.should == "SELECT DISTINCT ?uri ?graph WHERE { GRAPH ?graph { ?uri a <http://type> } } LIMIT 10 OFFSET 20"
91
91
  end
92
92
  end
93
93
  end
@@ -191,7 +191,7 @@ describe Tripod::Criteria do
191
191
 
192
192
  it "should execute the right Sparql" do
193
193
  sparql = "SELECT (COUNT(*) as ?tripod_count_var) {
194
- SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> . ?uri ?p ?o } } LIMIT 10 OFFSET 20
194
+ SELECT DISTINCT ?uri (<http://example.com/graph> as ?graph) WHERE { GRAPH <http://example.com/graph> { ?uri a <http://example.com/person> } } LIMIT 10 OFFSET 20
195
195
  }"
196
196
  Tripod::SparqlClient::Query.should_receive(:select).with(sparql).and_call_original
197
197
  Person.all.limit(10).offset(20).count
@@ -18,13 +18,13 @@ describe Tripod::Criteria do
18
18
 
19
19
  context "with rdf_type set on the class" do
20
20
  it "should initialize the where clauses to include a type restriction" do
21
- person_criteria.where_clauses.should == ["?uri a <http://example.com/person>", "?uri ?p ?o"]
21
+ person_criteria.where_clauses.should == ["?uri a <http://example.com/person>"]
22
22
  end
23
23
  end
24
24
 
25
25
  context "with no rdf_type set on the class" do
26
26
  it "should initialize the where clauses to ?uri ?p ?o" do
27
- resource_criteria.where_clauses.should == ["?uri ?p ?o"]
27
+ resource_criteria.where_clauses.should == []
28
28
  end
29
29
  end
30
30
  end
@@ -34,7 +34,7 @@ describe Tripod::Criteria do
34
34
  context 'given a string' do
35
35
  it "should add the sparql snippet to the where clauses" do
36
36
  resource_criteria.where("blah")
37
- resource_criteria.where_clauses.should == ["?uri ?p ?o", "blah"]
37
+ resource_criteria.where_clauses.should == ["blah"]
38
38
  end
39
39
 
40
40
  it "should return an instance of Criteria" do
@@ -42,7 +42,7 @@ describe Tripod::Criteria do
42
42
  end
43
43
 
44
44
  it "should return an instance of Criteria with the where clauses added" do
45
- resource_criteria.where("blah").where_clauses.should == ["?uri ?p ?o", "blah"]
45
+ resource_criteria.where("blah").where_clauses.should == ["blah"]
46
46
  end
47
47
  end
48
48
 
@@ -52,7 +52,7 @@ describe Tripod::Criteria do
52
52
 
53
53
  it 'should construct a sparql snippet with the appropriate predicate, treating the value as a literal' do
54
54
  criteria = resource_criteria.where(label: value)
55
- criteria.where_clauses[1].should == "?uri <#{ RDF::RDFS.label }> \"#{ value }\""
55
+ criteria.where_clauses[0].should == "?uri <#{ RDF::RDFS.label }> \"#{ value }\""
56
56
  end
57
57
  end
58
58
 
@@ -61,7 +61,7 @@ describe Tripod::Criteria do
61
61
 
62
62
  it 'should construct a sparql snippet with the appropriate predicate' do
63
63
  criteria = resource_criteria.where(label: value)
64
- criteria.where_clauses[1].should == "?uri <#{ RDF::RDFS.label }> <#{ value.to_s }>"
64
+ criteria.where_clauses[0].should == "?uri <#{ RDF::RDFS.label }> <#{ value.to_s }>"
65
65
  end
66
66
  end
67
67
  end
@@ -146,8 +146,8 @@ describe Tripod::Finders do
146
146
  end
147
147
 
148
148
  describe ".all" do
149
- it "should make and return a new criteria for the current class, with a where clause of ?uri ?p ?o already started" do
150
- Person.all.should == Tripod::Criteria.new(Person).where("?uri ?p ?o")
149
+ it "should make and return a new criteria for the current class" do
150
+ Person.all.should == Tripod::Criteria.new(Person)
151
151
  end
152
152
  end
153
153
 
@@ -106,8 +106,7 @@ describe Tripod::Links do
106
106
 
107
107
  rover.friends.length.should == 2
108
108
 
109
- rover.friends.to_a.first.uri.should == fido.uri
110
- rover.friends.to_a.last.uri.should == spot.uri
109
+ rover.friends.map {|f| f.uri}.should =~ [fido.uri, spot.uri]
111
110
  end
112
111
 
113
112
  it "creates field getters and setters with the _uris suffix" do
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.12
4
+ version: 0.11.0
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-04-22 00:00:00.000000000 Z
13
+ date: 2015-06-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client