This is a Ruby implementation of SPARQL for RDF.rb.
- 100% free and unencumbered public domain software.
- SPARQL 1.0 query parsing and execution
- SPARQL results as XML, JSON or HTML.
- SPARQL CONSTRUCT or DESCRIBE serialized based on Format, Extension of Mime Type using available RDF Writers (see Linked Data)
- SPARQL Client for accessing remote SPARQL endpoints.
- Rack and Sinatra middleware to perform HTTP content negotiation for result formats
- Compatible with any Rack or Sinatra application and any Rack-based framework.
- Helper method for describing SPARQL Service Description
- Compatible with Ruby 1.9.x.
- Compatible with older Ruby versions with the help of the Backports gem.
- Supports Unicode query strings both on Ruby 1.8.x and 1.9.x.
The {SPARQL} gem implements [SPARQL 1.0 Query] and provides Rack and Sinatra middleware to provide results using HTTP Content Negotiation.
- {SPARQL::Grammar} implements a [SPARQL 1.0 Query] parser generating SPARQL S-Expressions (SSE).
- {SPARQL::Algebra} executes SSE against Any
RDF::Graph
orRDF::Repository
, including compliant RDF.rb repository adaptors such as RDF::DO and RDF::Mongo. - {Rack::SPARQL} and {Sinatra::SPARQL} provide middleware components to format results using an appropriate format based on HTTP content negotiation.
Rack::SPARQL
is a superset of Rack::LinkedData to allow content negotiated results
to be returned any RDF::Enumerable
or RDF::Query::Solutions
compatible results.
You would typically return an instance of RDF::Graph
, RDF::Repository
or RDF::Query::Solutions
from your Rack application, and let the Rack::SPARQL::ContentNegotiation
middleware
take care of serializing your response into whatever format the HTTP
client requested and understands.
Sinatra::SPARQL
is a thin Sinatra-specific wrapper around the
{Rack::SPARQL} middleware, which implements SPARQL
content negotiation for Rack applications.
The middleware queries RDF.rb for the MIME content types of known RDF serialization formats, so it will work with whatever serialization plugins that are currently available for RDF.rb. (At present, this includes support for N-Triples, N-Quads, Turtle, RDF/XML, RDF/JSON, JSON-LD, RDFa, TriG and TriX.)
require 'rubygems'
require 'sparql'
queryable = RDF::Repository.load("etc/doap.ttl")
sse = SPARQL.parse("SELECT * WHERE { ?s ?p ?o }")
sse.execute(queryable)
queryable = RDF::Repository.load("etc/doap.ttl")
solutions = SPARQL.execute("SELECT * WHERE { ?s ?p ?o }", queryable)
solutions.to_json #to_xml #to_html
sse = SPARQL.parse("SELECT * WHERE { ?s ?p ?o }")
sse.to_sxp
sparql --default-graph etc/doap.ttl etc/from_default.rq
sparql -e "SELECT * FROM <etc/doap.ttl> WHERE { ?s ?p ?o }"
# Generate SPARQL Algebra Expression (SSE) format
sparql --to-sse etc/input.rq
sparql --to-sse -e "SELECT * WHERE { ?s ?p ?o }"
# Run query using SSE input
sparql --default-graph etc/doap.ttl --sse etc/input.sse
sparql --sse -e "(dataset (<etc/doap.ttl>) (bgp (triple ?s ?p ?o))))"
# config/application.rb
require 'rack/sparql'
class Application < Rails::Application
config.middleware.use Rack::SPARQL::ContentNegotiation
end
#!/usr/bin/env rackup
require 'rack/sparql'
repository = RDF::Repository.new do |graph|
graph << [RDF::Node.new, RDF::DC.title, "Hello, world!"]
end
results = SPARQL.execute("SELECT * WHERE { ?s ?p ?o }", repository)
use Rack::SPARQL::ContentNegotiation
run lambda { |env| [200, {}, results] }
#!/usr/bin/env ruby -rubygems
require 'sinatra'
require 'sinatra/sparql'
repository = RDF::Repository.new do |graph|
graph << [RDF::Node.new, RDF::DC.title, "Hello, world!"]
end
get '/sparql' do
SPARQL.execute("SELECT * WHERE { ?s ?p ?o }", repository)
end
Full documentation available on Rubydoc.info
- {SPARQL}
- {SPARQL::Algebra}
- {SPARQL::Algebra::Expression}
- {SPARQL::Algebra::Query}
- {SPARQL::Algebra::Operator}
- {SPARQL::Grammar}
- {SPARQL::Grammar::Parser}
- {SPARQL::Grammar::Lexer}
- {SPARQL::Algebra}
- {Sinatra::SPARQL}
- {Rack::SPARQL}
- {Rack::SPARQL::ContentNegotiation}
- Ruby (>= 1.9) or (>= 1.8.1 with Backports)
- RDF.rb (>= 0.3.4)
- SPARQL::Client (>= 0.0.11)
- SXP (>= 0.0.15)
- Builder (>= 3.0.0)
- JSON (>= 1.5.1)
The recommended installation method is via RubyGems.
To install the latest official release of the SPARQL
gem, do:
% [sudo] gem install sparql
To get a local working copy of the development repository, do:
% git clone git://github.com/gkellogg/sparql.git
- Gregg Kellogg - http://kellogg-assoc.com/
- Arto Bendiken - http://ar.to/
- Pius Uzamere - http://pius.me/
- Do your best to adhere to the existing coding conventions and idioms.
- Don't use hard tabs, and don't leave trailing whitespace on any line.
- Do document every method you add using YARD annotations. Read the tutorial or just look at the existing code for examples.
- Don't touch the
.gemspec
,VERSION
orAUTHORS
files. If you need to change them, do so on your private branch only. - Do feel free to add yourself to the
CREDITS
file and the corresponding list in the theREADME
. Alphabetical order applies. - Do note that in order for us to merge any non-trivial changes (as a rule of thumb, additions larger than about 15 lines of code), we need an explicit public domain dedication on record from you.
This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying {file:UNLICENSE} file.