sparql-client 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README +1 -0
  3. data/VERSION +1 -1
  4. data/lib/sparql/client.rb +61 -31
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21b9057801f8220049ffc8bc3c95646228bd6a4f
4
- data.tar.gz: 1aa7bd26e8eb4fcac61b7816711ab974eab95895
3
+ metadata.gz: 251df215fdb211b37f325950b2d6f64d37639558
4
+ data.tar.gz: f07ba6ef756c9bf6306b496a0626661638ee6af3
5
5
  SHA512:
6
- metadata.gz: b25feef617faf432f395a94da83ad0f6e4a39778f833aed8d481a1f27dffb31f3f60582a072693eee0057271dc2e183465fecdba33fa0e9fde585633972ebbd7
7
- data.tar.gz: 83ef87f783a2e662bdf163d8ceb7c439db993aadd6ecf24498ebf07c3e9077ed7620ad60bb557fd491eeae454a5538d1ea39630ad14a04d14a274212215783e4
6
+ metadata.gz: 281f97c225fc2a75aadd4483699043562a784aee163ebb899c8baa57e7118852950706ea939456c9c13f8d01e887d13517b3e14dc4de3b1d01c7b7e21a7ff431
7
+ data.tar.gz: c6c0b441502b862d6abde67a6933aa2eb4633b1cba6c2685eae3eca2a42a1f6829218d9fa1f6c91a16bdcf58bdb76263c941ecc53bb0df0de13d150f3ecd8375
data/README CHANGED
@@ -78,6 +78,7 @@ This is a [Ruby][] implementation of a [SPARQL][] client for [RDF.rb][].
78
78
  * [RDF.rb](http://rubygems.org/gems/rdf) (>= 1.1)
79
79
  * [Net::HTTP::Persistent](http://rubygems.org/gems/net-http-persistent) (>= 1.4)
80
80
  * Soft dependency on [SPARQL](http://rubygems.org/gems/sparql) (>= 1.1)
81
+ * Soft dependency on [Nokogiri](http://rubygems.org/gems/nokogiri) (>= 1.5)
81
82
 
82
83
  ##Installation
83
84
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.1.1
@@ -1,6 +1,11 @@
1
1
  require 'net/http/persistent' # @see http://rubygems.org/gems/net-http-persistent
2
2
  require 'rdf' # @see http://rubygems.org/gems/rdf
3
3
  require 'rdf/ntriples' # @see http://rubygems.org/gems/rdf
4
+ begin
5
+ require 'nokogiri'
6
+ rescue LoadError
7
+ require 'rexml/document'
8
+ end
4
9
 
5
10
  module SPARQL
6
11
  ##
@@ -35,6 +40,8 @@ module SPARQL
35
40
  DEFAULT_PROTOCOL = 1.0
36
41
  DEFAULT_METHOD = :post
37
42
 
43
+ XMLNS = {'sparql' => 'http://www.w3.org/2005/sparql-results#'}.freeze
44
+
38
45
  ##
39
46
  # The SPARQL endpoint URL, or an RDF::Queryable instance, to use the native SPARQL engine.
40
47
  #
@@ -64,6 +71,7 @@ module SPARQL
64
71
  # @option options [Symbol] :method (DEFAULT_METHOD)
65
72
  # @option options [Number] :protocol (DEFAULT_PROTOCOL)
66
73
  # @option options [Hash] :headers
74
+ # @option options [Hash] :read_timeout
67
75
  def initialize(url, options = {}, &block)
68
76
  case url
69
77
  when RDF::Queryable
@@ -314,7 +322,7 @@ module SPARQL
314
322
  # @param [Hash{Symbol => Object}] options
315
323
  # @return [Object]
316
324
  def parse_response(response, options = {})
317
- case content_type = options[:content_type] || response.content_type
325
+ case options[:content_type] || response.content_type
318
326
  when RESULT_BOOL # Sesame-specific
319
327
  response.body == 'true'
320
328
  when RESULT_JSON
@@ -343,7 +351,7 @@ module SPARQL
343
351
  when json.has_key?('results')
344
352
  solutions = json['results']['bindings'].map do |row|
345
353
  row = row.inject({}) do |cols, (name, value)|
346
- cols.merge(name.to_sym => parse_json_value(value))
354
+ cols.merge(name.to_sym => parse_json_value(value, nodes))
347
355
  end
348
356
  RDF::Query::Solution.new(row)
349
357
  end
@@ -404,15 +412,18 @@ module SPARQL
404
412
  tsv.each do |row|
405
413
  solution = RDF::Query::Solution.new
406
414
  row.each_with_index do |v, i|
407
- term = RDF::NTriples.unserialize(v) || case v
408
- when /^\d+\.\d*[eE][+-]?[0-9]+$/ then RDF::Literal::Double.new(v)
409
- when /^\d*\.\d+[eE][+-]?[0-9]+$/ then RDF::Literal::Double.new(v)
410
- when /^\d*\.\d+$/ then RDF::Literal::Decimal.new(v)
411
- when /^\d+$/ then RDF::Literal::Integer.new(v)
412
- else
413
- RDF::Literal(v)
415
+ if !v.empty?
416
+ term = RDF::NTriples.unserialize(v) || case v
417
+ when /^\d+\.\d*[eE][+-]?[0-9]+$/ then RDF::Literal::Double.new(v)
418
+ when /^\d*\.\d+[eE][+-]?[0-9]+$/ then RDF::Literal::Double.new(v)
419
+ when /^\d*\.\d+$/ then RDF::Literal::Decimal.new(v)
420
+ when /^\d+$/ then RDF::Literal::Integer.new(v)
421
+ else
422
+ RDF::Literal(v)
423
+ end
424
+ nodes[term.id] = term if term.is_a? RDF::Node
425
+ solution[vars[i].to_sym] = term
414
426
  end
415
- solution[vars[i].to_sym] = term
416
427
  end
417
428
  solutions << solution
418
429
  end
@@ -420,33 +431,52 @@ module SPARQL
420
431
  end
421
432
 
422
433
  ##
423
- # @param [String, REXML::Element] xml
434
+ # @param [String, IO, Nokogiri::XML::Node, REXML::Element] xml
424
435
  # @return [<RDF::Query::Solutions>]
425
436
  # @see http://www.w3.org/TR/rdf-sparql-json-res/#results
426
437
  def self.parse_xml_bindings(xml, nodes = {})
427
438
  xml.force_encoding(::Encoding::UTF_8) if xml.respond_to?(:force_encoding)
428
- require 'rexml/document' unless defined?(::REXML::Document)
429
- xml = REXML::Document.new(xml).root unless xml.is_a?(REXML::Element)
430
439
 
431
- case
432
- when boolean = xml.elements['boolean']
433
- boolean.text == 'true'
434
- when results = xml.elements['results']
435
- solutions = results.elements.map do |result|
436
- row = {}
437
- result.elements.each do |binding|
438
- name = binding.attributes['name'].to_sym
439
- value = binding.select { |node| node.kind_of?(::REXML::Element) }.first
440
- row[name] = parse_xml_value(value, nodes)
440
+ if defined?(::Nokogiri)
441
+ xml = Nokogiri::XML(xml).root unless xml.is_a?(Nokogiri::XML::Document)
442
+ case
443
+ when boolean = xml.xpath("//sparql:boolean", XMLNS)[0]
444
+ boolean.text == 'true'
445
+ when results = xml.xpath("//sparql:results", XMLNS)[0]
446
+ solutions = results.elements.map do |result|
447
+ row = {}
448
+ result.elements.each do |binding|
449
+ name = binding.attr('name').to_sym
450
+ value = binding.elements.first
451
+ row[name] = parse_xml_value(value, nodes)
452
+ end
453
+ RDF::Query::Solution.new(row)
441
454
  end
442
- RDF::Query::Solution.new(row)
443
- end
444
- RDF::Query::Solutions.new(solutions)
455
+ RDF::Query::Solutions.new(solutions)
456
+ end
457
+ else
458
+ # REXML
459
+ xml = REXML::Document.new(xml).root unless xml.is_a?(REXML::Element)
460
+ case
461
+ when boolean = xml.elements['boolean']
462
+ boolean.text == 'true'
463
+ when results = xml.elements['results']
464
+ solutions = results.elements.map do |result|
465
+ row = {}
466
+ result.elements.each do |binding|
467
+ name = binding.attributes['name'].to_sym
468
+ value = binding.select { |node| node.kind_of?(::REXML::Element) }.first
469
+ row[name] = parse_xml_value(value, nodes)
470
+ end
471
+ RDF::Query::Solution.new(row)
472
+ end
473
+ RDF::Query::Solutions.new(solutions)
474
+ end
445
475
  end
446
476
  end
447
477
 
448
478
  ##
449
- # @param [REXML::Element] value
479
+ # @param [Nokogiri::XML::Element, REXML::Element] value
450
480
  # @return [RDF::Value]
451
481
  # @see http://www.w3.org/TR/rdf-sparql-json-res/#variable-binding-results
452
482
  def self.parse_xml_value(value, nodes = {})
@@ -456,10 +486,9 @@ module SPARQL
456
486
  when :uri
457
487
  RDF::URI.new(value.text)
458
488
  when :literal
459
- RDF::Literal.new(value.text, {
460
- :language => value.attributes['xml:lang'],
461
- :datatype => value.attributes['datatype'],
462
- })
489
+ lang = value.respond_to?(:attr) ? value.attr('xml:lang') : value.attributes['xml:lang']
490
+ datatype = value.respond_to?(:attr) ? value.attr('datatype') : value.attributes['datatype']
491
+ RDF::Literal.new(value.text, :language => lang, :datatype => datatype)
463
492
  else nil
464
493
  end
465
494
  end
@@ -540,6 +569,7 @@ module SPARQL
540
569
  end
541
570
  klass = Net::HTTP::Persistent.new(self.class.to_s, proxy_url)
542
571
  klass.keep_alive = 120 # increase to 2 minutes
572
+ klass.read_timeout = @options[:read_timeout] || 60
543
573
  klass
544
574
  end
545
575
 
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.0
4
+ version: 1.1.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: 2013-11-11 00:00:00.000000000 Z
13
+ date: 2013-12-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rdf