Permalink
Browse files

refactor into couchapp

  • Loading branch information...
1 parent e9c03b4 commit 0bdc64657f6bf01e054ceb127f53e5b3890f4de5 Dale Harvey committed Jul 23, 2011
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,52 @@
+
+var request = require('request');
+
+function process(ctx, doc) {
+
+ if (doc.opts && doc.destination) {
+
+ request(doc.opts, function(err, resp, body) {
+
+ var result =
+ { time: (new Date().getTime())
+ , uri: doc.opts.uri
+ , statusCode: resp.statusCode
+ , headers:resp.headers
+ , body: body
+ };
+
+ if (doc.opaque) {
+ result.opaque = doc.opaque;
+ }
+
+ var destination = require("url").parse(doc.destination);
+ var uri = (!destination.protocol) ?
+ ctx.uri.protocol + "//" + ctx.uri.host + "/" + doc.destination :
+ doc.destination;
+
+ request(
+ { method: 'PUT'
+ , uri: uri
+ }, function(err, resp, body) {
+ if (resp.statusCode === 201 || resp.statusCode === 412) {
+ request(
+ { method: 'POST'
+ , headers: {'content-type':"application/json"}
+ , uri: uri
+ , body: JSON.stringify(result)
+ }, function(err, resp, body) {
+ if (resp.statusCode === 201) {
+ console.log("Saved webpage " + doc.opts.uri + " to " + uri);
+ }
+ }
+ );
+ }
+ }
+ );
+ });
+
+ }
+
+}
+
+exports.process = process;

0 comments on commit 0bdc646

Please sign in to comment.