views:

292

answers:

2

Has anybody succeeded in sending standalone attachments using the standalone attachment API (if possible gziped) to couchDB from ruby?
I know there are a few CURL examples, but my tries with Typhoeus haven't been successful so far. It usually just stops and waits for > 1 minute after the first few documents.
CouchRest doesn't seem to support it, neither do any of the other libraries I've looked at

edit: clarification I'm NOT looking for regular Base64 encoded attachments. CouchRest does that pretty fine.

+1  A: 

Got it working with typhoeus

Typhoeus::Request.put("http://127.0.0.1:5984/db/document/my_attachment_name?rev=#{rev}", "content-type" => "text/html", "Content-Encoding" => "gzip", "Accept-Encoding" => "gzip", :body => my_html_body)

this will store the "my_html_body" string into couchDB as a gziped standalone attachment

Marc Seeger
A: 

For a binary standalone attachment, I just used IO.read("/path/to/my/file") to give a string to the put method as the :body. It looks like it is working but I don't know if it is the right way to do it.

It looks like this:

  res = Typhoeus::Request.get("http://localhost:5984/_uuids")
  uuid = JSON.parse(res.body)["uuids"].first
  doc = {}
  doc["name"] = name
  ...
  res = Typhoeus::Request.put("http://localhost:5984/products/#{uuid}", :body => JSON.generate(doc))
  res = Typhoeus::Request.put("http://localhost:5984/products/#{uuid}/image.jpg?rev=#{rev}", :headers => {"Content-Type" => "image/jpeg" }, :body => IO.read("output/images/#{image}"))
Ludovic Kuty