Permalink
Browse files

refactored statements interface

  • Loading branch information...
1 parent 6ba00f3 commit e69c3a9fd70e5ea1f723ccd306aef1d748262e4f @phifty committed Mar 27, 2010
@@ -0,0 +1,54 @@
+
+module AllegroGraph
+
+ module Proxy
+
+ class Statements
+
+ attr_reader :server
+ attr_reader :repository
+
+ def initialize(repository)
+ @repository = repository
+ @server = @repository.server
+ end
+
+ def path
+ "#{@repository.path}/statements"
+ end
+
+ def create(subject, predicate, object, context = nil)
+ statement = [ subject, predicate, object ]
+ statement << context if context
+ @server.request :post, self.path, :body => [ statement ], :expected_status_code => 204
+ true
+ end
+
+ def find(options = { })
+ parameters = { }
+
+ { :subject => :subj, :predicate => :pred, :object => :obj, :context => :context }.each do |option_key, parameter_key|
+ value = options[option_key]
+ parameters.merge! value.is_a?(Array) ?
+ { :"#{parameter_key}" => value[0], :"#{parameter_key}End" => value[1] } :
+ { parameter_key => value } if value
+ end
+
+ [ :offset, :limit, :infer ].each do |key|
+ parameters.merge! key => options[key] if options.has_key?(key)
+ end
+
+ parameters = nil if parameters.empty?
+
+ @server.request :get, self.path, :parameters => parameters, :expected_status_code => 200
+ end
+
+ def delete
+ @server.request :delete, self.path, :expected_status_code => 200
+ end
+
+ end
+
+ end
+
+end
@@ -1,4 +1,5 @@
require File.join(File.dirname(__FILE__), "server")
+require File.join(File.dirname(__FILE__), "proxy", "statements")
module AllegroGraph
@@ -7,11 +8,13 @@ class Repository
attr_reader :server
attr_reader :catalog
attr_accessor :name
+ attr_reader :statements
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
+ @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
end
def ==(other)
@@ -55,34 +58,6 @@ def size
response.to_i
end
- def create_statement(subject, predicate, object, context = nil)
- statement = [ subject, predicate, object ]
- statement << context if context
- @server.request :post, self.path + "/statements", :body => [ statement ], :expected_status_code => 204
- true
- end
-
- def find_statements(options = { })
- parameters = { }
-
- { :subject => :subj, :predicate => :pred, :object => :obj, :context => :context }.each do |option_key, parameter_key|
- value = options[option_key]
- parameters.merge! value.is_a?(Array) ?
- { :"#{parameter_key}" => value[0], :"#{parameter_key}End" => value[1] } :
- { parameter_key => value } if value
- end
-
- [ :offset, :limit, :infer ].each do |key|
- parameters.merge! key => options[key] if options.has_key?(key)
- end
-
- @server.request :get, self.path + "/statements", :parameters => parameters, :expected_status_code => 200
- end
-
- def delete_statements(options = { })
- @server.request :delete, self.path + "/statements", :expected_status_code => 200
- end
-
end
end
@@ -88,3 +88,26 @@
:response:
:code: "204"
:body: "1"
+-
+ :http_method: "get"
+ :url: "http://localhost:10035/catalogs/test_catalog/repositories/test_repository/statements"
+ :response:
+ :code: "200"
+ :body:
+ - [ "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\"" ]
+ - [ "\"another_subject\"", "\"test_predicate\"", "\"another_object\"", "\"test_context\"" ]
+-
+ :http_method: "get"
+ :url: "http://localhost:10035/catalogs/test_catalog/repositories/test_repository/statements"
+ :parameters:
+ :subj: "test_subject"
+ :response:
+ :code: "200"
+ :body:
+ - [ "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\"" ]
+-
+ :http_method: "delete"
+ :url: "http://localhost:10035/catalogs/test_catalog/repositories/test_repository/statements"
+ :response:
+ :code: "200"
+ :body: ""
@@ -66,48 +66,60 @@
end
- describe "a repository" do
+ describe "statements" do
before :each do
@repository.create_if_missing!
+ @statements = @repository.statements
end
-
- it "should have a size of zero" do
- @repository.size.should == 0
- end
- it "should take a statement" do
- @repository.create_statement("\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\"").should be_true
+ describe "creation" do
+
+ it "should take a statement" do
+ result = @statements.create "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\""
+ result.should be_true
+ end
+
end
- context "filled with statements" do
+ describe "finding" do
before :each do
- @repository.delete_statements
- @repository.create_statement "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\""
- @repository.create_statement "\"another_subject\"", "\"test_predicate\"", "\"another_object\"", "\"test_context\""
- end
-
- it "should have a size of two or more" do
- @repository.size.should >= 2
+ @statements.delete
+ @statements.create "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\""
+ @statements.create "\"another_subject\"", "\"test_predicate\"", "\"another_object\"", "\"test_context\""
end
it "should find all statements" do
- statements = @repository.find_statements
+ statements = @statements.find
statements.should == [
[ "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\"" ],
[ "\"another_subject\"", "\"test_predicate\"", "\"another_object\"", "\"test_context\"" ]
]
end
it "should find statements by filter options" do
- statements = @repository.find_statements :subject => "test_subject"
+ statements = @statements.find :subject => "test_subject"
statements.should == [
[ "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\"" ]
]
end
end
+
+ describe "deletion" do
+
+ before :each do
+ @statements.create "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\""
+ end
+
+ it "should delete all statements" do
+ lambda do
+ @statements.delete
+ end.should change(@repository, :size).to(0)
+ end
+
+ end
end
@@ -0,0 +1,58 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "..", "lib", "allegro_graph", "proxy", "statements"))
+
+describe AllegroGraph::Proxy::Statements 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"
+ @statements = AllegroGraph::Proxy::Statements.new @repository
+ end
+
+ describe "path" do
+
+ it "should return the correct path" do
+ @statements.path.should == "#{@repository.path}/statements"
+ end
+
+ end
+
+ describe "create" do
+
+ it "should create a statement" do
+ result = @statements.create "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\""
+ result.should be_true
+ end
+
+ end
+
+ describe "find" do
+
+ it "should find all statements" do
+ result = @statements.find
+ result.should == [
+ [ "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\"" ],
+ [ "\"another_subject\"", "\"test_predicate\"", "\"another_object\"", "\"test_context\"" ]
+ ]
+ end
+
+ it "should find statements by filter options" do
+ result = @statements.find :subject => "test_subject"
+ result.should == [
+ [ "\"test_subject\"", "\"test_predicate\"", "\"test_object\"", "\"test_context\"" ]
+ ]
+ end
+
+ end
+
+ describe "delete" do
+
+ it "should delete all statements" do
+ result = @statements.delete
+ result.should be_true
+ end
+
+ end
+
+end
@@ -171,18 +171,4 @@
end
- describe "create_statement" do
-
- before :each do
- @subject = "test_subject"
- @predicate = "test_predicate"
- @object = "test_object"
- end
-
- it "should create a statement" do
- @repository.create_statement(@subject, @predicate, @object).should be_true
- end
-
- end
-
end

0 comments on commit e69c3a9

Please sign in to comment.