getting into view land

This commit is contained in:
Chris Anderson 2008-03-19 08:57:20 -07:00
parent d356e2356a
commit 097ab935c6
4 changed files with 83 additions and 16 deletions

View file

@ -36,19 +36,25 @@ class CouchRest
end
class << self
def put uri, payload = nil
response = RestClient.put(uri, payload)
JSON.parse response
def put uri, doc = nil
payload = JSON.unparse doc if doc
JSON.parse(RestClient.put(uri, payload))
end
def get uri
response = RestClient.get(uri)
JSON.parse response
JSON.parse(RestClient.get(uri))
end
def post uri, doc = nil
payload = JSON.unparse doc if doc
JSON.parse(RestClient.post(uri, payload))
end
def delete uri
response = RestClient.delete(uri)
JSON.parse response
JSON.parse(RestClient.delete(uri))
end
end
end

View file

@ -4,10 +4,29 @@ class CouchRest
def initialize host, name
@name = name
@host = host
@root = "#{host}/#{name}"
end
def documents
view "_all_docs"
end
def view name
CouchRest.get "#{@root}/#{name}"
end
def save doc
if doc['_id']
url = doc['_id']
CouchRest.put "#{@root}/#{doc['_id']}", doc
else
CouchRest.post "#{@root}", doc
end
end
def delete!
CouchRest.delete "#{host}/#{name}"
CouchRest.delete @root
end
end
end