Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#step 1 - installation
sudo gem install sparql
#step 2 - test
# in test.rb
require 'sparql/client'
sparql = SPARQL::Client.new("http://dbpedia.org/sparql")
# SELECT * WHERE { ?s ?p ?o } OFFSET 100 LIMIT 10
query = sparql.select.where([:s, :p, :o]).offset(100).limit(10)
query.each_solution do |solution|
puts solution.inspect
end
#step 3 - what we did?
# creat SPARQL::Client instance
# ~> call initialize("http://dbpedia.org/sparql", {}, nil)
# @url = RDF::URI.new("http://dbpedia.org/sparql")
# @headers = {'Accept' => "#{RESULT_JSON}, #{RESULT_XML}, text/plain"}
#
def initialize(url, options = {}, &block)
@url, @options = RDF::URI.new(url.to_s), options
@headers = {'Accept' => "#{RESULT_JSON}, #{RESULT_XML}, text/plain"}
#no blocks given!
if block_given?
case block.arity
when 1 then block.call(self)
else instance_eval(&block)
end
end
end
#then called select, try to what happed
#
def select(*args)
client = self
result = Query.select(*args)
(class << result; self; end).send(:define_method, :execute) do
client.query(self)
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment