Module: Tripod::SparqlClient::Query
- Defined in:
- lib/tripod/sparql_client.rb
Class Method Summary collapse
- ._response_limit_options(response_limit_bytes) ⇒ Object
-
.query(sparql, accept_header, extra_params = {}, response_limit_bytes = :default, extra_headers = {}) ⇒ RestClient::Response
Runs a
sparql
query against the endpoint. -
.select(query) ⇒ Hash, String
Runs a SELECT
query
against the endpoint. -
.to_query(hash) ⇒ Object
Tripod helper to turn a hash to a query string, allowing multiple params in arrays e.g.
Class Method Details
._response_limit_options(response_limit_bytes) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/tripod/sparql_client.rb', line 80 def self.(response_limit_bytes) case response_limit_bytes when Integer {response_limit_bytes: response_limit_bytes} when :default {response_limit_bytes: Tripod.response_limit_bytes} when :no_response_limit {} end end |
.query(sparql, accept_header, extra_params = {}, response_limit_bytes = :default, extra_headers = {}) ⇒ 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 47 |
# File 'lib/tripod/sparql_client.rb', line 17 def self.query(sparql, accept_header, extra_params={}, response_limit_bytes = :default, extra_headers = {}) non_sparql_params = (Tripod.extra_endpoint_params).merge(extra_params) params_hash = {:query => sparql} params = self.to_query(params_hash) request_url = URI(Tripod.query_endpoint).tap {|u| u.query = non_sparql_params.to_query}.to_s extra_headers.merge!(Tripod.extra_endpoint_headers) streaming_opts = {:accept => accept_header, :timeout_seconds => Tripod.timeout_seconds, :extra_headers => extra_headers} streaming_opts.merge!((response_limit_bytes)) if Tripod.response_limit_bytes # Hash.to_query from active support core extensions stream_data = -> { Tripod.logger.debug "TRIPOD: About to run query: #{sparql}" Tripod.logger.debug "TRIPOD: Streaming from url: #{request_url}" Tripod.logger.debug "TRIPOD: non sparql params #{non_sparql_params.to_s}" Tripod.logger.debug "TRIPOD: Streaming opts: #{streaming_opts.inspect}" Tripod::Streaming.get_data(request_url, params, streaming_opts) } if Tripod.cache_store # if a cache store is configured Tripod.logger.debug "TRIPOD: caching is on!" # 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. cache_key = 'SPARQL-QUERY-' + Digest::SHA2.hexdigest([extra_params, accept_header, sparql, Tripod.query_endpoint].join("-")) Tripod.cache_store.fetch(cache_key, &stream_data) else Tripod.logger.debug "TRIPOD caching is off!" 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’)
71 72 73 74 75 76 77 78 |
# File 'lib/tripod/sparql_client.rb', line 71 def self.select(query) query_response = self.query(query, "application/sparql-results+json") if query_response.length >0 JSON.parse(query_response)["results"]["bindings"] else [] end end |
.to_query(hash) ⇒ Object
Tripod helper to turn a hash to a query string, allowing multiple params in arrays e.g. :query=>‘foo’, :graph=>[‘bar’, ‘baz’]
-> query=foo&graph=bar&graph=baz
based on the ActiveSupport implementation, but with different behaviour for arrays
53 54 55 56 57 58 59 60 61 |
# File 'lib/tripod/sparql_client.rb', line 53 def self.to_query hash hash.collect_concat do |key, value| if value.class == Array value.collect { |v| v.to_query( key ) } else value.to_query(key) end end.sort * '&' end |