working specy stuff i hope

This commit is contained in:
Chris Anderson 2008-03-18 11:37:10 -07:00
parent 2c42cf1ca5
commit 32c01fadad
7 changed files with 147 additions and 52 deletions

1
deps/rest-client vendored Submodule

@ -0,0 +1 @@
Subproject commit d484781d75ff5f986412103f9f68b6be6fc223fc

54
lib/couch_rest.rb Normal file
View file

@ -0,0 +1,54 @@
require File.dirname(__FILE__) + '/../deps/rest-client/lib/rest_client'
require "rubygems"
gem "json"
begin
require "json/ext"
rescue LoadError
$stderr.puts "C version of json (fjson) could not be loaded, using pure ruby one"
require "json/pure"
end
require File.dirname(__FILE__) + '/database'
class CouchRest
attr_accessor :uri
def initialize server
@uri = server
end
# list all databases on the server
def databases
CouchRest.get "#{@uri}/_all_dbs"
end
def database name
CouchRest::Database.new(@uri, name)
end
# get the welcome message
def info
CouchRest.get "#{@uri}/"
end
# create a database
def create_db name
CouchRest.put "#{@uri}/#{name}"
database name
end
class << self
def put uri, payload = nil
response = RestClient.put(uri, payload)
JSON.parse response
end
def get uri
response = RestClient.get(uri)
JSON.parse response
end
def delete uri
response = RestClient.delete(uri)
JSON.parse response
end
end
end

View file

@ -1,13 +0,0 @@
require 'uri'
class Couchrest
def initialize server
@server = URI.parse server
end
def databases
end
end

13
lib/database.rb Normal file
View file

@ -0,0 +1,13 @@
class CouchRest
class Database
attr_accessor :host, :name
def initialize host, name
@name = name
@host = host
end
def delete!
CouchRest.delete "#{host}/#{name}"
end
end
end

60
spec/couch_rest_spec.rb Normal file
View file

@ -0,0 +1,60 @@
require File.dirname(__FILE__) + '/../lib/couchrest'
describe CouchRest do
before(:each) do
@cr = CouchRest.new("http://local.grabb.it:5984")
db = @cr.database('couchrest-test')
begin
db.delete!
rescue RestClient::Request::RequestFailed
end
end
describe "getting info" do
it "should list databases" do
@cr.databases.should be_an_instance_of(Array)
end
it "should get info" do
@cr.info.should == {"couchdb"=>"Welcome", "version"=>"0.0.0"}
@cr.info.class.should == Hash
end
end
describe "initializing a database" do
it "should return a db" do
db = @cr.database('couchrest-test')
db.should be_an_instance_of(CouchRest::Database)
db.host.should == @cr.uri
end
end
describe "successfully creating a database" do
it "should start without a database" do
@cr.databases.should_not include('couchrest-test')
end
it "should return the created database" do
db = @cr.create_db('couchrest-test')
db.should be_an_instance_of(CouchRest::Database)
end
it "should create the database" do
db = @cr.create_db('couchrest-test')
@cr.databases.should include('couchrest-test')
end
end
describe "failing to create a database because the name is taken" do
before(:each) do
db = @cr.create_db('couchrest-test')
end
it "should start with the test database" do
@cr.databases.should include('couchrest-test')
end
it "should PUT the database and raise an error" do
lambda{
@cr.create_db('couchrest-test')
}.should raise_error(RestClient::Request::RequestFailed)
end
end
end

View file

@ -1,39 +0,0 @@
require File.dirname(__FILE__) + '/../lib/couchrest'
describe Couchrest do
before(:each) do
@cr = Couchrest.new("http://local.grabb.it:5984")
end
describe "getting status" do
it "should list databases" do
@cr.databases.should be_an Array
end
end
describe "successfully creating a database" do
it "should start without a database" do
end
it "should PUT the database" do
end
it "should return the created databse" do
end
end
describe "failing to create a database because the name is taken" do
it "should start without a database" do
end
it "should PUT the database and raise an error" do
end
it "should not result in another database" do
end
end
end

19
spec/database_spec.rb Normal file
View file

@ -0,0 +1,19 @@
require File.dirname(__FILE__) + '/../lib/couchrest'
describe Couchrest::Database do
before(:each) do
@cr = Couchrest.new("http://local.grabb.it:5984")
end
describe "deleting one" do
it "should start with the test database" do
@cr.databases.should include('couchrest-test')
end
it "should delete the database" do
db = @cr.database('couchrest-test')
r = db.delete!
r.should == ""
@cr.databases.should_not include('couchrest-test')
end
end
end