|
| 1 | +/** |
| 2 | + * Copyright 2014 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | + |
| 17 | +module.exports = function(RED) { |
| 18 | + "use strict"; |
| 19 | + var request = require("request"); |
| 20 | + |
| 21 | + function DeliciousUserNode(n) { |
| 22 | + RED.nodes.createNode(this,n); |
| 23 | + this.username = n.username; |
| 24 | + } |
| 25 | + RED.nodes.registerType("delicious-user",DeliciousUserNode,{ |
| 26 | + credentials: { |
| 27 | + password: { type:"password"} |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + |
| 32 | + function DeliciousOutNode(n) { |
| 33 | + RED.nodes.createNode(this,n); |
| 34 | + var node = this; |
| 35 | + this.tags = n.tags; |
| 36 | + this.toread = n.toread; |
| 37 | + this.private = n.private; |
| 38 | + |
| 39 | + this.user = RED.nodes.getNode(n.user); |
| 40 | + if (this.user) { |
| 41 | + this.on("input", function(msg) { |
| 42 | + if (!msg.payload) { |
| 43 | + node.warn("url must be provided in msg.payload"); |
| 44 | + return; |
| 45 | + } |
| 46 | + if (!msg.title) { |
| 47 | + node.warn("msg.title must be provided"); |
| 48 | + } |
| 49 | + var options = { |
| 50 | + url: "https://api.delicious.com/v1/posts/add?"+ |
| 51 | + "url="+encodeURIComponent(msg.payload)+ |
| 52 | + "&description="+encodeURIComponent(msg.title)+ |
| 53 | + "&auth_token="+node.user.credentials.token+ |
| 54 | + "&shared="+((node.private !== false)?"no":"yes"), |
| 55 | + auth: { |
| 56 | + user: node.user.username, |
| 57 | + password: node.user.credentials.password |
| 58 | + } |
| 59 | + } |
| 60 | + // TODO: allow tags to be added by the message |
| 61 | + if (node.tags) { |
| 62 | + options.url += "&tags="+encodeURIComponent(node.tags) |
| 63 | + } |
| 64 | + if (msg.description) { |
| 65 | + options.url += "&extended="+encodeURIComponent(msg.description) |
| 66 | + } |
| 67 | + |
| 68 | + node.status({fill:"blue",shape:"dot",text:"saving"}); |
| 69 | + |
| 70 | + request.get(options, function(err,res,body) { |
| 71 | + if (err) { |
| 72 | + node.error(err); |
| 73 | + node.status({fill:"red",shape:"ring",text:"failed"}); |
| 74 | + } else { |
| 75 | + if (body.indexOf('code="done"') != -1) { |
| 76 | + node.status({}); |
| 77 | + } else { |
| 78 | + //TODO: This API only returns XML. Need to parse out error messages |
| 79 | + node.error(body); |
| 80 | + node.status({fill:"red",shape:"ring",text:"failed"}); |
| 81 | + } |
| 82 | + } |
| 83 | + }).on('error',function(err) { |
| 84 | + node.error(err); |
| 85 | + node.status({fill:"red",shape:"ring",text:err.code}); |
| 86 | + }); |
| 87 | + |
| 88 | + }); |
| 89 | + } else { |
| 90 | + this.error("missing credentials"); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + RED.nodes.registerType("delicious out",DeliciousOutNode); |
| 95 | +} |
0 commit comments