sparql-client 0.3.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -64,9 +64,9 @@ This is a [Ruby][] implementation of a [SPARQL][] client for [RDF.rb][].
64
64
  ##Dependencies
65
65
 
66
66
  * [Ruby](http://ruby-lang.org/) (>= 1.8.7) or (>= 1.8.1 with [Backports][])
67
- * [RDF.rb](http://rubygems.org/gems/rdf) (>= 0.3.5)
68
- * [Net::HTTP::Persistent](http://rubygems.org/gems/net-http-persistent) (>= 1.4.1)
69
- * [JSON](http://rubygems.org/gems/json_pure) (>= 1.4.6)
67
+ * [RDF.rb](http://rubygems.org/gems/rdf) (>= 1.0)
68
+ * [Net::HTTP::Persistent](http://rubygems.org/gems/net-http-persistent) (>= 1.4)
69
+ * [JSON](http://rubygems.org/gems/json_pure) (>= 1.4)
70
70
 
71
71
  ##Installation
72
72
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 1.0.0
@@ -4,13 +4,15 @@ require 'rdf/ntriples' # @see http://rubygems.org/gems/rdf
4
4
 
5
5
  module SPARQL
6
6
  ##
7
- # A SPARQL client for RDF.rb.
7
+ # A SPARQL 1.0/1.1 client for RDF.rb.
8
8
  #
9
9
  # @see http://www.w3.org/TR/sparql11-query/
10
+ # @see http://www.w3.org/TR/sparql11-protocol/
10
11
  # @see http://www.w3.org/TR/sparql11-results-json/
11
12
  class Client
12
13
  autoload :Query, 'sparql/client/query'
13
14
  autoload :Repository, 'sparql/client/repository'
15
+ autoload :Update, 'sparql/client/update'
14
16
  autoload :VERSION, 'sparql/client/version'
15
17
 
16
18
  class ClientError < StandardError; end
@@ -25,19 +27,32 @@ module SPARQL
25
27
  ACCEPT_XML = {'Accept' => RESULT_XML}.freeze
26
28
  ACCEPT_BRTR = {'Accept' => RESULT_BRTR}.freeze
27
29
 
30
+ DEFAULT_PROTOCOL = 1.0
31
+ DEFAULT_METHOD = :post
32
+
33
+ ##
34
+ # The SPARQL endpoint URL.
35
+ #
28
36
  # @return [RDF::URI]
29
37
  attr_reader :url
30
38
 
39
+ ##
40
+ # The HTTP headers that will be sent in requests to the endpoint.
41
+ #
31
42
  # @return [Hash{String => String}]
32
43
  attr_reader :headers
33
44
 
45
+ ##
46
+ # Any miscellaneous configuration.
47
+ #
34
48
  # @return [Hash{Symbol => Object}]
35
49
  attr_reader :options
36
50
 
37
51
  ##
38
52
  # @param [String, #to_s] url
39
53
  # @param [Hash{Symbol => Object}] options
40
- # @option options [Symbol] :method (:post)
54
+ # @option options [Symbol] :method (DEFAULT_METHOD)
55
+ # @option options [Number] :protocol (DEFAULT_PROTOCOL)
41
56
  # @option options [Hash] :headers
42
57
  def initialize(url, options = {}, &block)
43
58
  @url, @options = RDF::URI.new(url.to_s), options.dup
@@ -117,15 +132,7 @@ module SPARQL
117
132
  # @return [void] `self`
118
133
  # @see http://www.w3.org/TR/sparql11-update/#insertData
119
134
  def insert_data(data, options = {})
120
- raise ArgumentError, "no data given" if data.empty?
121
- query_text = 'INSERT DATA {'
122
- query_text += ' GRAPH ' + self.class.serialize_uri(options[:graph]) + ' {' if options[:graph]
123
- query_text += "\n"
124
- query_text += RDF::NTriples::Writer.buffer { |writer| writer << data }
125
- query_text += '}' if options[:graph]
126
- query_text += "}\n"
127
- query(query_text)
128
- self
135
+ self.update(Update::InsertData.new(data, options))
129
136
  end
130
137
 
131
138
  ##
@@ -146,15 +153,7 @@ module SPARQL
146
153
  # @return [void] `self`
147
154
  # @see http://www.w3.org/TR/sparql11-update/#deleteData
148
155
  def delete_data(data, options = {})
149
- raise ArgumentError, "no data given" if data.empty?
150
- query_text = 'DELETE DATA {'
151
- query_text += ' GRAPH ' + self.class.serialize_uri(options[:graph]) + ' {' if options[:graph]
152
- query_text += "\n"
153
- query_text += RDF::NTriples::Writer.buffer { |writer| writer << data }
154
- query_text += '}' if options[:graph]
155
- query_text += "}\n"
156
- query(query_text)
157
- self
156
+ self.update(Update::DeleteData.new(data, options))
158
157
  end
159
158
 
160
159
  ##
@@ -197,18 +196,7 @@ module SPARQL
197
196
  # @return [void] `self`
198
197
  # @see http://www.w3.org/TR/sparql11-update/#clear
199
198
  def clear(what, *arguments)
200
- options = arguments.last.is_a?(Hash) ? arguments.pop : {}
201
- query_text = 'CLEAR '
202
- query_text += 'SILENT ' if options[:silent]
203
- case what.to_sym
204
- when :graph then query_text += 'GRAPH ' + self.class.serialize_uri(arguments.pop)
205
- when :default then query_text += 'DEFAULT'
206
- when :named then query_text += 'NAMED'
207
- when :all then query_text += 'ALL'
208
- else raise ArgumentError, "invalid CLEAR operation: #{what.inspect}"
209
- end
210
- query(query_text)
211
- self
199
+ self.update(Update::Clear.new(what, *arguments))
212
200
  end
213
201
 
214
202
  ##
@@ -231,17 +219,34 @@ module SPARQL
231
219
  end
232
220
 
233
221
  ##
234
- # Executes a SPARQL query and returns parsed results.
222
+ # Executes a SPARQL query and returns the parsed results.
235
223
  #
236
224
  # @param [String, #to_s] query
237
225
  # @param [Hash{Symbol => Object}] options
238
226
  # @option options [String] :content_type
239
227
  # @option options [Hash] :headers
240
228
  # @return [Array<RDF::Query::Solution>]
229
+ # @see http://www.w3.org/TR/sparql11-protocol/#query-operation
241
230
  def query(query, options = {})
231
+ @op = :query
242
232
  parse_response(response(query, options), options)
243
233
  end
244
234
 
235
+ ##
236
+ # Executes a SPARQL update operation.
237
+ #
238
+ # @param [String, #to_s] query
239
+ # @param [Hash{Symbol => Object}] options
240
+ # @option options [String] :content_type
241
+ # @option options [Hash] :headers
242
+ # @return [void] `self`
243
+ # @see http://www.w3.org/TR/sparql11-protocol/#update-operation
244
+ def update(query, options = {})
245
+ @op = :update
246
+ parse_response(response(query, options), options)
247
+ self
248
+ end
249
+
245
250
  ##
246
251
  # Executes a SPARQL query and returns the Net::HTTP::Response of the
247
252
  # result.
@@ -458,7 +463,7 @@ module SPARQL
458
463
  # @return [Net::HTTPResponse]
459
464
  # @see http://www.w3.org/TR/sparql11-protocol/#query-operation
460
465
  def request(query, headers = {}, &block)
461
- method = (self.options[:method] || :post).to_sym
466
+ method = (self.options[:method] || DEFAULT_METHOD).to_sym
462
467
  request = send("make_#{method}_request", query, headers)
463
468
 
464
469
  request.basic_auth(url.user, url.password) if url.user && !url.user.empty?
@@ -491,11 +496,19 @@ module SPARQL
491
496
  # @param [String, #to_s] query
492
497
  # @param [Hash{String => String}] headers
493
498
  # @return [Net::HTTPRequest]
499
+ # @see http://www.w3.org/TR/sparql11-protocol/#query-via-post-direct
494
500
  # @see http://www.w3.org/TR/sparql11-protocol/#query-via-post-urlencoded
495
501
  def make_post_request(query, headers = {})
496
- url = self.url
497
- request = Net::HTTP::Post.new(url.request_uri, self.headers.merge(headers))
498
- request.set_form_data(:query => query.to_s)
502
+ request = Net::HTTP::Post.new(self.url.request_uri, self.headers.merge(headers))
503
+ case (self.options[:protocol] || DEFAULT_PROTOCOL).to_s
504
+ when '1.1'
505
+ request['Content-Type'] = 'application/sparql-' + (@op || :query).to_s
506
+ request.body = query.to_s
507
+ when '1.0'
508
+ request.set_form_data(:query => query.to_s)
509
+ else
510
+ raise ArgumentError, "unknown SPARQL protocol version: #{self.options[:protocol].inspect}"
511
+ end
499
512
  request
500
513
  end
501
514
  end # Client
@@ -0,0 +1,228 @@
1
+ class SPARQL::Client
2
+ ##
3
+ # SPARQL 1.1 Update operation builders.
4
+ module Update
5
+ def self.insert_data(*arguments)
6
+ InsertData.new(*arguments)
7
+ end
8
+
9
+ def self.delete_data(*arguments)
10
+ DeleteData.new(*arguments)
11
+ end
12
+
13
+ def self.load(*arguments)
14
+ Load.new(*arguments)
15
+ end
16
+
17
+ def self.clear(*arguments)
18
+ Clear.new(*arguments)
19
+ end
20
+
21
+ def self.create(*arguments)
22
+ Create.new(*arguments)
23
+ end
24
+
25
+ def self.drop(*arguments)
26
+ Drop.new(*arguments)
27
+ end
28
+
29
+ class Operation
30
+ attr_reader :options
31
+
32
+ def initialize(*arguments)
33
+ @options = arguments.last.is_a?(Hash) ? arguments.pop.dup : {}
34
+ unless arguments.empty?
35
+ send(arguments.shift, *arguments)
36
+ end
37
+ end
38
+
39
+ def silent
40
+ self.options[:silent] = true
41
+ self
42
+ end
43
+ end
44
+
45
+ ##
46
+ # @see http://www.w3.org/TR/sparql11-update/#insertData
47
+ class InsertData < Operation
48
+ attr_reader :data
49
+
50
+ def initialize(data, options = {})
51
+ @data = data
52
+ super(options)
53
+ end
54
+
55
+ def graph(uri)
56
+ self.options[:graph] = uri
57
+ self
58
+ end
59
+
60
+ def to_s
61
+ query_text = 'INSERT DATA {'
62
+ query_text += ' GRAPH ' + SPARQL::Client.serialize_uri(self.options[:graph]) + ' {' if self.options[:graph]
63
+ query_text += "\n"
64
+ query_text += RDF::NTriples::Writer.buffer { |writer| writer << @data }
65
+ query_text += '}' if self.options[:graph]
66
+ query_text += "}\n"
67
+ end
68
+ end
69
+
70
+ ##
71
+ # @see http://www.w3.org/TR/sparql11-update/#deleteData
72
+ class DeleteData < Operation
73
+ attr_reader :data
74
+
75
+ def initialize(data, options = {})
76
+ @data = data
77
+ super(options)
78
+ end
79
+
80
+ def graph(uri)
81
+ self.options[:graph] = uri
82
+ self
83
+ end
84
+
85
+ def to_s
86
+ query_text = 'DELETE DATA {'
87
+ query_text += ' GRAPH ' + SPARQL::Client.serialize_uri(self.options[:graph]) + ' {' if self.options[:graph]
88
+ query_text += "\n"
89
+ query_text += RDF::NTriples::Writer.buffer { |writer| writer << @data }
90
+ query_text += '}' if self.options[:graph]
91
+ query_text += "}\n"
92
+ end
93
+ end
94
+
95
+ ##
96
+ # @see http://www.w3.org/TR/sparql11-update/#deleteInsert
97
+ class DeleteInsert < Operation
98
+ def to_s
99
+ # TODO
100
+ end
101
+ end
102
+
103
+ ##
104
+ # @see http://www.w3.org/TR/sparql11-update/#load
105
+ class Load < Operation
106
+ attr_reader :from
107
+ attr_reader :into
108
+
109
+ def initialize(from, options = {})
110
+ options = options.dup
111
+ @from = RDF::URI(from)
112
+ @into = RDF::URI(options.delete(:into)) if options[:into]
113
+ super(options)
114
+ end
115
+
116
+ def into(url)
117
+ @into = RDF::URI(url)
118
+ self
119
+ end
120
+
121
+ def to_s
122
+ query_text = 'LOAD '
123
+ query_text += 'SILENT ' if self.options[:silent]
124
+ query_text += SPARQL::Client.serialize_uri(@from)
125
+ query_text += ' INTO GRAPH ' + SPARQL::Client.serialize_uri(@into) if @into
126
+ query_text
127
+ end
128
+ end
129
+
130
+ ##
131
+ # @see http://www.w3.org/TR/sparql11-update/#clear
132
+ class Clear < Operation
133
+ attr_reader :uri
134
+
135
+ def graph(uri)
136
+ @what, @uri = :graph, uri
137
+ self
138
+ end
139
+
140
+ def default
141
+ @what = :default
142
+ self
143
+ end
144
+
145
+ def named
146
+ @what = :named
147
+ self
148
+ end
149
+
150
+ def all
151
+ @what = :all
152
+ self
153
+ end
154
+
155
+ def to_s
156
+ query_text = 'CLEAR '
157
+ query_text += 'SILENT ' if self.options[:silent]
158
+ case @what.to_sym
159
+ when :graph then query_text += 'GRAPH ' + SPARQL::Client.serialize_uri(@uri)
160
+ when :default then query_text += 'DEFAULT'
161
+ when :named then query_text += 'NAMED'
162
+ when :all then query_text += 'ALL'
163
+ else raise ArgumentError, "invalid CLEAR operation: #{@what.inspect}"
164
+ end
165
+ query_text
166
+ end
167
+ end
168
+
169
+ ##
170
+ # @see http://www.w3.org/TR/sparql11-update/#create
171
+ class Create < Operation
172
+ attr_reader :uri
173
+
174
+ def initialize(uri, options = {})
175
+ @uri = RDF::URI(uri)
176
+ super(options)
177
+ end
178
+
179
+ def to_s
180
+ query_text = 'CREATE '
181
+ query_text += 'SILENT ' if self.options[:silent]
182
+ query_text += 'GRAPH ' + SPARQL::Client.serialize_uri(@uri)
183
+ query_text
184
+ end
185
+ end
186
+
187
+ ##
188
+ # @see http://www.w3.org/TR/sparql11-update/#drop
189
+ class Drop < Clear
190
+ def to_s
191
+ query_text = 'DROP '
192
+ query_text += 'SILENT ' if self.options[:silent]
193
+ case @what.to_sym
194
+ when :graph then query_text += 'GRAPH ' + SPARQL::Client.serialize_uri(@uri)
195
+ when :default then query_text += 'DEFAULT'
196
+ when :named then query_text += 'NAMED'
197
+ when :all then query_text += 'ALL'
198
+ else raise ArgumentError, "invalid DROP operation: #{@what.inspect}"
199
+ end
200
+ query_text
201
+ end
202
+ end
203
+
204
+ ##
205
+ # @see http://www.w3.org/TR/sparql11-update/#copy
206
+ class Copy < Operation
207
+ def to_s
208
+ # TODO
209
+ end
210
+ end
211
+
212
+ ##
213
+ # @see http://www.w3.org/TR/sparql11-update/#move
214
+ class Move < Operation
215
+ def to_s
216
+ # TODO
217
+ end
218
+ end
219
+
220
+ ##
221
+ # @see http://www.w3.org/TR/sparql11-update/#add
222
+ class Add < Operation
223
+ def to_s
224
+ # TODO
225
+ end
226
+ end
227
+ end # Update
228
+ end # SPARQL::Client
@@ -1,8 +1,8 @@
1
1
  module SPARQL; class Client
2
2
  module VERSION
3
- VERSION_FILE = File.expand_path('../../../../VERSION', __FILE__)
4
- MAJOR, MINOR, TINY, EXTRA = File.read(VERSION_FILE).chop.split('.')
5
- STRING = [MAJOR, MINOR, TINY, EXTRA].compact.join('.')
3
+ FILE = File.expand_path('../../../../VERSION', __FILE__)
4
+ MAJOR, MINOR, TINY, EXTRA = File.read(FILE).chomp.split('.')
5
+ STRING = [MAJOR, MINOR, TINY, EXTRA].compact.join('.').freeze
6
6
 
7
7
  ##
8
8
  # @return [String]
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: 0.3.3
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-01-11 00:00:00.000000000 Z
14
+ date: 2013-01-19 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rdf
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ! '>='
22
22
  - !ruby/object:Gem::Version
23
- version: 0.3.5
23
+ version: '1.0'
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
@@ -28,7 +28,7 @@ dependencies:
28
28
  requirements:
29
29
  - - ! '>='
30
30
  - !ruby/object:Gem::Version
31
- version: 0.3.5
31
+ version: '1.0'
32
32
  - !ruby/object:Gem::Dependency
33
33
  name: net-http-persistent
34
34
  requirement: !ruby/object:Gem::Requirement
@@ -36,7 +36,7 @@ dependencies:
36
36
  requirements:
37
37
  - - ! '>='
38
38
  - !ruby/object:Gem::Version
39
- version: 1.4.1
39
+ version: '1.4'
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
@@ -44,7 +44,7 @@ dependencies:
44
44
  requirements:
45
45
  - - ! '>='
46
46
  - !ruby/object:Gem::Version
47
- version: 1.4.1
47
+ version: '1.4'
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: json_pure
50
50
  requirement: !ruby/object:Gem::Requirement
@@ -52,7 +52,7 @@ dependencies:
52
52
  requirements:
53
53
  - - ! '>='
54
54
  - !ruby/object:Gem::Version
55
- version: 1.4.6
55
+ version: '1.4'
56
56
  type: :runtime
57
57
  prerelease: false
58
58
  version_requirements: !ruby/object:Gem::Requirement
@@ -60,7 +60,7 @@ dependencies:
60
60
  requirements:
61
61
  - - ! '>='
62
62
  - !ruby/object:Gem::Version
63
- version: 1.4.6
63
+ version: '1.4'
64
64
  - !ruby/object:Gem::Dependency
65
65
  name: rdf-spec
66
66
  requirement: !ruby/object:Gem::Requirement
@@ -68,7 +68,7 @@ dependencies:
68
68
  requirements:
69
69
  - - ! '>='
70
70
  - !ruby/object:Gem::Version
71
- version: 0.3.5
71
+ version: '1.0'
72
72
  type: :development
73
73
  prerelease: false
74
74
  version_requirements: !ruby/object:Gem::Requirement
@@ -76,7 +76,7 @@ dependencies:
76
76
  requirements:
77
77
  - - ! '>='
78
78
  - !ruby/object:Gem::Version
79
- version: 0.3.5
79
+ version: '1.0'
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
82
  requirement: !ruby/object:Gem::Requirement
@@ -84,7 +84,7 @@ dependencies:
84
84
  requirements:
85
85
  - - ! '>='
86
86
  - !ruby/object:Gem::Version
87
- version: 2.10.0
87
+ version: '2.12'
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
@@ -92,7 +92,7 @@ dependencies:
92
92
  requirements:
93
93
  - - ! '>='
94
94
  - !ruby/object:Gem::Version
95
- version: 2.10.0
95
+ version: '2.12'
96
96
  - !ruby/object:Gem::Dependency
97
97
  name: yard
98
98
  requirement: !ruby/object:Gem::Requirement
@@ -100,7 +100,7 @@ dependencies:
100
100
  requirements:
101
101
  - - ! '>='
102
102
  - !ruby/object:Gem::Version
103
- version: 0.8.3
103
+ version: '0.8'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
@@ -108,7 +108,7 @@ dependencies:
108
108
  requirements:
109
109
  - - ! '>='
110
110
  - !ruby/object:Gem::Version
111
- version: 0.8.3
111
+ version: '0.8'
112
112
  description: SPARQL client for RDF.rb.
113
113
  email: public-rdf-ruby@w3.org
114
114
  executables: []
@@ -122,9 +122,10 @@ files:
122
122
  - VERSION
123
123
  - lib/sparql/client/query.rb
124
124
  - lib/sparql/client/repository.rb
125
+ - lib/sparql/client/update.rb
125
126
  - lib/sparql/client/version.rb
126
127
  - lib/sparql/client.rb
127
- homepage: http://github.com/ruby-rdf/sparql-client
128
+ homepage: http://ruby-rdf.github.com/sparql-client/
128
129
  licenses:
129
130
  - Public Domain
130
131
  post_install_message: