sparql-client 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CREDITS ADDED
@@ -0,0 +1,4 @@
1
+ * Gabriel Horner <gabriel.horner@gmail.com>
2
+ * Nicholas Humfrey <njh@aelius.com>
3
+ * David Nielsen <david@graveyard.dk>
4
+ * Thamaraiselvan Poomalai <p.thamarai@gmail.com>
data/README CHANGED
@@ -1,7 +1,7 @@
1
1
  SPARQL Client for RDF.rb
2
2
  ========================
3
3
 
4
- This is a pure-Ruby implementation of a [SPARQL][] client for [RDF.rb][].
4
+ This is a [Ruby][] implementation of a [SPARQL][] client for [RDF.rb][].
5
5
 
6
6
  * <http://github.com/bendiken/sparql-client>
7
7
 
@@ -68,7 +68,8 @@ Documentation
68
68
  Dependencies
69
69
  ------------
70
70
 
71
- * [RDF.rb](http://rubygems.org/gems/rdf) (>= 0.1.10)
71
+ * [Ruby](http://ruby-lang.org/) (>= 1.8.7) or (>= 1.8.1 with [Backports][])
72
+ * [RDF.rb](http://rubygems.org/gems/rdf) (>= 0.2.0)
72
73
  * [JSON](http://rubygems.org/gems/json_pure) (>= 1.4.0)
73
74
 
74
75
  Installation
@@ -91,6 +92,35 @@ as follows:
91
92
 
92
93
  % wget http://github.com/bendiken/sparql-client/tarball/master
93
94
 
95
+ Authors
96
+ -------
97
+
98
+ * [Arto Bendiken](mailto:arto.bendiken@gmail.com) - <http://ar.to/>
99
+ * [Ben Lavender](mailto:blavender@gmail.com) - <http://bhuga.net/>
100
+
101
+ Contributors
102
+ ------------
103
+
104
+ * [Gabriel Horner](http://github.com/cldwalker) - <http://tagaholic.me/>
105
+ * [Nicholas Humfrey](http://github.com/njh) - <http://www.aelius.com/njh/>
106
+ * [David Nielsen](http://github.com/drankard) - <http://github.com/drankard>
107
+ * [Thamaraiselvan Poomalai](http://github.com/selvan) - <http://softonaut.blogspot.com/>
108
+
109
+ Contributing
110
+ ------------
111
+
112
+ * Do your best to adhere to the existing coding conventions and idioms.
113
+ * Don't use hard tabs, and don't leave trailing whitespace on any line.
114
+ * Do document every method you add using [YARD][] annotations. Read the
115
+ [tutorial][YARD-GS] or just look at the existing code for examples.
116
+ * Don't touch the `.gemspec`, `VERSION` or `AUTHORS` files. If you need to
117
+ change them, do so on your private branch only.
118
+ * Do feel free to add yourself to the `CREDITS` file and the corresponding
119
+ list in the the `README`. Alphabetical order applies.
120
+ * Do note that in order for us to merge any non-trivial changes (as a rule
121
+ of thumb, additions larger than about 15 lines of code), we need an
122
+ explicit [public domain dedication][PDD] on record from you.
123
+
94
124
  Resources
95
125
  ---------
96
126
 
@@ -101,22 +131,13 @@ Resources
101
131
  * <http://raa.ruby-lang.org/project/sparql-client/>
102
132
  * <http://www.ohloh.net/p/rdf>
103
133
 
104
- Authors
105
- -------
106
-
107
- * [Arto Bendiken](mailto:arto.bendiken@gmail.com) - <http://ar.to/>
108
- * [Ben Lavender](mailto:blavender@gmail.com) - <http://bhuga.net/>
109
-
110
- Contributors
111
- ------------
112
- * [Nicholas Humfrey](mailto:njh@aelius.com) - <http://www.aelius.com/njh/>
113
-
114
134
  License
115
135
  -------
116
136
 
117
- `SPARQL::Client` is free and unencumbered public domain software. For more
118
- information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
137
+ This is free and unencumbered public domain software. For more information,
138
+ see <http://unlicense.org/> or the accompanying {file:UNLICENSE} file.
119
139
 
140
+ [Ruby]: http://ruby-lang.org/
120
141
  [RDF]: http://www.w3.org/RDF/
121
142
  [SPARQL]: http://en.wikipedia.org/wiki/SPARQL
122
143
  [SPARQL JSON]: http://www.w3.org/TR/rdf-sparql-json-res/
@@ -125,3 +146,7 @@ information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
125
146
  [RDF::Repository]: http://rdf.rubyforge.org/RDF/Repository.html
126
147
  [DSL]: http://en.wikipedia.org/wiki/Domain-specific_language
127
148
  "domain-specific language"
149
+ [YARD]: http://yardoc.org/
150
+ [YARD-GS]: http://rubydoc.info/docs/yard/file/docs/GettingStarted.md
151
+ [PDD]: http://unlicense.org/#unlicensing-contributions
152
+ [Backports]: http://rubygems.org/gems/backports
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -1,3 +1,6 @@
1
1
  module SPARQL
2
- autoload :Client, 'sparql/client'
2
+ # @see http://rubygems.org/gems/sparql-client
3
+ autoload :Client, 'sparql/client'
4
+ # @see http://rubygems.org/gems/sparql-grammar
5
+ autoload :Grammar, 'sparql/grammar'
3
6
  end
@@ -43,12 +43,7 @@ module SPARQL
43
43
  #
44
44
  # @return [Query]
45
45
  def ask(*args)
46
- client = self
47
- result = Query.ask(*args)
48
- (class << result; self; end).send(:define_method, :execute) do
49
- client.query(self)
50
- end
51
- result
46
+ call_query_method(:ask, *args)
52
47
  end
53
48
 
54
49
  ##
@@ -57,12 +52,16 @@ module SPARQL
57
52
  # @param [Array<Symbol>] variables
58
53
  # @return [Query]
59
54
  def select(*args)
60
- client = self
61
- result = Query.select(*args)
62
- (class << result; self; end).send(:define_method, :execute) do
63
- client.query(self)
64
- end
65
- result
55
+ call_query_method(:select, *args)
56
+ end
57
+
58
+ ##
59
+ # Executes a `DESCRIBE` query.
60
+ #
61
+ # @param [Array<Symbol, RDF::URI>] variables
62
+ # @return [Query]
63
+ def describe(*args)
64
+ call_query_method(:describe, *args)
66
65
  end
67
66
 
68
67
  ##
@@ -71,8 +70,14 @@ module SPARQL
71
70
  # @param [Array<Symbol>] pattern
72
71
  # @return [Query]
73
72
  def construct(*args)
73
+ call_query_method(:construct, *args)
74
+ end
75
+
76
+ ##
77
+ # @private
78
+ def call_query_method(meth, *args)
74
79
  client = self
75
- result = Query.construct(*args)
80
+ result = Query.send(meth, *args)
76
81
  (class << result; self; end).send(:define_method, :execute) do
77
82
  client.query(self)
78
83
  end
@@ -84,6 +89,7 @@ module SPARQL
84
89
  #
85
90
  # @param [String, #to_s] url
86
91
  # @param [Hash{Symbol => Object}] options
92
+ # @option options [String] :content_type
87
93
  # @return [Array<RDF::Query::Solution>]
88
94
  def query(query, options = {})
89
95
  get(query) do |response|
@@ -95,16 +101,17 @@ module SPARQL
95
101
  when Net::HTTPServerError # 5xx
96
102
  raise ServerError.new(response.body)
97
103
  when Net::HTTPSuccess # 2xx
98
- parse_response(response)
104
+ parse_response(response, options)
99
105
  end
100
106
  end
101
107
  end
102
108
 
103
109
  ##
104
110
  # @param [Net::HTTPSuccess] response
111
+ # @param [Hash{Symbol => Object}] options
105
112
  # @return [Object]
106
- def parse_response(response)
107
- case content_type = response.content_type
113
+ def parse_response(response, options = {})
114
+ case content_type = options[:content_type] || response.content_type
108
115
  when RESULT_BOOL # Sesame-specific
109
116
  response.body == 'true'
110
117
  when RESULT_JSON
@@ -112,7 +119,7 @@ module SPARQL
112
119
  when RESULT_XML
113
120
  parse_xml_bindings(response.body)
114
121
  else
115
- parse_rdf_serialization(response)
122
+ parse_rdf_serialization(response, options)
116
123
  end
117
124
  end
118
125
 
@@ -202,9 +209,11 @@ module SPARQL
202
209
 
203
210
  ##
204
211
  # @param [Net::HTTPSuccess] response
212
+ # @param [Hash{Symbol => Object}] options
205
213
  # @return [RDF::Enumerable]
206
- def parse_rdf_serialization(response)
207
- if reader = RDF::Reader.for(:content_type => response.content_type)
214
+ def parse_rdf_serialization(response, options = {})
215
+ options = {:content_type => response.content_type} if options.empty?
216
+ if reader = RDF::Reader.for(options)
208
217
  reader.new(response.body)
209
218
  end
210
219
  end
@@ -227,6 +236,24 @@ module SPARQL
227
236
 
228
237
  protected
229
238
 
239
+ ##
240
+ # Returns an HTTP class or HTTP proxy class based on environment http_proxy & https_proxy settings
241
+ # @return [Net::HTTP::Proxy]
242
+ def http_klass(scheme)
243
+ proxy_uri = nil
244
+ case scheme
245
+ when "http"
246
+ proxy_uri = URI.parse(ENV['http_proxy']) unless ENV['http_proxy'].nil?
247
+ when "https"
248
+ proxy_uri = URI.parse(ENV['https_proxy']) unless ENV['https_proxy'].nil?
249
+ end
250
+ return Net::HTTP if proxy_uri.nil?
251
+
252
+ proxy_host, proxy_port = proxy_uri.host, proxy_uri.port
253
+ proxy_user, proxy_pass = proxy_uri.userinfo.split(/:/) if proxy_uri.userinfo
254
+ Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass)
255
+ end
256
+
230
257
  ##
231
258
  # Performs an HTTP GET request against the SPARQL endpoint.
232
259
  #
@@ -239,7 +266,7 @@ module SPARQL
239
266
  url = self.url.dup
240
267
  url.query_values = {:query => query.to_s}
241
268
 
242
- Net::HTTP.start(url.host, url.port) do |http|
269
+ http_klass(url.scheme).start(url.host, url.port) do |http|
243
270
  response = http.get(url.path + "?#{url.query}", @headers.merge(headers))
244
271
  if block_given?
245
272
  block.call(response)
@@ -16,6 +16,10 @@ module SPARQL; class Client
16
16
  # @return [Hash{Symbol => Object}]
17
17
  attr_reader :options
18
18
 
19
+ ##
20
+ # @return [Array<[key, RDF::Value]>]
21
+ attr_reader :values
22
+
19
23
  ##
20
24
  # Creates a boolean `ASK` query.
21
25
  #
@@ -35,7 +39,19 @@ module SPARQL; class Client
35
39
  # @see http://www.w3.org/TR/rdf-sparql-query/#select
36
40
  def self.select(*variables)
37
41
  options = variables.last.is_a?(Hash) ? variables.pop : {}
38
- self.new(:select, options).select(*variables) # FIXME
42
+ self.new(:select, options).select(*variables)
43
+ end
44
+
45
+ ##
46
+ # Creates a `DESCRIBE` query.
47
+ #
48
+ # @param [Array<Symbol, RDF::URI>] variables
49
+ # @param [Hash{Symbol => Object}] options
50
+ # @return [Query]
51
+ # @see http://www.w3.org/TR/rdf-sparql-query/#describe
52
+ def self.describe(*variables)
53
+ options = variables.last.is_a?(Hash) ? variables.pop : {}
54
+ self.new(:describe, options).describe(*variables)
39
55
  end
40
56
 
41
57
  ##
@@ -70,15 +86,21 @@ module SPARQL; class Client
70
86
 
71
87
  ##
72
88
  # @param [Array<Symbol>] variables
73
- # @param [Hash{Symbol => Object}] options
74
89
  # @return [Query]
75
90
  # @see http://www.w3.org/TR/rdf-sparql-query/#select
76
91
  def select(*variables)
77
- @form = :select
78
- options = variables.last.is_a?(Hash) ? variables.pop : {}
79
- @variables = variables.inject({}) do |vars, var|
80
- vars.merge(var.to_sym => RDF::Query::Variable.new(var))
81
- end
92
+ @values = variables.map { |var| [var, RDF::Query::Variable.new(var)] }
93
+ self
94
+ end
95
+
96
+ ##
97
+ # @param [Array<Symbol>] variables
98
+ # @return [Query]
99
+ # @see http://www.w3.org/TR/rdf-sparql-query/#describe
100
+ def describe(*variables)
101
+ @values = variables.map { |var|
102
+ [var, var.is_a?(RDF::URI) ? var : RDF::Query::Variable.new(var)]
103
+ }
82
104
  self
83
105
  end
84
106
 
@@ -86,12 +108,15 @@ module SPARQL; class Client
86
108
  # @param [Array<RDF::Query::Pattern, Array>] patterns
87
109
  # @return [Query]
88
110
  def construct(*patterns)
89
- options[:template] = patterns.map do |pattern|
90
- case pattern
91
- when RDF::Query::Pattern then pattern
92
- else RDF::Query::Pattern.new(*pattern.to_a)
93
- end
94
- end
111
+ options[:template] = build_patterns(patterns)
112
+ self
113
+ end
114
+
115
+ # @param RDF::URI uri
116
+ # @return [Query]
117
+ # @see http://www.w3.org/TR/rdf-sparql-query/#specDataset
118
+ def from(uri)
119
+ options[:from] = uri
95
120
  self
96
121
  end
97
122
 
@@ -100,19 +125,14 @@ module SPARQL; class Client
100
125
  # @return [Query]
101
126
  # @see http://www.w3.org/TR/rdf-sparql-query/#GraphPattern
102
127
  def where(*patterns)
103
- @patterns += patterns.map do |pattern|
104
- case pattern
105
- when RDF::Query::Pattern then pattern
106
- else RDF::Query::Pattern.new(*pattern.to_a)
107
- end
108
- end
128
+ @patterns += build_patterns(patterns)
109
129
  self
110
130
  end
111
131
 
112
132
  alias_method :whether, :where
113
133
 
114
134
  ##
115
- # @param [Array<Symbol>] variables
135
+ # @param [Array<Symbol, String>] variables
116
136
  # @return [Query]
117
137
  # @see http://www.w3.org/TR/rdf-sparql-query/#modOrderBy
118
138
  def order(*variables)
@@ -164,6 +184,33 @@ module SPARQL; class Client
164
184
  self
165
185
  end
166
186
 
187
+ ##
188
+ # @return [Query]
189
+ # @see http://www.w3.org/TR/rdf-sparql-query/#prefNames
190
+ def prefix(string)
191
+ (options[:prefixes] ||= []) << string
192
+ self
193
+ end
194
+
195
+ ##
196
+ # @return [Query]
197
+ # @see http://www.w3.org/TR/rdf-sparql-query/#optionals
198
+ def optional(*patterns)
199
+ (options[:optionals] ||= []) << build_patterns(patterns)
200
+ self
201
+ end
202
+
203
+ ##
204
+ # @private
205
+ def build_patterns(patterns)
206
+ patterns.map do |pattern|
207
+ case pattern
208
+ when RDF::Query::Pattern then pattern
209
+ else RDF::Query::Pattern.new(*pattern.to_a)
210
+ end
211
+ end
212
+ end
213
+
167
214
  ##
168
215
  # @private
169
216
  def filter(string)
@@ -220,38 +267,54 @@ module SPARQL; class Client
220
267
  def to_s
221
268
  buffer = [form.to_s.upcase]
222
269
  case form
223
- when :select
270
+ when :select, :describe
224
271
  buffer << 'DISTINCT' if options[:distinct]
225
272
  buffer << 'REDUCED' if options[:reduced]
226
- buffer << (variables.empty? ? '*' : variables.values.map { |v| serialize_value(v) }.join(' '))
273
+ buffer << (values.empty? ? '*' : values.map { |v| serialize_value(v[1]) }.join(' '))
227
274
  when :construct
228
275
  buffer << '{'
229
- buffer += options[:template].map do |p|
230
- p.to_triple.map { |v| serialize_value(v) }.join(' ') + " . "
231
- end
276
+ buffer += serialize_patterns(options[:template])
232
277
  buffer << '}'
233
278
  end
234
279
 
235
- buffer << 'WHERE {'
236
- buffer += patterns.map do |p|
237
- p.to_triple.map { |v| serialize_value(v) }.join(' ') + " . "
238
- end
239
- if options[:filters]
240
- buffer += options[:filters].map { |filter| "FILTER(#{filter})" }
280
+ buffer << "FROM #{serialize_value(options[:from])}" if options[:from]
281
+
282
+ unless patterns.empty? && form == :describe
283
+ buffer << 'WHERE {'
284
+ buffer += serialize_patterns(patterns)
285
+ if options[:optionals]
286
+ options[:optionals].each do |patterns|
287
+ buffer << 'OPTIONAL {'
288
+ buffer += serialize_patterns(patterns)
289
+ buffer << '}'
290
+ end
291
+ end
292
+ if options[:filters]
293
+ buffer += options[:filters].map { |filter| "FILTER(#{filter})" }
294
+ end
295
+ buffer << '}'
241
296
  end
242
- buffer << '}'
243
297
 
244
298
  if options[:order_by]
245
299
  buffer << 'ORDER BY'
246
- buffer += options[:order_by].map { |var| "?#{var}" }
300
+ buffer += options[:order_by].map { |var| var.is_a?(String) ? var : "?#{var}" }
247
301
  end
248
302
 
249
303
  buffer << "OFFSET #{options[:offset]}" if options[:offset]
250
304
  buffer << "LIMIT #{options[:limit]}" if options[:limit]
305
+ options[:prefixes].reverse.each {|e| buffer.unshift("PREFIX #{e}") } if options[:prefixes]
251
306
 
252
307
  buffer.join(' ')
253
308
  end
254
309
 
310
+ ##
311
+ # @private
312
+ def serialize_patterns(patterns)
313
+ patterns.map do |p|
314
+ p.to_triple.map { |v| serialize_value(v) }.join(' ') + " ."
315
+ end
316
+ end
317
+
255
318
  ##
256
319
  # Outputs a developer-friendly representation of this query to `stderr`.
257
320
  #
@@ -279,7 +342,7 @@ module SPARQL; class Client
279
342
  # SPARQL queries are UTF-8, but support ASCII-style Unicode escapes, so
280
343
  # the N-Triples serializer is fine unless it's a variable:
281
344
  case
282
- when value.variable? then "?#{value.name}"
345
+ when value.variable? then value.to_s
283
346
  else RDF::NTriples.serialize(value)
284
347
  end
285
348
  end
@@ -2,11 +2,10 @@ module SPARQL; class Client
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 4
5
+ TINY = 5
6
6
  EXTRA = nil
7
7
 
8
- STRING = [MAJOR, MINOR, TINY].join('.')
9
- STRING << ".#{EXTRA}" if EXTRA
8
+ STRING = [MAJOR, MINOR, TINY, EXTRA].compact.join('.')
10
9
 
11
10
  ##
12
11
  # @return [String]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Arto Bendiken
@@ -15,36 +15,36 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-05-21 00:00:00 +02:00
18
+ date: 2010-09-06 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: rdf-spec
22
+ name: json_pure
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ~>
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
- - 0
30
29
  - 1
31
- - 10
32
- version: 0.1.10
33
- type: :development
30
+ - 4
31
+ - 0
32
+ version: 1.4.0
33
+ type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: rspec
36
+ name: rdf
37
37
  prerelease: false
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - ">="
40
+ - - ~>
41
41
  - !ruby/object:Gem::Version
42
42
  segments:
43
- - 1
44
- - 3
45
43
  - 0
46
- version: 1.3.0
47
- type: :development
44
+ - 2
45
+ - 0
46
+ version: 0.2.0
47
+ type: :runtime
48
48
  version_requirements: *id002
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: yard
@@ -55,41 +55,41 @@ dependencies:
55
55
  - !ruby/object:Gem::Version
56
56
  segments:
57
57
  - 0
58
- - 5
59
- - 4
60
- version: 0.5.4
58
+ - 6
59
+ - 0
60
+ version: 0.6.0
61
61
  type: :development
62
62
  version_requirements: *id003
63
63
  - !ruby/object:Gem::Dependency
64
- name: rdf
64
+ name: rspec
65
65
  prerelease: false
66
66
  requirement: &id004 !ruby/object:Gem::Requirement
67
67
  requirements:
68
- - - ~>
68
+ - - ">="
69
69
  - !ruby/object:Gem::Version
70
70
  segments:
71
- - 0
72
71
  - 1
73
- - 10
74
- version: 0.1.10
75
- type: :runtime
72
+ - 3
73
+ - 0
74
+ version: 1.3.0
75
+ type: :development
76
76
  version_requirements: *id004
77
77
  - !ruby/object:Gem::Dependency
78
- name: json_pure
78
+ name: rdf-spec
79
79
  prerelease: false
80
80
  requirement: &id005 !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - ~>
83
83
  - !ruby/object:Gem::Version
84
84
  segments:
85
- - 1
86
- - 4
87
85
  - 0
88
- version: 1.4.0
89
- type: :runtime
86
+ - 2
87
+ - 0
88
+ version: 0.2.0
89
+ type: :development
90
90
  version_requirements: *id005
91
91
  description: SPARQL client for RDF.rb.
92
- email: arto.bendiken@gmail.com
92
+ email: public-rdf-ruby@w3.org
93
93
  executables: []
94
94
 
95
95
  extensions: []
@@ -98,7 +98,7 @@ extra_rdoc_files: []
98
98
 
99
99
  files:
100
100
  - AUTHORS
101
- - CONTRIBUTORS
101
+ - CREDITS
102
102
  - README
103
103
  - UNLICENSE
104
104
  - VERSION
@@ -108,7 +108,7 @@ files:
108
108
  - lib/sparql/client.rb
109
109
  - lib/sparql.rb
110
110
  has_rdoc: false
111
- homepage: http://sparql.rubyforge.org/
111
+ homepage: http://sparql.rubyforge.org/client/
112
112
  licenses:
113
113
  - Public Domain
114
114
  post_install_message:
@@ -123,8 +123,8 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
123
  segments:
124
124
  - 1
125
125
  - 8
126
- - 2
127
- version: 1.8.2
126
+ - 1
127
+ version: 1.8.1
128
128
  required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  requirements:
130
130
  - - ">="
@@ -1 +0,0 @@
1
- * Nicholas Humfrey <njh@aelius.com>