Permalink
Browse files

Handles select, set, add, namespaces, and example files

  • Loading branch information...
1 parent 1b1505e commit 6c1e0b68d48682bae4d54cd47962e58264766dfd @moustaki committed Aug 4, 2009
Showing with 155 additions and 0 deletions.
  1. +8 −0 examples/add.rb
  2. +6 −0 examples/delete.rb
  3. +14 −0 examples/select.rb
  4. +8 −0 examples/set.rb
  5. +38 −0 lib/FourStore/namespace.rb
  6. +81 −0 lib/FourStore/store.rb
View
8 examples/add.rb
@@ -0,0 +1,8 @@
+require '../lib/FourStore/store'
+
+store = FourStore::Store.new 'http://dbtune.org/beancounter/sparql/'
+response = store.add('http://github.com/moustaki/4store-ruby', "
+ <http://moustaki.org/foaf.rdf#moustaki> foaf:nick \"moustaki\".
+");
+
+puts response
View
6 examples/delete.rb
@@ -0,0 +1,6 @@
+require '../lib/FourStore/store'
+
+store = FourStore::Store.new 'http://dbtune.org/beancounter/sparql/'
+response = store.delete('http://github.com/moustaki/4store-ruby')
+
+puts response
View
14 examples/select.rb
@@ -0,0 +1,14 @@
+require '../lib/FourStore/store'
+require 'pp'
+
+store = FourStore::Store.new 'http://dbtune.org/beancounter/sparql/'
+response = store.select("
+ SELECT ?subject ?predicate ?object
+ WHERE {
+ GRAPH <http://github.com/moustaki/4store-ruby> {
+ ?subject ?predicate ?object
+ }
+ }
+");
+
+pp response
View
8 examples/set.rb
@@ -0,0 +1,8 @@
+require '../lib/FourStore/store'
+
+store = FourStore::Store.new 'http://dbtune.org/beancounter/sparql/'
+response = store.set('http://github.com/moustaki/4store-ruby', "
+ <http://github.com/moustaki/4store-ruby> foaf:maker <http://moustaki.org/foaf.rdf#moustaki> .
+");
+
+puts response
View
38 lib/FourStore/namespace.rb
@@ -0,0 +1,38 @@
+module FourStore
+
+ class Namespace
+
+ @namespaces = {
+ 'dc' => 'http://purl.org/dc/elements/1.1/',
+ 'foaf' => 'http://xmlns.com/foaf/0.1/',
+ 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
+ 'xsd' => 'http://www.w3.org/2001/XMLSchema#'
+ }
+
+ def self.add(short, long)
+ @namespaces[short] = long
+ end
+
+ def self.get(short)
+ @namespaces[short]
+ end
+
+ def self.to_sparql()
+ sparql = ""
+ @namespaces.keys.each do |short|
+ sparql += "PREFIX #{short}: <#{@namespaces[short]}>\n"
+ end
+ sparql
+ end
+
+ def self.to_turtle()
+ turtle = ""
+ @namespaces.keys.each do |short|
+ turtle += "@prefix #{short}: <#{@namespaces[short]}>.\n"
+ end
+ turtle
+ end
+
+ end
+
+end
View
81 lib/FourStore/store.rb
@@ -0,0 +1,81 @@
+require 'uri'
+require 'net/http'
+require 'rexml/document'
+require File.expand_path(File.dirname(__FILE__)) + '/namespace'
+
+module FourStore
+
+ class Store
+
+ def initialize(endpoint)
+ @endpoint = URI.parse(endpoint)
+ @proxy = URI.parse(ENV['HTTP_PROXY']) if ENV['HTTP_PROXY']
+ end
+
+ def select(query)
+ http.start do |h|
+ request = Net::HTTP::Post.new(@endpoint.path)
+ request.set_form_data({ 'query' => Namespace::to_sparql + query })
+ response = h.request(request)
+ parse_sparql_xml_results(response.body)
+ end
+ end
+
+ def set(graph, turtle)
+ http.start do |h|
+ request = Net::HTTP::Put.new(@endpoint.path + graph)
+ request.body = Namespace::to_turtle + turtle
+ request.content_type = 'application/x-turtle'
+ response = h.request(request)
+ end
+ end
+
+ def add(graph, turtle)
+ http.start do |h|
+ request = Net::HTTP::Post.new(@endpoint.path.split("/sparql/")[0] + "/data/")
+ request.set_form_data({
+ 'graph' => graph,
+ 'data' => Namespace::to_turtle + turtle,
+ 'mime-type' => 'application/x-turtle'
+ })
+ response = h.request(request)
+ end
+ end
+
+ def delete(graph)
+ http.start do |h|
+ request = Net::HTTP::Delete.new(@endpoint.path + graph)
+ response = h.request(request)
+ end
+ end
+
+ private
+
+ def http
+ if @proxy
+ http = Net::HTTP::Proxy(@proxy.host, @proxy.port).new(@endpoint.host, @endpoint.port)
+ else
+ http = Net::HTTP.new(@endpoint.host, @endpoint.port)
+ end
+ http
+ end
+
+ def parse_sparql_xml_results(xml)
+ results = []
+ doc = REXML::Document.new xml
+ doc.elements.each("*/results/result") do |result|
+ result_hash = {}
+ result.elements.each do |binding|
+ key = binding.attributes["name"]
+ value = binding.elements[1].text
+ type = binding.elements[1].name
+ result_hash[key] = value
+ end
+ results.push result_hash
+ end
+ results
+ end
+
+ end
+
+end

0 comments on commit 6c1e0b6

Please sign in to comment.