class EventMachine::Synchrony::CouchDB

Public Class Methods

connect(connection_params={}) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 11
def self.connect(connection_params={})
  puts "*****Connecting" if $debug
  self.new(connection_params)
end
new(connection_params={}) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 16
def initialize(connection_params={})
  @host = connection_params[:host] || '127.0.0.1'
  @port = connection_params[:port] || 5984
  @timeout = connection_params[:timeout] || 10
end

Public Instance Methods

compact(db_name) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 40
def compact(db_name)
  post_request "/#{db_name}/_compact"
end
create_db(db_name) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 28
def create_db(db_name)
  put_request "/#{db_name}/"
end
delete(db_name, doc) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 61
def delete(db_name, doc)
  doc_id, doc_revision = get_id_and_revision(doc)
  delete_request "/#{db_name}/#{doc_id}?rev=#{doc_revision}"
end
delete_db(db_name) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 36
def delete_db(db_name)
  delete_request "/#{db_name}/"
end
get(db_name, id) click to toggle source

Document API

# File lib/em-synchrony/couchdb.rb, line 46
def get(db_name, id)
  get_request "/#{db_name}/#{id}", :timeout => @timeout
end
get_all_dbs() click to toggle source

DB API

# File lib/em-synchrony/couchdb.rb, line 24
def get_all_dbs
  get_request "/_all_dbs/", :timeout => @timeout
end
get_db(db_name) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 32
def get_db(db_name)
  get_request "/#{db_name}/", :timeout => @timeout
end
get_id_and_revision(doc) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 66
def get_id_and_revision(doc)
  if doc.has_key? "_id"
    return doc["_id"], doc["_rev"]
  else
    return doc["id"], doc["rev"]
  end
end
save(db_name, doc) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 50
def save(db_name, doc)
  post_request "/#{db_name}/", :body => JSON.dump(doc)
end
update(db_name, old_doc, new_doc) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 54
def update(db_name, old_doc, new_doc)
  id, rev = get_id_and_revision(old_doc)
  new_doc["_rev"] = rev
  new_doc["_id"] = id
  put_request "/#{db_name}/#{id}", :body => JSON.dump(new_doc)
end

Private Instance Methods

delete_request(path, options={}) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 88
def delete_request(path, options={})
  request :delete, path, options
end
get_request(path, options={}) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 76
def get_request(path, options={})
  request :get, path, options
end
post_request(path, options={}) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 80
def post_request(path, options={})
  request :post, path, options
end
put_request(path, options={}) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 84
def put_request(path, options={})
  request :put, path, options
end
request(type, path, options) click to toggle source
# File lib/em-synchrony/couchdb.rb, line 92
def request(type, path, options)
  uri = URI.parse("http://#{@host}:#{@port}#{path}")
  options = options.merge(:head => {"content-type" => "application/json"})
  conn = EventMachine::HttpRequest.new(uri).send type, options
  JSON.load conn.response
end