#ProtonCouch
It's a simple and efficient CouchDB client written on Ruby.
Current solutions like CouchRest are too heavy and have strange behaviour (like raising error when Resource not found). Also they can't manage many databases.
#How to use (real life examples from 8Protons' project)
It's simple. Create an object like this:
require 'protons8/couch'
@couch = Protons8::Couch.new({ :account => 'http://user:password@127.0.0.1:5984/accounts', :activations => 'http://user:password@127.0.0.1:5984/email_activations'})
Now you can query:
GET
@couch.get(:account, 'test@test.com')
PUT (create document with id)
@couch.save_with_id(db, id, document)
POST (create document with autogenerated id
@couch.save(db, document)
Save or Update (Document exists? Update. No? Create a new one)
@couch.save_or_update(db, id, document)
Simple DELETE
@couch.delete(db, id)
DELETE with revision
@couch.delete_rev(db, id, rev)
VIEW Example
Spec: view(db, view, params = {})
@couchdb.view(:accounts, 'accounts/login', { :key => ["login", "hashedpassword"] })
#Methods (Asking the Right Questions)
Every query wrapped by help class.
Base methods are:
- error?
- no_error?
- error_name
- reason
- not_found? = if CouchDB answered { "error": "not_found" }...
- unauth?
- conflict?
- raw = raw document (hash)
- attachments
GET methods
VIEW methods
- total_rows
- offset
- rows = get all rows
- any?
- empty? = no document found by criteria
PUT-POST-DELETE methods
- id = id of saved document
- rev
- ok? = is query okay?
#Real life examples
-
Check unique of email
if @couch.view(:account, "accounts/list_emails", { :key => email }).empty?
-
Save email activation code and check query
if @couch.save_or_update(:activation, email, { :code => @code }).ok?
-
Check email ectivation code
if @couch.view(:activation, 'email_activations/list_codes', { :key => params['code'] }).any?
-
User
user = @couch.save(:account, document)
if user.ok? and @couch.delete(:activation, document['email']).ok?
#Enjoy!
#License
The MIT License
Copyright (c) 2010 Anthony Sekatski <anthony@8protons.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.