module Tripod::SparqlClient::Query

Public Class Methods

_response_limit_options(response_limit_bytes) click to toggle source
# File lib/tripod/sparql_client.rb, line 80
def self._response_limit_options(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 = {}) click to toggle source

Runs a sparql query against the endpoint. Returns a RestClient response object.

@example Run a query

Tripod::SparqlClient::Query.query('SELECT * WHERE {?s ?p ?o}')

@param [ String ] sparql The sparql query. @param [ String ] accept_header The accept header to send with the request @param [ Hash ] any extra params to send with the request @return [ RestClient::Response ]

# 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_options(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) click to toggle source
Runs a SELECT +query+ against the endpoint. Returns a Hash of the results.

@param [ String ] query The query to run

@example Run a SELECT query

  Tripod::SparqlClient::Query.select('SELECT * WHERE {?s ?p ?o}')

@return [ Hash, String ]
# 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) click to toggle source

 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

# 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