sparql-client 3.0.0 → 3.0.1

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
- SHA1:
3
- metadata.gz: 996cb716d6311ccd569174d200c39d05a035e3b7
4
- data.tar.gz: 23d718dd6a3e23cec61d02df0689d46dd87cf481
2
+ SHA256:
3
+ metadata.gz: b3a059f24774ca03ca4acab2ad37de9edcfbfcc8844ea0da0d7fe3d4b2a90849
4
+ data.tar.gz: 8aa709b83cab4ee290d3513ebb6fd90370d40587b58e8b06351b3df0f32973bb
5
5
  SHA512:
6
- metadata.gz: 2ae85660825933ff2779a03acc8837f3cc82442edd6b5e725e44e2fd4558c346cce7d1bfd8a03d63fbc0ef5bbd4e7a6bfccb626575106d1b256f5a6e36eb8a71
7
- data.tar.gz: a89eaac82c304cc3fc28d6b63c0c3e317728d20d2773262871fd111a2bf4dd8fe3597a87dbcfddf2284975bd12af7183856f9dcc603b2be1178a46e5f189e6db
6
+ metadata.gz: a7abad9a9c21cf0b8d88f9e1bf7ee98cc97ba8fa9a40c9eb06d29293202d16cb10aef0dc9edabe626709230e13511cae7a28af7bc0d48c5ece65ed7030c86022
7
+ data.tar.gz: fe927a99648a455a10494b839d994edae99b755512c69862e1a77418af0aad62d59132e663915ebea6f3f34c0fa6b057f7eab281fbb401bef8be30afecc63c2a
data/README.md CHANGED
@@ -25,70 +25,92 @@ This is a [Ruby][] implementation of a [SPARQL][] client for [RDF.rb][].
25
25
  ## Examples
26
26
 
27
27
  ### Querying a remote SPARQL endpoint
28
- require 'sparql/client'
29
28
 
30
- sparql = SPARQL::Client.new("http://dbpedia.org/sparql")
29
+ ```ruby
30
+ require 'sparql/client'
31
+ sparql = SPARQL::Client.new("http://dbpedia.org/sparql")
32
+ ```
33
+ ### Querying a remote SPARQL endpoint with a specified default graph
31
34
 
32
- ### Querying a `RDF::Repository` instance
35
+ ```ruby
36
+ require 'sparql/client'
37
+ sparql = SPARQL::Client.new("http://dbpedia.org/sparql", { :graph => "http://dbpedia.org" })
38
+ ```
33
39
 
34
- require 'rdf/trig'
35
- repository = RDF::Repository.load("http://example/dataset.trig")
36
40
 
37
- sparql = SPARQL::Client.new(repository)
41
+ ### Querying a `RDF::Repository` instance
38
42
 
39
- ### Executing a boolean query and outputting the result
43
+ ```ruby
44
+ require 'rdf/trig'
45
+ repository = RDF::Repository.load("http://example/dataset.trig")
46
+ sparql = SPARQL::Client.new(repository)
47
+ ```
40
48
 
41
- # ASK WHERE { ?s ?p ?o }
42
- result = sparql.ask.whether([:s, :p, :o]).true?
49
+ ### Executing a boolean query and outputting the result
43
50
 
44
- puts result.inspect #=> true or false
51
+ ```ruby
52
+ # ASK WHERE { ?s ?p ?o }
53
+ result = sparql.ask.whether([:s, :p, :o]).true?
54
+ puts result.inspect #=> true or false
55
+ ```
45
56
 
46
57
  ### Executing a tuple query and iterating over the returned solutions
47
58
 
48
- # SELECT * WHERE { ?s ?p ?o } OFFSET 100 LIMIT 10
49
- query = sparql.select.where([:s, :p, :o]).offset(100).limit(10)
59
+ ```ruby
60
+ # SELECT * WHERE { ?s ?p ?o } OFFSET 100 LIMIT 10
61
+ query = sparql.select.where([:s, :p, :o]).offset(100).limit(10)
50
62
 
51
- query.each_solution do |solution|
52
- puts solution.inspect
53
- end
63
+ query.each_solution do |solution|
64
+ puts solution.inspect
65
+ end
66
+ ```
54
67
 
55
68
  ### Executing a graph query and iterating over the returned statements
56
69
 
57
- # CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o } LIMIT 10
58
- query = sparql.construct([:s, :p, :o]).where([:s, :p, :o]).limit(10)
59
70
 
60
- query.each_statement do |statement|
61
- puts statement.inspect
62
- end
71
+ ```ruby
72
+ # CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o } LIMIT 10
73
+ query = sparql.construct([:s, :p, :o]).where([:s, :p, :o]).limit(10)
74
+
75
+ query.each_statement do |statement|
76
+ puts statement.inspect
77
+ end
78
+ ```
63
79
 
64
80
  ### Executing an arbitrary textual SPARQL query string
65
81
 
66
- result = sparql.query("ASK WHERE { ?s ?p ?o }")
82
+ ```ruby
83
+ result = sparql.query("ASK WHERE { ?s ?p ?o }")
67
84
 
68
- puts result.inspect #=> true or false
85
+ puts result.inspect #=> true or false
86
+ ```
69
87
 
70
88
  ### Inserting data into a graph
71
89
 
72
- # INSERT DATA { <http://example.org/jhacker> <http://xmlns.com/foaf/0.1/name> "J. Random Hacker" .}
73
- data = RDF::Graph.new do |graph|
74
- graph << [RDF::URI('http://example.org/jhacker'), RDF::Vocab::FOAF.name, "J. Random Hacker"]
75
- end
76
- sparql.insert_data(data)
90
+ ```ruby
91
+ # INSERT DATA { <http://example.org/jhacker> <http://xmlns.com/foaf/0.1/name> "J. Random Hacker" .}
92
+ data = RDF::Graph.new do |graph|
93
+ graph << [RDF::URI('http://example.org/jhacker'), RDF::Vocab::FOAF.name, "J. Random Hacker"]
94
+ end
95
+ sparql.insert_data(data)
96
+ ```
77
97
 
78
98
  ### Deleting data from a graph
79
99
 
80
- # DELETE DATA { <http://example.org/jhacker> <http://xmlns.com/foaf/0.1/name> "J. Random Hacker" .}
81
- data = RDF::Graph.new do |graph|
82
- graph << [RDF::URI('http://example.org/jhacker'), RDF::Vocab::FOAF.name, "J. Random Hacker"]
83
- end
84
- sparql.delete_data(data)
100
+ ```ruby
101
+ # DELETE DATA { <http://example.org/jhacker> <http://xmlns.com/foaf/0.1/name> "J. Random Hacker" .}
102
+ data = RDF::Graph.new do |graph|
103
+ graph << [RDF::URI('http://example.org/jhacker'), RDF::Vocab::FOAF.name, "J. Random Hacker"]
104
+ end
105
+ sparql.delete_data(data)
106
+ ```
85
107
 
86
108
  ## Documentation
87
109
 
88
- * {SPARQL::Client}
89
- * {SPARQL::Client::Query}
90
- * {SPARQL::Client::Repository}
91
- * {SPARQL::Client::Update}
110
+ * [SPARQL::Client](https://www.rubydoc.info/github/ruby-rdf/sparql-client/SPARQL/Client)
111
+ * [SPARQL::Client::Query](https://www.rubydoc.info/github/ruby-rdf/sparql-client/SPARQL/Client/Query)
112
+ * [SPARQL::Client::Repository](https://www.rubydoc.info/github/ruby-rdf/sparql-client/SPARQL/Client/Repository)
113
+ * [SPARQL::Client::Update](https://www.rubydoc.info/github/ruby-rdf/sparql-client/SPARQL/Client/Update)
92
114
 
93
115
  ## Dependencies
94
116
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.0.0
1
+ 3.0.1
@@ -40,7 +40,7 @@ module SPARQL
40
40
  '*/*;q=0.1'
41
41
  ].join(', ').freeze
42
42
  GRAPH_ALL = (
43
- RDF::Format.content_types.keys +
43
+ RDF::Format.content_types.keys +
44
44
  ['*/*;q=0.1']
45
45
  ).join(', ').freeze
46
46
 
@@ -543,7 +543,7 @@ module SPARQL
543
543
  if reader = RDF::Reader.for(options)
544
544
  reader.new(response.body)
545
545
  else
546
- raise RDF::ReaderError, "no suitable rdf reader was found."
546
+ raise RDF::ReaderError, "no RDF reader was found for #{options}."
547
547
  end
548
548
  end
549
549
 
@@ -701,8 +701,8 @@ module SPARQL
701
701
  if response.kind_of? Net::HTTPRedirection
702
702
  response = @http.request(::URI.parse(response['location']), request)
703
703
  else
704
- return block_given? ? block.call(response) : response
705
- end
704
+ return block_given? ? block.call(response) : response
705
+ end
706
706
  end
707
707
  raise ServerError, "Infinite redirect at #{url}. Redirected more than 10 times."
708
708
  end
@@ -726,6 +726,7 @@ module SPARQL
726
726
  def make_get_request(query, headers = {})
727
727
  url = self.url.dup
728
728
  url.query_values = (url.query_values || {}).merge(:query => query.to_s)
729
+ set_url_default_graph url unless @options[:graph].nil?
729
730
  request = Net::HTTP::Get.new(url.request_uri, self.headers.merge(headers))
730
731
  request
731
732
  end
@@ -740,23 +741,56 @@ module SPARQL
740
741
  # @see http://www.w3.org/TR/sparql11-protocol/#query-via-post-urlencoded
741
742
  def make_post_request(query, headers = {})
742
743
  if @alt_endpoint.nil?
743
- endpoint = url.request_uri
744
+ url = self.url.dup
745
+ set_url_default_graph url unless @options[:graph].nil?
746
+ endpoint = url.request_uri
744
747
  else
745
748
  endpoint = @alt_endpoint
746
749
  end
750
+
747
751
  request = Net::HTTP::Post.new(endpoint, self.headers.merge(headers))
748
752
  case (self.options[:protocol] || DEFAULT_PROTOCOL).to_s
749
753
  when '1.1'
750
754
  request['Content-Type'] = 'application/sparql-' + (@op || :query).to_s
751
755
  request.body = query.to_s
752
756
  when '1.0'
753
- request.set_form_data((@op || :query) => query.to_s)
757
+ form_data = {(@op || :query) => query.to_s}
758
+ form_data.merge!(
759
+ {:'default-graph-uri' => @options[:graph]}
760
+ ) if !@options[:graph].nil? && (@op.eql? :query)
761
+ form_data.merge!(
762
+ {:'using-graph-uri' => @options[:graph]}
763
+ ) if !@options[:graph].nil? && (@op.eql? :update)
764
+ request.set_form_data(form_data)
754
765
  else
755
766
  raise ArgumentError, "unknown SPARQL protocol version: #{self.options[:protocol].inspect}"
756
767
  end
757
768
  request
758
769
  end
759
770
 
771
+ ##
772
+ # Setup url query parameter to use a specified default graph
773
+ #
774
+ # @see https://www.w3.org/TR/sparql11-protocol/#query-operation
775
+ # @see https://www.w3.org/TR/sparql11-protocol/#update-operation
776
+ def set_url_default_graph url
777
+ if @options[:graph].is_a? Array
778
+ graphs = @options[:graph].map {|graph|
779
+ CGI::escape(graph)
780
+ }
781
+ else
782
+ graphs = CGI::escape(@options[:graph])
783
+ end
784
+ case @op
785
+ when :query
786
+ url.query_values = (url.query_values || {})
787
+ .merge(:'default-graph-uri' => graphs)
788
+ when :update
789
+ url.query_values = (url.query_values || {})
790
+ .merge(:'using-graph-uri' => graphs)
791
+ end
792
+ end
793
+
760
794
  # A query element can be used as a component of a query. It may be initialized with a string, which is wrapped in an appropriate container depending on the type of QueryElement. Implements {#to_s} to property serialize when generating a SPARQL query.
761
795
  class QueryElement
762
796
  attr_reader :elements
@@ -1,4 +1,4 @@
1
- module SPARQL; class Client
1
+ class SPARQL::Client
2
2
  ##
3
3
  # A SPARQL query builder.
4
4
  #
@@ -366,16 +366,38 @@ module SPARQL; class Client
366
366
  end
367
367
 
368
368
  ##
369
- # @example PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT * WHERE \{ ?s ?p ?o . \}
370
- # query.select.
371
- # prefix(dc: RDF::URI("http://purl.org/dc/elements/1.1/")).
372
- # prefix(foaf: RDF::URI("http://xmlns.com/foaf/0.1/")).
373
- # where([:s, :p, :o])
369
+ # @overload prefix(prefix: uri)
370
+ # @example PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT * WHERE \{ ?s ?p ?o . \}
371
+ # query.select.
372
+ # prefix(dc: RDF::URI("http://purl.org/dc/elements/1.1/")).
373
+ # prefix(foaf: RDF::URI("http://xmlns.com/foaf/0.1/")).
374
+ # where([:s, :p, :o])
374
375
  #
375
- # @return [Query]
376
+ # @param [RDF::URI] uri
377
+ # @param [Symbol, String] prefix
378
+ # @return [Query]
379
+ #
380
+ # @overload prefix(string)
381
+ # @example PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT * WHERE \{ ?s ?p ?o . \}
382
+ # query.select.
383
+ # prefix("dc: <http://purl.org/dc/elements/1.1/>").
384
+ # prefix("foaf: <http://xmlns.com/foaf/0.1/>").
385
+ # where([:s, :p, :o])
386
+ #
387
+ # @param [string] string
388
+ # @return [Query]
376
389
  # @see http://www.w3.org/TR/sparql11-query/#prefNames
377
- def prefix(string)
378
- (options[:prefixes] ||= []) << string
390
+ def prefix(val)
391
+ options[:prefixes] ||= []
392
+ if val.kind_of? String
393
+ options[:prefixes] << val
394
+ elsif val.kind_of? Hash
395
+ val.each do |k, v|
396
+ options[:prefixes] << "#{k}: <#{v}>"
397
+ end
398
+ else
399
+ raise ArgumentError, "prefix must be a kind of String or a Hash"
400
+ end
379
401
  self
380
402
  end
381
403
 
@@ -823,4 +845,4 @@ module SPARQL; class Client
823
845
  end
824
846
  end
825
847
  end
826
- end; end
848
+ end
@@ -1,4 +1,4 @@
1
- module SPARQL; class Client
1
+ class SPARQL::Client
2
2
  ##
3
3
  # A read-only repository view of a SPARQL endpoint.
4
4
  #
@@ -345,4 +345,4 @@ module SPARQL; class Client
345
345
  end
346
346
 
347
347
  end
348
- end; end
348
+ end
@@ -1,4 +1,4 @@
1
- module SPARQL; class Client
1
+ class SPARQL::Client
2
2
  module VERSION
3
3
  FILE = File.expand_path('../../../../VERSION', __FILE__)
4
4
  MAJOR, MINOR, TINY, EXTRA = File.read(FILE).chomp.split('.')
@@ -16,4 +16,4 @@ module SPARQL; class Client
16
16
  # @return [Array(Integer, Integer, Integer)]
17
17
  def self.to_a() [MAJOR, MINOR, TINY] end
18
18
  end
19
- end; end
19
+ end
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: 3.0.0
4
+ version: 3.0.1
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: 2017-12-31 00:00:00.000000000 Z
13
+ date: 2018-12-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rdf
@@ -170,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
170
  version: '0'
171
171
  requirements: []
172
172
  rubyforge_project:
173
- rubygems_version: 2.6.14
173
+ rubygems_version: 2.7.6
174
174
  signing_key:
175
175
  specification_version: 4
176
176
  summary: SPARQL client for RDF.rb.