sparql-client 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42303e20768dba6ca714f7203a71ae45de154d41
4
- data.tar.gz: 7c30be238f6ad8752e8f146337c88f88b3d5b8f7
3
+ metadata.gz: 04a17f253c88990873cd961f8d53c71c3634e5e7
4
+ data.tar.gz: 6c8734ce71831639979568cc2a82738f25dc0039
5
5
  SHA512:
6
- metadata.gz: 49ef463d9894b382fe2ace2b28ee68140cc0877c834598f1466076579451b03e79e7c9b2d922ba35365e5501205023b3ec6ef8887cfd0b3f3c9ec13e7f0dccc4
7
- data.tar.gz: 657acc7375b86f3e8d977fb6b5f63f9ce3173115ae9db04f9bd12ed7e958c56b53e4c6b77b90e0a99df038334bea59f4c693a0f2fb21389e1a26d9a564586426
6
+ metadata.gz: 5e45a58edc5c8165e948938e82e6653fb19d9f0ec28f954bc8deba8e299ff816f9c167f344f71a436702273f1daadc703e5edaccf2ba9e57b9d52127a914b9b9
7
+ data.tar.gz: 0b80f8de6e6926d3535b9ed0bbbb63fe86d33e826526f0ffbbdbe4e47337b48e7882f205186f3dfe00c367afae6cbfdfaf147b6213d93950c4c8d55fd29b3fbe
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.1.3
@@ -241,6 +241,12 @@ module SPARQL; class Client
241
241
  self
242
242
  end
243
243
 
244
+ ##
245
+ # @return expects_statements?
246
+ def expects_statements?
247
+ [:construct, :describe].include?(form)
248
+ end
249
+
244
250
  ##
245
251
  # @private
246
252
  def build_patterns(patterns)
@@ -32,8 +32,9 @@ module SPARQL; class Client
32
32
  # @see RDF::Queryable#query
33
33
  # @see RDF::Query#execute
34
34
  def query_execute(query, options = {}, &block)
35
+ return nil unless block_given?
35
36
  q = SPARQL::Client::Query.select(query.variables).where(*query.patterns)
36
- client.query(q, options).each do |solution|
37
+ client.query(q, options).each do |solution|
37
38
  yield solution
38
39
  end
39
40
  end
@@ -252,16 +253,11 @@ module SPARQL; class Client
252
253
  # @return [void]
253
254
  def delete_statements(statements)
254
255
 
255
- constant = true
256
- statements.each do |value|
257
- case
258
- when value.respond_to?(:each_statement)
259
- # needs to be flattened... urgh
260
- nil
261
- when (statement = RDF::Statement.from(value)).constant?
262
- # constant
263
- else
264
- constant = false
256
+ constant = statements.all? do |value|
257
+ # needs to be flattened... urgh
258
+ !value.respond_to?(:each_statement) && begin
259
+ statement = RDF::Statement.from(value)
260
+ statement.constant? && !statement.has_blank_nodes?
265
261
  end
266
262
  end
267
263
 
@@ -36,6 +36,14 @@ class SPARQL::Client
36
36
  end
37
37
  end
38
38
 
39
+ ##
40
+ # Generic Update always returns statements
41
+ #
42
+ # @return expects_statements?
43
+ def expects_statements?
44
+ true
45
+ end
46
+
39
47
  def silent
40
48
  self.options[:silent] = true
41
49
  self
@@ -57,6 +65,14 @@ class SPARQL::Client
57
65
  self
58
66
  end
59
67
 
68
+ ##
69
+ # InsertData always returns result set
70
+ #
71
+ # @return expects_statements?
72
+ def expects_statements?
73
+ false
74
+ end
75
+
60
76
  def to_s
61
77
  query_text = 'INSERT DATA {'
62
78
  query_text += ' GRAPH ' + SPARQL::Client.serialize_uri(self.options[:graph]) + ' {' if self.options[:graph]
@@ -119,19 +135,19 @@ class SPARQL::Client
119
135
  buffer << SPARQL::Client.serialize_uri(self.options[:graph])
120
136
  end
121
137
  if delete_graph and !delete_graph.empty?
122
- serialized_delete = SPARQL::Client.serialize_patterns delete_graph
138
+ serialized_delete = SPARQL::Client.serialize_patterns delete_graph, true
123
139
  buffer << "DELETE {\n"
124
140
  buffer += serialized_delete
125
141
  buffer << "}\n"
126
142
  end
127
143
  if insert_graph and !insert_graph.empty?
128
144
  buffer << "INSERT {\n"
129
- buffer += SPARQL::Client.serialize_patterns insert_graph
145
+ buffer += SPARQL::Client.serialize_patterns insert_graph, true
130
146
  buffer << "}\n"
131
147
  end
132
148
  buffer << "WHERE {\n"
133
149
  if where_graph
134
- buffer += SPARQL::Client.serialize_patterns where_graph
150
+ buffer += SPARQL::Client.serialize_patterns where_graph, true
135
151
  elsif serialized_delete
136
152
  buffer += serialized_delete
137
153
  end
@@ -193,6 +209,14 @@ class SPARQL::Client
193
209
  self
194
210
  end
195
211
 
212
+ ##
213
+ # Clear always returns statements
214
+ #
215
+ # @return expects_statements?
216
+ def expects_statements?
217
+ false
218
+ end
219
+
196
220
  def to_s
197
221
  query_text = 'CLEAR '
198
222
  query_text += 'SILENT ' if self.options[:silent]
data/lib/sparql/client.rb CHANGED
@@ -31,11 +31,26 @@ module SPARQL
31
31
  RESULT_TSV = 'text/tab-separated-values'.freeze
32
32
  RESULT_BOOL = 'text/boolean'.freeze # Sesame-specific
33
33
  RESULT_BRTR = 'application/x-binary-rdf-results-table'.freeze # Sesame-specific
34
- ACCEPT_JSON = {'Accept' => RESULT_JSON}.freeze
35
- ACCEPT_XML = {'Accept' => RESULT_XML}.freeze
36
- ACCEPT_CSV = {'Accept' => RESULT_CSV}.freeze
37
- ACCEPT_TSV = {'Accept' => RESULT_TSV}.freeze
38
- ACCEPT_BRTR = {'Accept' => RESULT_BRTR}.freeze
34
+ RESULT_ALL = [
35
+ RESULT_JSON,
36
+ RESULT_XML,
37
+ RESULT_BOOL,
38
+ "#{RESULT_TSV};p=0.8",
39
+ "#{RESULT_CSV};p=0.2",
40
+ '*/*;p=0.1'
41
+ ].join(', ').freeze
42
+ GRAPH_ALL = (
43
+ RDF::Format.content_types.keys +
44
+ ['*/*;p=0.1']
45
+ ).join(', ').freeze
46
+
47
+ ACCEPT_JSON = {'Accept' => RESULT_JSON}.freeze
48
+ ACCEPT_XML = {'Accept' => RESULT_XML}.freeze
49
+ ACCEPT_CSV = {'Accept' => RESULT_CSV}.freeze
50
+ ACCEPT_TSV = {'Accept' => RESULT_TSV}.freeze
51
+ ACCEPT_BRTR = {'Accept' => RESULT_BRTR}.freeze
52
+ ACCEPT_RESULTS = {'Accept' => RESULT_ALL}.freeze
53
+ ACCEPT_GRAPH = {'Accept' => GRAPH_ALL}.freeze
39
54
 
40
55
  DEFAULT_PROTOCOL = 1.0
41
56
  DEFAULT_METHOD = :post
@@ -78,9 +93,7 @@ module SPARQL
78
93
  @url, @options = url, options.dup
79
94
  else
80
95
  @url, @options = RDF::URI.new(url.to_s), options.dup
81
- @headers = {
82
- 'Accept' => [RESULT_JSON, RESULT_XML, "#{RESULT_TSV};p=0.8", "#{RESULT_CSV};p=0.2", RDF::Format.content_types.keys.map(&:to_s)].join(', ')
83
- }.merge(@options.delete(:headers) || {})
96
+ @headers = @options.delete(:headers) || {}
84
97
  @http = http_klass(@url.scheme)
85
98
  end
86
99
 
@@ -291,12 +304,14 @@ module SPARQL
291
304
  #
292
305
  # @param [String, #to_s] query
293
306
  # @param [Hash{Symbol => Object}] options
307
+ # @option options [String] :endpoint
294
308
  # @option options [String] :content_type
295
309
  # @option options [Hash] :headers
296
310
  # @return [void] `self`
297
311
  # @see http://www.w3.org/TR/sparql11-protocol/#update-operation
298
312
  def update(query, options = {})
299
313
  @op = :update
314
+ @alt_endpoint = options[:endpoint] unless options[:endpoint].nil?
300
315
  case @url
301
316
  when RDF::Queryable
302
317
  require 'sparql' unless defined?(::SPARQL::Grammar)
@@ -322,11 +337,11 @@ module SPARQL
322
337
  request(query, headers) do |response|
323
338
  case response
324
339
  when Net::HTTPBadRequest # 400 Bad Request
325
- raise MalformedQuery.new(response.body)
340
+ raise MalformedQuery.new(response.body + " Processing query #{query}")
326
341
  when Net::HTTPClientError # 4xx
327
- raise ClientError.new(response.body)
342
+ raise ClientError.new(response.body + " Processing query #{query}")
328
343
  when Net::HTTPServerError # 5xx
329
- raise ServerError.new(response.body)
344
+ raise ServerError.new(response.body + " Processing query #{query}")
330
345
  when Net::HTTPSuccess # 2xx
331
346
  response
332
347
  end
@@ -378,6 +393,7 @@ module SPARQL
378
393
  ##
379
394
  # @param [Hash{String => String}] value
380
395
  # @return [RDF::Value]
396
+ # @see http://www.w3.org/TR/sparql11-results-json/#select-encode-terms
381
397
  # @see http://www.w3.org/TR/rdf-sparql-json-res/#variable-binding-results
382
398
  def self.parse_json_value(value, nodes = {})
383
399
  case value['type'].to_sym
@@ -386,7 +402,7 @@ module SPARQL
386
402
  when :uri
387
403
  RDF::URI.new(value['value'])
388
404
  when :literal
389
- RDF::Literal.new(value['value'], :language => value['xml:lang'])
405
+ RDF::Literal.new(value['value'], :datatype => value['datatype'], :language => value['xml:lang'])
390
406
  when :'typed-literal'
391
407
  RDF::Literal.new(value['value'], :datatype => value['datatype'])
392
408
  else nil
@@ -538,14 +554,16 @@ module SPARQL
538
554
  # Serializes an `RDF::Value` into SPARQL syntax.
539
555
  #
540
556
  # @param [RDF::Value] value
557
+ # @param [Boolean] use_vars (false) Use variables in place of BNodes
541
558
  # @return [String]
542
559
  # @private
543
- def self.serialize_value(value)
560
+ def self.serialize_value(value, use_vars = false)
544
561
  # SPARQL queries are UTF-8, but support ASCII-style Unicode escapes, so
545
562
  # the N-Triples serializer is fine unless it's a variable:
546
563
  case
547
- when value.nil? then RDF::Query::Variable.new.to_s
564
+ when value.nil? then RDF::Query::Variable.new.to_s
548
565
  when value.variable? then value.to_s
566
+ when value.node? then (use_vars ? RDF::Query::Variable.new(value.id) : value)
549
567
  else RDF::NTriples.serialize(value)
550
568
  end
551
569
  end
@@ -559,6 +577,8 @@ module SPARQL
559
577
  # @private
560
578
  def self.serialize_predicate(value,rdepth=0)
561
579
  case value
580
+ when nil
581
+ RDF::Query::Variable.new.to_s
562
582
  when String then value
563
583
  when Array
564
584
  s = value.map{|v|serialize_predicate(v,rdepth+1)}.join
@@ -573,15 +593,16 @@ module SPARQL
573
593
  # Serializes a SPARQL graph
574
594
  #
575
595
  # @param [RDF::Enumerable] patterns
596
+ # @param [Boolean] use_vars (false) Use variables in place of BNodes
576
597
  # @return [String]
577
598
  # @private
578
- def self.serialize_patterns(patterns)
599
+ def self.serialize_patterns(patterns, use_vars = false)
579
600
  patterns.map do |pattern|
580
601
  serialized_pattern = RDF::Statement.from(pattern).to_triple.each_with_index.map do |v, i|
581
602
  if i == 1
582
603
  SPARQL::Client.serialize_predicate(v)
583
604
  else
584
- SPARQL::Client.serialize_value(v)
605
+ SPARQL::Client.serialize_value(v, use_vars)
585
606
  end
586
607
  end
587
608
  serialized_pattern.join(' ') + ' .'
@@ -639,6 +660,16 @@ module SPARQL
639
660
  # @see http://www.w3.org/TR/sparql11-protocol/#query-operation
640
661
  def request(query, headers = {}, &block)
641
662
  method = (self.options[:method] || DEFAULT_METHOD).to_sym
663
+
664
+ # Make sure an appropriate Accept header is present
665
+ headers['Accept'] ||= if (query.respond_to?(:expects_statements?) ?
666
+ query.expects_statements? :
667
+ (query =~ /CONSTRUCT|DESCRIBE|DELETE|CLEAR/))
668
+ GRAPH_ALL
669
+ else
670
+ RESULT_ALL
671
+ end
672
+
642
673
  request = send("make_#{method}_request", query, headers)
643
674
 
644
675
  request.basic_auth(url.user, url.password) if url.user && !url.user.empty?
@@ -674,7 +705,12 @@ module SPARQL
674
705
  # @see http://www.w3.org/TR/sparql11-protocol/#query-via-post-direct
675
706
  # @see http://www.w3.org/TR/sparql11-protocol/#query-via-post-urlencoded
676
707
  def make_post_request(query, headers = {})
677
- request = Net::HTTP::Post.new(self.url.request_uri, self.headers.merge(headers))
708
+ if @alt_endpoint.nil?
709
+ endpoint = url.request_uri
710
+ else
711
+ endpoint = @alt_endpoint
712
+ end
713
+ request = Net::HTTP::Post.new(endpoint, self.headers.merge(headers))
678
714
  case (self.options[:protocol] || DEFAULT_PROTOCOL).to_s
679
715
  when '1.1'
680
716
  request['Content-Type'] = 'application/sparql-' + (@op || :query).to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparql-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-06-30 00:00:00.000000000 Z
13
+ date: 2014-08-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rdf