made CouchRest a module

This commit is contained in:
Chris Anderson 2008-09-11 21:14:34 -07:00
parent 26de4acc5a
commit f5fdc8b913
10 changed files with 110 additions and 114 deletions

View file

@ -0,0 +1,105 @@
require 'cgi'
require "base64"
module CouchRest
class Database
attr_reader :server, :host, :name, :root
def initialize server, name
@name = name
@server = server
@host = server.uri
@root = "#{host}/#{name}"
end
def to_s
@root
end
def info
CouchRest.get @root
end
def documents params = nil
url = CouchRest.paramify_url "#{@root}/_all_docs", params
CouchRest.get url
end
def temp_view funcs, params = nil
url = CouchRest.paramify_url "#{@root}/_temp_view", params
JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'}))
end
def view name, params = nil
url = CouchRest.paramify_url "#{@root}/_view/#{name}", params
CouchRest.get url
end
# experimental
def search params = nil
url = CouchRest.paramify_url "#{@root}/_search", params
CouchRest.get url
end
# experimental
def action action, params = nil
url = CouchRest.paramify_url "#{@root}/_action/#{action}", params
CouchRest.get url
end
def get id
slug = CGI.escape(id)
CouchRest.get "#{@root}/#{slug}"
end
def fetch_attachment doc, name
doc = CGI.escape(doc)
name = CGI.escape(name)
RestClient.get "#{@root}/#{doc}/#{name}"
end
# PUT or POST depending on presence of _id attribute
def save doc
if doc['_attachments']
doc['_attachments'] = encode_attachments(doc['_attachments'])
end
if doc['_id']
slug = CGI.escape(doc['_id'])
else
slug = doc['_id'] = @server.next_uuid
end
CouchRest.put "#{@root}/#{slug}", doc
end
def bulk_save docs
ids, noids = docs.partition{|d|d['_id']}
uuid_count = [noids.length, @server.uuid_batch_count].max
noids.each do |doc|
doc['_id'] = @server.next_uuid(uuid_count)
end
CouchRest.post "#{@root}/_bulk_docs", {:docs => docs}
end
def delete doc
slug = CGI.escape(doc['_id'])
CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}"
end
def delete!
CouchRest.delete @root
end
private
def encode_attachments attachments
attachments.each do |k,v|
next if v['stub']
v['data'] = base64(v['data'])
end
attachments
end
def base64 data
Base64.encode64(data).gsub(/\s/,'')
end
end
end

View file

@ -0,0 +1,98 @@
module CouchRest
class Server
attr_accessor :uri, :uuid_batch_count
def initialize server = 'http://localhost:5984', uuid_batch_count = 1000
@uri = server
@uuid_batch_count = uuid_batch_count
end
# 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
def self.database url
uri = URI.parse url
path = uri.path
uri.path = ''
cr = CouchRest.new(uri.to_s)
cr.database(path)
end
# list all databases on the server
def databases
CouchRest.get "#{@uri}/_all_dbs"
end
def database name
CouchRest::Database.new(self, name)
end
# creates the database if it doesn't exist
def database! name
create_db(name) rescue nil
database 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
# 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
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
end