Skip to content

Instantly share code, notes, and snippets.

@rsinger
Created May 12, 2011 23:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsinger/969667 to your computer and use it in GitHub Desktop.
Save rsinger/969667 to your computer and use it in GitHub Desktop.
require 'httparty'
require 'rdf'
require 'rdf/ntriples'
require 'rdf/nquads'
require 'json'
class Fuseki
include HTTParty
headers 'Accept' => 'text/plain; charset=utf-8'
base_uri 'http://localhost:3030/'
def self.describe(dataset, uris)
all_statements(dataset, {:conditions=>["FILTER (?s IN (<#{[*uris].join(">, ")}>))"]})
end
def self.all_statements(dataset, options={})
headers 'Accept' => 'application/sparql-results+json; charset=utf-8'
sparql = Sparql.generate_select_all(options)
resp = get("/#{dataset}/query", {:query=>{:query=>sparql}})
result = JSON.parse(resp.body)
statements = []
result['results']['bindings'].each do |r|
obj = case r["o"]["type"]
when "uri" then RDF::URI.intern(r["o"]["value"])
when "bnode" then RDF::Node.new(r["o"]["value"])
else
lit = RDF::Literal.new(r["o"]["value"])
lit.datatype = RDF::URI.new(r["o"]["datatype"]) if r["o"]["datatype"]
lit.language = r["o"]["language"].to_sym if r["o"]["language"]
lit
end
statements << RDF::Statement.new(RDF::URI.intern(r["s"]["value"]), RDF::URI.intern(r["p"]["value"]), obj, :context=>RDF::URI.intern(r["g"]["value"]))
end
statements
end
def self.method_missing(meth, *args)
if meth.to_s =~ /^find_by_/
sparql_find(meth, args)
end
end
def self.sparql_find(meth, args)
mod_meth = meth.to_s.sub(/^find_by_/,'')
params = mod_meth.split("_and_")
dataset = args.slice!(0,1)
meth_args = args.slice!(0,params.length)
options = args.first
conditions = []
(0..(params.length-1)).each do |i|
f = "FILTER ("
puts params[i]
f << case params[i]
when "subject" then "?s"
when "predicate" then "?p"
when "object" then "?o"
when "graph" then "?g"
end
f << " IN ("
vals = []
[*meth_args[i]].each do |v|
if v.respond_to?(:to_ntriples)
vals << v.to_ntriples
else
vals << RDF::Literal.new(v).to_ntriples
end
end
f << "#{vals.join(", ")}))"
conditions << f
end
options[:conditions] = conditions
self.all_statements(dataset, options)
end
end
class Sparql
def self.generate_select_all(options)
sparql = "SELECT ?g ?s ?p ?o \n"
sparql << "WHERE { GRAPH ?g {?s ?p ?o . \n"
if options[:conditions]
sparql << options[:conditions].join(" . \n")
end
sparql << "} }\n"
if options[:limit]
sparql << "LIMIT #{options[:limit]}\n"
end
if options[:offset]
sparql << "LIMIT #{options[:limit]}\n"
end
if options[:order]
sparql << "ORDER BY #{options[:order]}"
end
sparql
end
def self.generate_select_resource(options)
sparql = "SELECT ?g ?s ?p ?o \n"
sparql << "WHERE { GRAPH ?g {?s ?p ?o . \n"
if options[:conditions]
sparql << conditions.join(" . \n")
end
sparql << "} }\n"
if options[:limit]
sparql << "LIMIT #{options[:limit]}\n"
end
if options[:offset]
sparql << "LIMIT #{options[:limit]}\n"
end
if options[:order]
sparql << "ORDER BY #{options[:order]}"
end
sparql
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment