added a url parser that CouchRest can use throughout (but is not yet using)

This commit is contained in:
Chris Anderson 2008-09-13 21:07:21 -04:00
parent de9fe45744
commit 26d50c8ac1
2 changed files with 81 additions and 0 deletions

View file

@ -41,6 +41,37 @@ module CouchRest
Server.new(*opts) Server.new(*opts)
end end
def parse url
case url
when /^http:\/\/(.*)\/(.*)\/(.*)/
host = $1
db = $2
docid = $3
when /^http:\/\/(.*)\/(.*)/
host = $1
db = $2
when /^http:\/\/(.*)/
host = $1
when /(.*)\/(.*)\/(.*)/
host = $1
db = $2
docid = $3
when /(.*)\/(.*)/
host = $1
db = $2
else
db = url
end
db = nil if db && db.empty?
{
:host => host || "localhost:5984",
:database => db,
:doc => docid
}
end
# ensure that a database exists # ensure that a database exists
# creates it if it isn't already there # creates it if it isn't already there
# returns it after it's been created # returns it after it's been created

View file

@ -42,6 +42,56 @@ describe CouchRest do
end end
end end
describe "parsing urls" do
it "should parse just a dbname" do
db = CouchRest.parse "my-db"
db[:database].should == "my-db"
db[:host].should == "localhost:5984"
end
it "should parse a host and db" do
db = CouchRest.parse "localhost/my-db"
db[:database].should == "my-db"
db[:host].should == "localhost"
end
it "should parse a host and db with http" do
db = CouchRest.parse "http://localhost/my-db"
db[:database].should == "my-db"
db[:host].should == "localhost"
end
it "should parse a host with a port and db" do
db = CouchRest.parse "localhost:5555/my-db"
db[:database].should == "my-db"
db[:host].should == "localhost:5555"
end
it "should parse a host with a port and db with http" do
db = CouchRest.parse "http://localhost:5555/my-db"
db[:database].should == "my-db"
db[:host].should == "localhost:5555"
end
it "should parse just a host" do
db = CouchRest.parse "http://localhost:5555/"
db[:database].should be_nil
db[:host].should == "localhost:5555"
end
it "should parse just a host no slash" do
db = CouchRest.parse "http://localhost:5555"
db[:host].should == "localhost:5555"
db[:database].should be_nil
end
it "should get docid" do
db = CouchRest.parse "localhost:5555/my-db/my-doc"
db[:database].should == "my-db"
db[:host].should == "localhost:5555"
db[:doc].should == "my-doc"
end
it "should get docid with http" do
db = CouchRest.parse "http://localhost:5555/my-db/my-doc"
db[:database].should == "my-db"
db[:host].should == "localhost:5555"
db[:doc].should == "my-doc"
end
end
describe "easy initializing a database adapter" do describe "easy initializing a database adapter" do
it "should be possible without an explicit CouchRest instantiation" do it "should be possible without an explicit CouchRest instantiation" do
db = CouchRest.database "http://localhost:5984/couchrest-test" db = CouchRest.database "http://localhost:5984/couchrest-test"