Skip to content

Instantly share code, notes, and snippets.

@gkellogg
Created February 11, 2012 02:17
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 gkellogg/1795436 to your computer and use it in GitHub Desktop.
Save gkellogg/1795436 to your computer and use it in GitHub Desktop.
A Micro Gem for running a simple SPARQL endpoint

Micro SPARQL

A Ruby Micro Gem for creating a Sinatra endpoint using the Ruby SPARQL gem.

Description

Defines a Sinatra class used for responding to SPARQL requests with an empty in-memory repository.

Usage

Create a Gemfile containing the following:

gem micro-sparql, :git => "http://github.com/gkellogg/xxx.gist

Create a config.ru containing the following, along with environment-specific

#!/usr/bin/env rackup
$:unshift(File.expand_path("..", __FILE__))
require 'ruby gems' || Gem.clear_paths
require 'bundler'
Bundler.setup
require 'micro-sparql'
run MicroSparql

Dependencies

  • Sinatra (>= 1.3.2)
  • SPARQL (>= 0.3.5)
  • Linked Data (>= 0.3.5)
  • Soft dependency on Nokogiri (>= 1.3.3) Falls back to REXML for XML parsing Builder for XML serializing. Nokogiri is much more efficient
  • Soft dependency on Equivalent XML (>= 0.2.9) Equivalent XML performs more efficient comparisons of XML Literals when Nokogiri is included

Author

License

This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying {file:UNLICENSE} file.

#!/usr/bin/env rackup
$:.unshift(File.expand_path("..", __FILE__))
require 'rubygems' || Gem.clear_paths
require 'bundler'
Bundler.setup
require 'micro_sparql'
run MicroSparql
source "http://rubygems.org"
gemspec
group :development do
gem 'shotgun'
gem "wirble"
end
PATH
remote: .
specs:
micro-sparql (0.0.1)
equivalent-xml (>= 0.2.9)
linkeddata (>= 0.3.5)
sinatra (>= 1.3.2)
sparql (>= 0.1.0)
GEM
remote: http://rubygems.org/
specs:
addressable (2.2.8)
builder (3.0.0)
diff-lcs (1.1.3)
equivalent-xml (0.2.9)
nokogiri (>= 1.4.3)
haml (3.1.5)
htmlentities (4.3.1)
json (1.7.3)
json-ld (0.1.2)
json (>= 1.6.5)
rdf (>= 0.3.5)
json_pure (1.7.3)
linkeddata (0.3.5)
json-ld (>= 0.1.0)
rdf (>= 0.3.5)
rdf-isomorphic (>= 0.3.4)
rdf-json (>= 0.3.0)
rdf-microdata (>= 0.2.3)
rdf-n3 (>= 0.3.6)
rdf-rdfa (>= 0.3.9)
rdf-rdfxml (>= 0.3.5)
rdf-trig (>= 0.1.1)
rdf-trix (>= 0.3.0)
rdf-turtle (>= 0.1.1)
net-http-persistent (1.4.1)
nokogiri (1.5.2)
rack (1.4.1)
rack-protection (1.2.0)
rack
rack-test (0.6.1)
rack (>= 1.0)
rdf (0.3.5.2)
addressable (>= 2.2.6)
rdf-isomorphic (0.3.4)
rdf (>= 0.2.3)
rdf-json (0.3.0)
json_pure (>= 1.4.2)
rdf (~> 0.3.0)
rdf-microdata (0.2.4)
htmlentities (>= 4.3.0)
json (>= 1.6.1)
rdf (>= 0.3.4)
rdf-xsd (>= 0.3.4)
rdf-n3 (0.3.7)
rdf (>= 0.3.4)
rdf-rdfa (0.3.12)
haml (>= 3.0.0)
htmlentities (>= 4.3.0)
rdf (>= 0.3.3)
rdf-xsd (>= 0.3.5)
rdf-rdfxml (0.3.7)
rdf (>= 0.3.4)
rdf-xsd (>= 0.3.5)
rdf-trig (0.1.3)
rdf (>= 0.3.4)
rdf-turtle (>= 0.1.1)
rdf-trix (0.3.0)
rdf (~> 0.3.0)
rdf-turtle (0.1.2)
rdf (>= 0.3.4)
rdf-xsd (0.3.6)
nokogiri (>= 1.5.0)
rdf (>= 0.3.4)
rspec (2.10.0)
rspec-core (~> 2.10.0)
rspec-expectations (~> 2.10.0)
rspec-mocks (~> 2.10.0)
rspec-core (2.10.0)
rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)
shotgun (0.9)
rack (>= 1.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
sparql (0.3.0)
builder (>= 3.0.0)
json (>= 1.5.1)
rdf (>= 0.3.5)
rdf-xsd (>= 0.3.5)
sparql-client (>= 0.1.0)
sxp (>= 0.0.14)
sparql-client (0.1.0)
json_pure (>= 1.6.5)
net-http-persistent (~> 1.4.1)
rdf (>= 0.3.5)
sxp (0.0.14)
json (>= 1.5.1)
tilt (1.3.3)
wirble (0.1.3)
PLATFORMS
ruby
DEPENDENCIES
micro-sparql!
nokogiri (>= 1.5.0)
rack-test (>= 0.6.1)
rspec (>= 2.8)
shotgun
wirble
Gem::Specification.new do |s|
s.name = 'micro-sparql'
s.summary = 'A Ruby Micro Gem for creating a Sinatra endpoint using the Ruby SPARQL gem.'
s.description = %(Defines a Sinatra class used for responding to SPARQL requests with an empty in-memory repository.)
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
s.files = %w(micro_sparql.rb)
s.require_path = '.'
s.author = 'Gregg Kellogg'
s.email = 'gregg@greggkellogg.net'
s.homepage = 'http://greggkellogg.net'
s.test_file = 'micro_sparql_spec.rb'
s.add_runtime_dependency 'sinatra', '>= 1.3.2'
s.add_runtime_dependency 'linkeddata', '>= 0.3.5'
s.add_runtime_dependency 'sparql', '>= 0.1.0'
s.add_runtime_dependency 'equivalent-xml', '>= 0.2.9' if RUBY_ENGINE == "ruby"
s.add_development_dependency 'nokogiri' , '>= 1.5.0' if RUBY_ENGINE == "ruby"
s.add_development_dependency 'rspec', '>= 2.8'
s.add_development_dependency 'rack-test', '>= 0.6.1'
end
require 'linkeddata'
require 'sparql'
require 'sinatra/sparql'
class MicroSparql < Sinatra::Base
register Sinatra::SPARQL
get '/' do
settings.sparql_options.replace(:standard_prefixes => true)
repository = RDF::Repository.new
if params["query"]
query = params["query"].to_s.match(/^http:/) ? RDF::Util::File.open_file(params["query"]) : ::URI.decode(params["query"].to_s)
puts "query: #{query}"
SPARQL.execute(query, repository)
else
settings.sparql_options.merge!(:prefixes => {
:ssd => "http://www.w3.org/ns/sparql-service-description#",
:void => "http://rdfs.org/ns/void#"
})
service_description(:repo => repository, :endpoint => url)
end
end
end
require __FILE__.sub('_spec', '')
require 'bundler/setup'
require 'rspec'
require 'rspec/mocks'
require 'rack/test'
::RSpec.configure do |c|
c.filter_run :focus => true
c.run_all_when_everything_filtered = true
c.include ::Rack::Test::Methods
def app
MicroSparql
end
def mime_type(sym)
::Sinatra::Base.mime_type(sym)
end
end
describe MicroSparql do
describe 'get /' do
context 'with no query parameters' do
before(:all) { get '/', {}, {'HTTP_ACCEPT' => 'text/turtle'} }
specify { last_response.body.should match(/^@prefix ssd: <.*> \.$/)}
specify { last_response.body.should match(/\[ a ssd:Service;/)}
specify { last_response.status.should == 200}
specify { last_response.content_type.should == 'text/turtle'}
end
context 'with ?query=>"CONSTRUCT FROM.."' do
before(:each) do
g = RDF::Graph.new << [RDF::URI('a'), RDF::URI('b'), RDF::URI('c')]
RDF::Repository.any_instance.should_receive(:load).with("http://greggkellogg.net/foaf", anything).and_return(g)
get '/', {:query => "CONSTRUCT {?s ?p ?o} FROM <http://greggkellogg.net/foaf> WHERE {?s ?p ?o}"}
end
specify { last_response.body.should_not be_empty}
specify { last_response.status.should == 200}
specify { last_response.content_type.should == 'text/plain'}
end
context 'with ?query=>"SELECT FROM"' do
before(:each) do
g = RDF::Graph.new << [RDF::URI('a'), RDF::URI('b'), RDF::URI('c')]
RDF::Repository.any_instance.should_receive(:load).with("http://greggkellogg.net/foaf", anything).and_return(g)
get '/', {:query => "SELECT * FROM <http://greggkellogg.net/foaf> WHERE {?s ?p ?o}"}
end
specify { last_response.body.should_not be_empty}
specify { last_response.status.should == 200}
specify { last_response.content_type.should match(%r(^application/sparql-results))}
end
end
end
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment