2008-08-03 21:51:17 +02:00
|
|
|
class CouchRest
|
2008-09-07 22:51:26 +02:00
|
|
|
attr_accessor :uri, :uuid_batch_count
|
|
|
|
def initialize server = 'http://localhost:5984', uuid_batch_count = 1000
|
2008-08-03 21:51:17 +02:00
|
|
|
@uri = server
|
2008-09-07 22:51:26 +02:00
|
|
|
@uuid_batch_count = uuid_batch_count
|
2008-08-03 21:51:17 +02:00
|
|
|
end
|
|
|
|
|
2008-09-07 21:43:13 +02:00
|
|
|
# ensure that a database exists
|
|
|
|
# creates it if it isn't already there
|
|
|
|
# returns it after it's been created
|
|
|
|
def self.database! url
|
|
|
|
uri = URI.parse url
|
|
|
|
path = uri.path
|
|
|
|
uri.path = ''
|
|
|
|
cr = CouchRest.new(uri.to_s)
|
|
|
|
cr.database!(path)
|
|
|
|
end
|
|
|
|
|
2008-09-07 21:54:10 +02:00
|
|
|
def self.database url
|
|
|
|
uri = URI.parse url
|
|
|
|
path = uri.path
|
|
|
|
uri.path = ''
|
|
|
|
cr = CouchRest.new(uri.to_s)
|
|
|
|
cr.database(path)
|
|
|
|
end
|
|
|
|
|
2008-08-03 21:51:17 +02:00
|
|
|
# list all databases on the server
|
|
|
|
def databases
|
|
|
|
CouchRest.get "#{@uri}/_all_dbs"
|
|
|
|
end
|
|
|
|
|
|
|
|
def database name
|
2008-09-07 22:51:26 +02:00
|
|
|
CouchRest::Database.new(self, name)
|
2008-09-07 21:43:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# creates the database if it doesn't exist
|
|
|
|
def database! name
|
2008-09-07 21:54:10 +02:00
|
|
|
create_db(path) rescue nil
|
2008-09-07 22:51:26 +02:00
|
|
|
database name
|
2008-08-03 21:51:17 +02:00
|
|
|
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
|
|
|
|
|
2008-09-07 22:51:26 +02:00
|
|
|
# restart the couchdb instance
|
|
|
|
def restart!
|
|
|
|
CouchRest.post "#{@uri}/_restart"
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_uuid count = @uuid_batch_count
|
|
|
|
@uuids ||= []
|
|
|
|
if @uuids.empty?
|
|
|
|
@uuids = CouchRest.post("#{@uri}/_uuids?count=#{count}")["uuids"]
|
|
|
|
end
|
|
|
|
@uuids.pop
|
|
|
|
end
|
|
|
|
|
2008-08-03 21:51:17 +02:00
|
|
|
class << self
|
|
|
|
def put uri, doc = nil
|
|
|
|
payload = doc.to_json if doc
|
|
|
|
JSON.parse(RestClient.put(uri, payload))
|
|
|
|
end
|
|
|
|
|
|
|
|
def get uri
|
|
|
|
JSON.parse(RestClient.get(uri), :max_nesting => false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post uri, doc = nil
|
|
|
|
payload = doc.to_json if doc
|
|
|
|
JSON.parse(RestClient.post(uri, payload))
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete uri
|
|
|
|
JSON.parse(RestClient.delete(uri))
|
|
|
|
end
|
|
|
|
|
|
|
|
def paramify_url url, params = nil
|
|
|
|
if params
|
|
|
|
query = params.collect do |k,v|
|
|
|
|
v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
|
|
|
|
"#{k}=#{CGI.escape(v.to_s)}"
|
|
|
|
end.join("&")
|
|
|
|
url = "#{url}?#{query}"
|
|
|
|
end
|
|
|
|
url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|