Module: Tripod::SparqlClient::Query
- Defined in:
- lib/tripod/sparql_client.rb
Class Method Summary collapse
-
.query(sparql, accept_header, extra_params = {}) ⇒ RestClient::Response
Runs a
sparqlquery against the endpoint. -
.select(query) ⇒ Hash, String
Runs a SELECT
queryagainst the endpoint.
Class Method Details
.query(sparql, accept_header, extra_params = {}) ⇒ RestClient::Response
Runs a sparql query against the endpoint. Returns a RestClient response object.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tripod/sparql_client.rb', line 17 def self.query(sparql, accept_header, extra_params={}) params = {:query => sparql}.merge(extra_params) request_url = Tripod.query_endpoint + '?' + params.to_query streaming_opts = {:accept => accept_header, :timeout_seconds => Tripod.timeout_seconds} streaming_opts.merge!(:response_limit_bytes => Tripod.response_limit_bytes) if Tripod.response_limit_bytes # Hash.to_query from active support core extensions stream_data = -> { if defined?(Rails) Rails.logger.debug "TRIPOD: About to run query: #{sparql}" Rails.logger.debug "TRIPOD: Straming fron url: #{request_url}" Rails.logger.debug "TRIPOD: Streaming opts: #{streaming_opts.inspect}" end response = Tripod::Streaming.get_data(request_url, streaming_opts) Rails.logger.debug "TRIPOD: response: #{response}" if defined?(Rails) response } if Tripod.cache_store # if a cache store is configured # SHA-2 the key to keep the it within the small limit for many cache stores (e.g. Memcached is 250bytes) # Note: SHA2's are pretty certain to be unique http://en.wikipedia.org/wiki/SHA-2. key = 'SPARQL-QUERY-' + Digest::SHA2.hexdigest([extra_params, accept_header, sparql].join(" ")) Tripod.cache_store.fetch(key, &stream_data) else stream_data.call() end end |
.select(query) ⇒ Hash, String
Runs a SELECT query against the endpoint. Returns a Hash of the results.
Tripod::SparqlClient::Query.select(‘SELECT * WHERE ?p ?o’)
56 57 58 59 |
# File 'lib/tripod/sparql_client.rb', line 56 def self.select(query) query_response = self.query(query, "application/sparql-results+json") JSON.parse(query_response)["results"]["bindings"] end |