all specs pass; refined attachment api
This commit is contained in:
parent
bd2dafd107
commit
60c577963d
2 changed files with 30 additions and 11 deletions
|
@ -87,27 +87,28 @@ module CouchRest
|
||||||
end
|
end
|
||||||
|
|
||||||
# GET an attachment directly from CouchDB
|
# GET an attachment directly from CouchDB
|
||||||
def fetch_attachment(docid, name)
|
def fetch_attachment(doc, name)
|
||||||
slug = escape_docid(docid)
|
# slug = escape_docid(docid)
|
||||||
name = CGI.escape(name)
|
# name = CGI.escape(name)
|
||||||
RestClient.get "#{@uri}/#{slug}/#{name}"
|
|
||||||
|
uri = uri_for_attachment(doc, name)
|
||||||
|
|
||||||
|
RestClient.get uri
|
||||||
|
# "#{@uri}/#{slug}/#{name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# PUT an attachment directly to CouchDB
|
# PUT an attachment directly to CouchDB
|
||||||
def put_attachment(doc, name, file, options = {})
|
def put_attachment(doc, name, file, options = {})
|
||||||
docid = escape_docid(doc['_id'])
|
docid = escape_docid(doc['_id'])
|
||||||
name = CGI.escape(name)
|
name = CGI.escape(name)
|
||||||
uri = if doc['_rev']
|
uri = uri_for_attachment(doc, name)
|
||||||
"#{@uri}/#{docid}/#{name}?rev=#{doc['_rev']}"
|
|
||||||
else
|
|
||||||
"#{@uri}/#{docid}/#{name}"
|
|
||||||
end
|
|
||||||
JSON.parse(RestClient.put(uri, file, options))
|
JSON.parse(RestClient.put(uri, file, options))
|
||||||
end
|
end
|
||||||
|
|
||||||
# DELETE an attachment directly from CouchDB
|
# DELETE an attachment directly from CouchDB
|
||||||
def delete_attachment doc, name
|
def delete_attachment doc, name
|
||||||
uri = uri_for_attachment(doc, name)
|
uri = uri_for_attachment(doc, name)
|
||||||
|
# this needs a rev
|
||||||
JSON.parse(RestClient.delete(uri))
|
JSON.parse(RestClient.delete(uri))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -280,9 +281,17 @@ module CouchRest
|
||||||
private
|
private
|
||||||
|
|
||||||
def uri_for_attachment doc, name
|
def uri_for_attachment doc, name
|
||||||
docid = escape_docid(doc['_id'])
|
if doc.is_a?(String)
|
||||||
|
puts "CouchRest::Database#fetch_attachment will eventually require a doc as the first argument, not a doc.id"
|
||||||
|
docid = doc
|
||||||
|
rev = nil
|
||||||
|
else
|
||||||
|
docid = doc['_id']
|
||||||
|
rev = doc['_rev']
|
||||||
|
end
|
||||||
|
docid = escape_docid(docid)
|
||||||
name = CGI.escape(name)
|
name = CGI.escape(name)
|
||||||
rev = "?rev=#{doc['_rev']}" if doc['_rev']
|
rev = "?rev=#{doc['_rev']}" if rev
|
||||||
"#{@root}/#{docid}/#{name}#{rev}"
|
"#{@root}/#{docid}/#{name}#{rev}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,14 @@ module CouchRest
|
||||||
@@database
|
@@database
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def id
|
||||||
|
self['_id']
|
||||||
|
end
|
||||||
|
|
||||||
|
def rev
|
||||||
|
self['_rev']
|
||||||
|
end
|
||||||
|
|
||||||
# copies the document to a new id. If the destination id currently exists, a rev must be provided.
|
# copies the document to a new id. If the destination id currently exists, a rev must be provided.
|
||||||
# <tt>dest</tt> can take one of two forms if overwriting: "id_to_overwrite?rev=revision" or the actual doc
|
# <tt>dest</tt> can take one of two forms if overwriting: "id_to_overwrite?rev=revision" or the actual doc
|
||||||
# hash with a '_rev' key
|
# hash with a '_rev' key
|
||||||
|
@ -67,6 +75,7 @@ module CouchRest
|
||||||
|
|
||||||
# saves an attachment directly to couchdb
|
# saves an attachment directly to couchdb
|
||||||
def put_attachment(name, file, options={})
|
def put_attachment(name, file, options={})
|
||||||
|
raise ArgumentError, "doc must be saved" unless self.rev
|
||||||
raise ArgumentError, "doc.database required to put_attachment" unless database
|
raise ArgumentError, "doc.database required to put_attachment" unless database
|
||||||
result = database.put_attachment(self, name, file, options)
|
result = database.put_attachment(self, name, file, options)
|
||||||
self['_rev'] = result['rev']
|
self['_rev'] = result['rev']
|
||||||
|
@ -75,6 +84,7 @@ module CouchRest
|
||||||
|
|
||||||
# returns an attachment's data
|
# returns an attachment's data
|
||||||
def fetch_attachment(name)
|
def fetch_attachment(name)
|
||||||
|
raise ArgumentError, "doc must be saved" unless self.rev
|
||||||
raise ArgumentError, "doc.database required to put_attachment" unless database
|
raise ArgumentError, "doc.database required to put_attachment" unless database
|
||||||
database.fetch_attachment(self, name)
|
database.fetch_attachment(self, name)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue