Skip to content

Instantly share code, notes, and snippets.

@sr
Created August 31, 2008 17:12
Show Gist options
  • Save sr/8200 to your computer and use it in GitHub Desktop.
Save sr/8200 to your computer and use it in GitHub Desktop.
module CouchRest
class Database
attr_accessor :name, :server
def initialize(server, database_name)
@name = database_name
@server = server
end
def documents(params=nil)
server.get('/_all_docs', params)
end
def temp_view(funcs, params={})
server.put("/#{name}/_temp_view", params,
'Content-Type' => 'application/json',
function.to_json
)
end
def view(name, params={})
server.get("/#{name}/_view/#{name}", params)
end
# experimental
def search(params={})
server.get("/#{name}/_search", params)
end
# experimental
def action(action, params={})
server.get("/#{name}/_action/#{action}", params)
end
def get(id)
server.get("/#{name}/#{CGI.escape(id)}")
end
end
end
module CouchRest
class Server
attr_accessor :uri
def initialize(server='http://localhost:5984')
@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
# create a database
def create_db(name)
CouchRest.put "#{@uri}/#{name}"
database name
end
# get the welcome message
def info
CouchRest.get "#{@uri}/"
end
# restart the couchdb instance
def restart!
CouchRest.post "#{@uri}/_restart"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment