Skip to content
Browse files

extended transport layer

added catalog class
  • Loading branch information...
1 parent 839d6e2 commit c47fd6827d20d8f6038215bb7aa4abc0238755e5 @phifty committed Mar 26, 2010
View
33 lib/allegro_graph/catalog.rb
@@ -0,0 +1,33 @@
+
+module AllegroGraph
+
+ class Catalog
+
+ attr_reader :server
+ attr_reader :name
+
+ def initialize(server, name, options = { })
+ @server = server
+ @name = name
+ @root = options[:root] || false
+ end
+
+ def ==(other)
+ other.is_a?(self.class) && self.server == other.server && self.root? == other.root? && self.name == other.name
+ end
+
+ def url
+ self.root? ? @server.url : "#{@server.url}/catalogs/#{@name}"
+ end
+
+ def root?
+ !!@root
+ end
+
+ def repositories
+ [ ]
+ end
+
+ end
+
+end
View
40 lib/allegro_graph/server.rb
@@ -1,37 +1,53 @@
require File.join(File.dirname(__FILE__), "transport")
+require File.join(File.dirname(__FILE__), "catalog")
module AllegroGraph
- # The Server class provides methods to retrieve informations and statistics
- # of a AllegroGraph server.
+ # The Server class provides methods to retrieve informations about a AllegroGraph server.
class Server
attr_reader :host
attr_reader :port
+ attr_reader :username
+ attr_reader :password
+ attr_reader :root_catalog
def initialize(options = { })
- @host = options[:host] || "localhost"
- @port = options[:port] || "10035"
+ @host = options[:host] || "localhost"
+ @port = options[:port] || "10035"
+ @username = options[:username]
+ @password = options[:password]
+
+ @root_catalog = Catalog.new self, "root", :root => true
end
def ==(other)
other.is_a?(self.class) && @host == other.host && @port == other.port
end
- def version
- self.class.version self.url
+ def request(http_method, path, options = { })
+ JSONTransport.request http_method,
+ self.url + path,
+ { :auth_type => :basic, :username => self.username, :password => self.password }.merge(options)
end
- def url
- "http://#{@host}:#{@port}"
- end
-
- def self.version(base_url)
+ def version
+ base_url = self.url
{
:version => Transport.request(:get, base_url + "/version", :expected_status_code => 200),
:date => Transport.request(:get, base_url + "/version/date", :expected_status_code => 200),
:revision => Transport.request(:get, base_url + "/version/revision", :expected_status_code => 200)
- }
+ }
+ end
+
+ def catalogs
+ result = [ @root_catalog ]
+ catalogs = self.request :get, "/catalogs", :expected_status_code => 200
+ result
+ end
+
+ def url
+ "http://#{@host}:#{@port}"
end
end
View
30 lib/allegro_graph/transport.rb
@@ -1,11 +1,12 @@
require 'uri'
require 'net/http'
+require 'base64'
require 'json'
module AllegroGraph
# Common transport layer for http transfers.
- class Transport
+ module Transport
# The UnexpectedStatusCodeError is raised if the :expected_status_code option is given to
# the :request method and the responded status code is different from the expected one.
@@ -67,19 +68,34 @@ def self.check_status_code(response, expected_status_code)
end
+ module AuthorizedTransport
+
+ def self.request(http_method, url, options = { })
+ if options[:auth_type] == :basic
+ options[:headers] ||= { }
+ options[:headers]["Authorization"] = "Basic " + Base64.encode64("#{options[:username]}:#{options[:password]}")
+ end
+ Transport.request http_method, url, options
+ end
+
+ end
+
# Common json transport layer for http transfers.
- class JSONTransport < Transport
+ module JSONTransport
- def self.request(*arguments)
- parse super(*arguments)
+ def self.request(http_method, url, options = { })
+ options[:headers] ||= { }
+ options[:headers]["Content-Type"] = "application/json"
+ parse AuthorizedTransport.request(http_method, url, options)
end
def self.serialize_parameter_value(value)
- value.respond_to?(:to_json) ? value.to_json : super(value)
+ value.respond_to?(:to_json) ? value.to_json : AuthorizedTransport.serialize_parameter_value(value)
end
- def self.parse(response_body)
- JSON.parse response_body
+ def self.parse(response)
+ return nil if response.nil? || response == ""
+ JSON.parse response
end
end
View
9 spec/fake_transport.yml
@@ -16,3 +16,12 @@
:response:
:code: "200"
:body: "[unknown]"
+-
+ :http_method: "get"
+ :url: "http://localhost:10035/catalogs"
+ :headers:
+ "Authorization": "Basic dGVzdDp0ZXN0\n"
+ "Content-Type": "application/json"
+ :response:
+ :code: "200"
+ :body: ""
View
6 spec/fake_transport_helper.rb
@@ -10,10 +10,14 @@ def self.disable!
@enabled = false
end
+ def self.transport_class
+ AllegroGraph::Transport
+ end
+
def self.fake!
return unless @enabled
@@fake ||= YAML::load_file File.join(File.dirname(__FILE__), "fake_transport.yml")
- self.stub!(:request).and_return do |http_method, url, options|
+ transport_class.stub!(:request).and_return do |http_method, url, options|
options ||= { }
parameters = options[:parameters]
headers = options[:headers]
View
10 spec/integration/server_spec.rb
@@ -6,7 +6,7 @@
use_real_transport!
before :each do
- @server = AllegroGraph::Server.new
+ @server = AllegroGraph::Server.new :username => "test", :password => "test"
end
describe "version" do
@@ -21,4 +21,12 @@
end
+ describe "catalogs" do
+
+ it "should return the catalogs of the server" do
+ @server.catalogs.should == [ @server.root_catalog ]
+ end
+
+ end
+
end
View
57 spec/lib/allegro_graph/catalog_spec.rb
@@ -0,0 +1,57 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
+require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "lib", "allegro_graph", "catalog"))
+
+describe AllegroGraph::Catalog do
+
+ before :each do
+ @server = AllegroGraph::Server.new
+ @catalog = AllegroGraph::Catalog.new @server, "test_catalog"
+ end
+
+ describe "==" do
+
+ it "should be true when comparing two equal catalogs" do
+ other = AllegroGraph::Catalog.new @server, "test_catalog"
+ @catalog.should == other
+ end
+
+ it "should be false when comparing two different catalog" do
+ other = AllegroGraph::Catalog.new @server, "other_catalog"
+ @catalog.should_not == other
+ end
+
+ end
+
+ describe "url" do
+
+ context "for root catalog" do
+
+ before :each do
+ @catalog.stub!(:root?).and_return(true)
+ end
+
+ it "should the root catalog url" do
+ @catalog.url.should == "#{@server.url}"
+ end
+
+ end
+
+ context "for named catalog" do
+
+ it "should the named catalog url" do
+ @catalog.url.should == "#{@server.url}/catalogs/test_catalog"
+ end
+
+ end
+
+ end
+
+ describe "repositories" do
+
+ it "should return the catalog's repositories" do
+ @catalog.repositories.should == [ ]
+ end
+
+ end
+
+end
View
26 spec/lib/allegro_graph/server_spec.rb
@@ -4,8 +4,7 @@
describe AllegroGraph::Server do
before :each do
- FakeTransport.fake!
- @server = AllegroGraph::Server.new
+ @server = AllegroGraph::Server.new :username => "test", :password => "test"
end
describe "==" do
@@ -22,6 +21,21 @@
end
+ describe "request" do
+
+ before :each do
+ AllegroGraph::JSONTransport.stub!(:request)
+ end
+
+ it "should perform a authorized request" do
+ AllegroGraph::JSONTransport.should_receive(:request).with(
+ :get, "http://localhost:10035/", hash_including(:expected_status_code => 200)
+ ).and_return("test" => "test")
+ @server.request(:get, "/", :expected_status_code => 200).should == { "test" => "test" }
+ end
+
+ end
+
describe "version" do
it "should return the server's version" do
@@ -34,4 +48,12 @@
end
+ describe "catalogs" do
+
+ it "should return the catalogs of the server" do
+ @server.catalogs.should == [ @server.root_catalog ]
+ end
+
+ end
+
end
View
53 spec/lib/allegro_graph/transport_spec.rb
@@ -3,6 +3,8 @@
describe AllegroGraph::Transport do
+ use_real_transport!
+
describe "request" do
before :each do
@@ -54,8 +56,55 @@ def do_request(options = { })
end
it "should serialize the given parameters" do
- AllegroGraph::Transport.send(:serialize_parameters, @parameters).should ==
- "?another_test=test&test=1"
+ AllegroGraph::Transport.send(:serialize_parameters, @parameters).should == "?another_test=test&test=1"
+ end
+
+ end
+
+end
+
+describe AllegroGraph::AuthorizedTransport do
+
+ describe "request" do
+
+ before :each do
+ AllegroGraph::Transport.stub!(:request)
+ end
+
+ it "should call Transport.request with extended headers" do
+ AllegroGraph::Transport.should_receive(:request).with(
+ :get, "/", hash_including(:headers => { "Authorization" => "Basic dGVzdDp0ZXN0\n" })
+ )
+ AllegroGraph::AuthorizedTransport.request :get, "/", :auth_type => :basic, :username => "test", :password => "test"
+ end
+
+ end
+
+end
+
+describe AllegroGraph::JSONTransport do
+
+ describe "request" do
+
+ before :each do
+ AllegroGraph::AuthorizedTransport.stub!(:request)
+ end
+
+ it "should call Transport.request with extended headers" do
+ AllegroGraph::AuthorizedTransport.should_receive(:request).with(
+ :get, "/", hash_including(:headers => { "Content-Type" => "application/json" })
+ )
+ AllegroGraph::JSONTransport.request :get, "/"
+ end
+
+ it "should call AuthorizedTransport.request and parse the response" do
+ AllegroGraph::AuthorizedTransport.should_receive(:request).and_return("{\"test\":\"test\"}")
+ AllegroGraph::JSONTransport.request(:get, "/").should == { "test" => "test" }
+ end
+
+ it "should return nil if AuthorizedTransport.request returns nothing" do
+ AllegroGraph::AuthorizedTransport.should_receive(:request).and_return("")
+ AllegroGraph::JSONTransport.request(:get, "/").should be_nil
end
end

0 comments on commit c47fd68

Please sign in to comment.
Something went wrong with that request. Please try again.