Class: SparqlAdapter
- Inherits:
-
ActiveRdfAdapter
- Object
- ActiveRdfAdapter
- SparqlAdapter
- Defined in:
- lib/activerdf_sparql/sparql.rb
Overview
SPARQL adapter
Instance Method Summary collapse
-
#execute_sparql_query(qs, header, &block) ⇒ Object
do the real work of executing the sparql query.
-
#initialize(params = {}) ⇒ SparqlAdapter
constructor
Instantiate the connection with the SPARQL Endpoint.
-
#query(query, &block) ⇒ Object
query datastore with query string (SPARQL), returns array with query results may be called with a block.
- #size ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ SparqlAdapter
Instantiate the connection with the SPARQL Endpoint. available parameters:
-
:url => url: endpoint location e.g. “m3pe.org:8080/repositories/test-people”
-
:results => one of :xml, :json, :sparql_xml
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/activerdf_sparql/sparql.rb', line 17 def initialize(params = {}) @reads = true @writes = false @url = params[:url] || '' @result_format = params[:results] || :json known_formats = [:xml, :json, :sparql_xml] raise ActiveRdfError, "Result format unsupported" unless known_formats.include?(@result_format) $activerdflog.info "Sparql adapter initialised #{inspect}" end |
Instance Method Details
#execute_sparql_query(qs, header, &block) ⇒ Object
do the real work of executing the sparql query
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/activerdf_sparql/sparql.rb', line 46 def execute_sparql_query(qs, header, &block) $activerdflog.debug "executing query #{qs} on url #@url" # encoding query string in URL url = "#@url?query=#{CGI.escape(qs)}" # querying sparql endpoint response = '' begin open(url, header) do |f| response = f.read end rescue OpenURI::HTTPError => e raise ActiveRdfError, "could not query SPARQL endpoint, server said: #{e}" return [] rescue Errno::ECONNREFUSED raise ActiveRdfError, "connection refused on SPARQL endpoint #@url" return [] end # we parse content depending on the result format results = case @result_format when :json parse_json(response) when :xml, :sparql_xml parse_xml(response) end if block_given? results.each do |*clauses| yield(*clauses) end else results end end |
#query(query, &block) ⇒ Object
query datastore with query string (SPARQL), returns array with query results may be called with a block
37 38 39 40 41 42 43 |
# File 'lib/activerdf_sparql/sparql.rb', line 37 def query(query, &block) time = Time.now qs = Query2SPARQL.translate(query) $activerdflog.debug "executing sparql query #{query}" execute_sparql_query(qs, header(query), &block) end |
#size ⇒ Object
31 32 33 |
# File 'lib/activerdf_sparql/sparql.rb', line 31 def size query(Query.new.select(:s,:p,:o).where(:s,:p,:o)).size end |