Permalink
Browse files

added sparql bypass

  • Loading branch information...
1 parent e69c3a9 commit e797131bbe7b92228454f3389e4ef66b51446413 @phifty committed Mar 27, 2010
@@ -0,0 +1,29 @@
+
+module AllegroGraph
+
+ module Proxy
+
+ class SparQL
+
+ attr_reader :server
+ attr_reader :repository
+
+ def initialize(repository)
+ @repository = repository
+ @server = @repository.server
+ end
+
+ def path
+ @repository.path
+ end
+
+ def perform(query)
+ parameters = { :query => query, :queryLn => "sparql" }
+ @server.request :get, self.path, :parameters => parameters, :expected_status_code => 200
+ end
+
+ end
+
+ end
+
+end
@@ -1,5 +1,6 @@
require File.join(File.dirname(__FILE__), "server")
require File.join(File.dirname(__FILE__), "proxy", "statements")
+require File.join(File.dirname(__FILE__), "proxy", "sparql")
module AllegroGraph
@@ -9,12 +10,14 @@ class Repository
attr_reader :catalog
attr_accessor :name
attr_reader :statements
+ attr_reader :sparql
def initialize(server_or_catalog, name, options = { })
@catalog = server_or_catalog.is_a?(AllegroGraph::Server) ? server_or_catalog.root_catalog : server_or_catalog
@server = @catalog.server
@name = name
@statements = Proxy::Statements.new self
+ @sparql = Proxy::SparQL.new self
end
def ==(other)
@@ -1,4 +1,5 @@
require 'uri'
+require 'cgi'
require 'net/http'
require 'base64'
require 'json'
@@ -48,7 +49,7 @@ def serialize_parameters
@serialized_parameters = if @parameters.nil? || @parameters.empty?
""
else
- "?" + @parameters.collect{ |key, value| "#{key}=#{URI.escape(value)}" }.reverse.join("&")
+ "?" + @parameters.collect{ |key, value| "#{key}=#{CGI.escape(value)}" }.reverse.join("&")
end
end
@@ -80,7 +81,7 @@ class UnexpectedStatusCodeError < StandardError
attr_reader :status_code
attr_reader :message
- def initialize(status_code, message = nil)
+ def initialize(status_code, message)
@status_code, @message = status_code, message
end
@@ -122,13 +123,6 @@ def initialize_headers
@headers["Content-Type"] = "application/json"
end
- def serialize_parameters
- @parameters.each do |key, value|
- @parameters[key] = value.to_json if value.respond_to?(:to_json)
- end
- super
- end
-
def initialize_request_object
super
@request.body = @body.to_json if @body
@@ -138,7 +132,7 @@ def check_status_code
return unless @expected_status_code
response_code = @response.code
response_body = @response.body
- raise UnexpectedStatusCodeError, response_code.to_i, response_body if @expected_status_code.to_s != response_code
+ raise UnexpectedStatusCodeError.new(response_code.to_i, response_body) if @expected_status_code.to_s != response_code
end
def parse_response
View
@@ -111,3 +111,16 @@
:response:
:code: "200"
:body: ""
+-
+ :http_method: "get"
+ :url: "http://localhost:10035/catalogs/test_catalog/repositories/test_repository"
+ :parameters:
+ :query: "SELECT ?subject WHERE { ?subject <http://xmlns.com/foaf/0.1/knows> ?object . }"
+ :queryLn: "sparql"
+ :response:
+ :code: "200"
+ :body:
+ "names": [ "subject" ]
+ "values":
+ - [ "\"another_subject\"" ]
+ - [ "\"test_subject\"" ]
@@ -30,7 +30,7 @@ def self.fake!
hash[:headers] == headers
end
raise StandardError, "no fake request found for [#{http_method} #{url} #{parameters.inspect} #{headers.inspect}]" unless request
- raise transport_class::UnexpectedStatusCodeError, request[:response][:code].to_i if expected_status_code && expected_status_code.to_s != request[:response][:code]
+ raise transport_class::UnexpectedStatusCodeError.new(request[:response][:code].to_i, request[:response][:body]) if expected_status_code && expected_status_code.to_s != request[:response][:code]
request[:response][:body].dup
end
end
@@ -99,7 +99,7 @@
end
it "should find statements by filter options" do
- statements = @statements.find :subject => "test_subject"
+ statements = @statements.find :subject => "\"test_subject\""
statements.should == [
[ "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\"" ]
]
@@ -123,4 +123,26 @@
end
+ describe "sparql" do
+
+ before :each do
+ @repository.create_if_missing!
+
+ statements = @repository.statements
+ statements.create "\"test_subject\"", "<http://xmlns.com/foaf/0.1/knows>", "\"test_object\"", "\"test_context\""
+ statements.create "\"another_subject\"", "<http://xmlns.com/foaf/0.1/knows>", "\"another_object\"", "\"test_context\""
+
+ @sparql = @repository.sparql
+ end
+
+ it "should respond to queried data" do
+ result = @sparql.perform "SELECT ?subject WHERE { ?subject <http://xmlns.com/foaf/0.1/knows> ?object . }"
+ result.should == {
+ "names" => [ "subject" ],
+ "values" => [ [ "\"another_subject\"" ], [ "\"test_subject\"" ] ]
+ }
+ end
+
+ end
+
end
@@ -0,0 +1,33 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "lib", "allegro_graph", "proxy", "sparql"))
+
+describe AllegroGraph::Proxy::SparQL do
+
+ before :each do
+ @server = AllegroGraph::Server.new :username => "test", :password => "test"
+ @catalog = AllegroGraph::Catalog.new @server, "test_catalog"
+ @repository = AllegroGraph::Repository.new @catalog, "test_repository"
+ @sparql = AllegroGraph::Proxy::SparQL.new @repository
+ end
+
+ describe "path" do
+
+ it "should return the correct path" do
+ @sparql.path.should == @repository.path
+ end
+
+ end
+
+ describe "perform" do
+
+ it "should return the query result" do
+ result = @sparql.perform "SELECT ?subject WHERE { ?subject <http://xmlns.com/foaf/0.1/knows> ?object . }"
+ result.should == {
+ "names" => [ "subject" ],
+ "values" => [ [ "\"another_subject\"" ], [ "\"test_subject\"" ] ]
+ }
+ end
+
+ end
+
+end

0 comments on commit e797131

Please sign in to comment.