1 | //- JavaScript source code
|
2 |
|
3 | //- couch-api-ddoc.js ~~
|
4 | // ~~ (c) SRW, 23 Oct 2012
|
5 | // ~~ last updated 20 May 2013
|
6 |
|
7 | (function () {
|
8 | ;
|
9 |
|
10 | // Pragmas
|
11 |
|
12 | /*global exports: false, getRow: false, send: false */
|
13 |
|
14 | /*jshint maxparams: 2, quotmark: single, strict: true */
|
15 |
|
16 | /*jslint couch: true, indent: 4, maxlen: 80, nomen: true, unparam: true */
|
17 |
|
18 | /*properties
|
19 | 'as-array', body, box_status, 'Content-Type', data, _deleted, exp_date,
|
20 | hasOwnProperty, headers, _id, jobs, key, lists, map, outdated, parse,
|
21 | _rev, shows, split, stringify, updates, upsert, value, views
|
22 | */
|
23 |
|
24 | // Out-of-scope definitions
|
25 |
|
26 | exports._id = '_design/app';
|
27 |
|
28 | exports.lists = {
|
29 | 'as-array': function () {
|
30 | // This function needs documentation.
|
31 | var first, row;
|
32 | first = true;
|
33 | row = getRow();
|
34 | send('[');
|
35 | while (row !== null) {
|
36 | if (first === true) {
|
37 | first = false;
|
38 | send(JSON.stringify(row.value));
|
39 | } else {
|
40 | send(',' + JSON.stringify(row.value));
|
41 | }
|
42 | row = getRow();
|
43 | }
|
44 | send(']');
|
45 | return;
|
46 | }
|
47 | };
|
48 |
|
49 | exports.shows = {
|
50 | data: function (doc) {
|
51 | // This function needs documentation.
|
52 | var response;
|
53 | response = {
|
54 | headers: {
|
55 | 'Content-Type': 'application/json'
|
56 | }
|
57 | };
|
58 | if ((doc === null) || (doc.hasOwnProperty('body') === false)) {
|
59 | // If a document with the requested docid doesn't exist, return a
|
60 | // plain old JSON object to avoid giving [evil] robots any useful
|
61 | // information via 404s.
|
62 | response.body = '{}';
|
63 | } else {
|
64 | response.body = doc.body;
|
65 | }
|
66 | return response;
|
67 | }
|
68 | };
|
69 |
|
70 | exports.updates = {
|
71 | // NOTE: Do not out-clever yourself here! You _can_ add the CORS
|
72 | // headers in this function, but if you're already doing it inside an
|
73 | // external webserver like Nginx or Node.js, it will cause CORS _not_
|
74 | // to work.
|
75 | upsert: function (doc, req) {
|
76 | // This function needs documentation.
|
77 | var key, newDoc, response;
|
78 | newDoc = JSON.parse(req.body);
|
79 | response = {
|
80 | headers: {
|
81 | 'Content-Type': 'text/plain'
|
82 | },
|
83 | body: ''
|
84 | };
|
85 | if (doc === null) {
|
86 | // We are inserting a new document.
|
87 | return [newDoc, response];
|
88 | }
|
89 | // We are updating the existing document.
|
90 | for (key in newDoc) {
|
91 | if (newDoc.hasOwnProperty(key)) {
|
92 | doc[key] = newDoc[key];
|
93 | }
|
94 | }
|
95 | return [doc, response];
|
96 | }
|
97 | };
|
98 |
|
99 | exports.views = {
|
100 | jobs: {
|
101 | map: function (doc) {
|
102 | // This function needs documentation.
|
103 | var flag;
|
104 | flag = ((doc.hasOwnProperty('box_status')) &&
|
105 | (doc.hasOwnProperty('key')));
|
106 | if (flag === true) {
|
107 | emit(doc.box_status.split('&'), doc.key);
|
108 | }
|
109 | return;
|
110 | }
|
111 | },
|
112 | outdated: {
|
113 | map: function (doc) {
|
114 | // This function outputs a JSON array that I can modify slightly
|
115 | // and POST back to Couch via the Bulk Documents API in order to
|
116 | // delete all documents that are past their expiration dates.
|
117 | if (doc.hasOwnProperty('exp_date')) {
|
118 | emit(doc.exp_date, {
|
119 | _id: doc._id,
|
120 | _rev: doc._rev,
|
121 | _deleted: true
|
122 | });
|
123 | }
|
124 | return;
|
125 | }
|
126 | }
|
127 | };
|
128 |
|
129 | // That's all, folks!
|
130 |
|
131 | return;
|
132 |
|
133 | }());
|
134 |
|
135 | //- vim:set syntax=javascript:
|