|
| 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 | + var time = require('time'); |
| 21 | + |
| 22 | + function EventbriteNode(n) { |
| 23 | + RED.nodes.createNode(this, n); |
| 24 | + } |
| 25 | + RED.nodes.registerType("eventbrite-credentials",EventbriteNode,{ |
| 26 | + credentials: { |
| 27 | + appKey: { type:"text" }, |
| 28 | + apiUserKey: { type: "password" }, |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + EventbriteNode.prototype.request = function(req, cb) { |
| 33 | + if (typeof req !== 'object') { |
| 34 | + req = { url: "https://www.eventbrite.com/json/" + req }; |
| 35 | + } |
| 36 | + req.method = req.method || 'GET'; |
| 37 | + req.json = req.json || true; |
| 38 | + req.qs = req.qs || {}; |
| 39 | + req.qs.app_key = this.credentials.appKey; |
| 40 | + req.qs.user_key = this.credentials.apiUserKey; |
| 41 | + request(req, function(err, result, data) { cb(err, data); }); |
| 42 | + }; |
| 43 | + EventbriteNode.prototype.userListTickets = function(cb) { |
| 44 | + this.request('user_list_tickets', function(err, data) { |
| 45 | + if (err || data.error) { |
| 46 | + return cb(err, data); |
| 47 | + } |
| 48 | + /* For response details see: |
| 49 | + * http://developer.eventbrite.com/doc/users/user_list_tickets/ |
| 50 | + */ |
| 51 | + var res = []; |
| 52 | + var orders = flattenTrivialHashes(data.user_tickets[1].orders); |
| 53 | + for (var i = 0; i< orders.length; i++) { |
| 54 | + var o = orders[i]; |
| 55 | + var ev = o.event; |
| 56 | + if (ev.hasOwnProperty('start_date') && |
| 57 | + ev.hasOwnProperty('timezone')) { |
| 58 | + ev.start = new time.Date(ev.start_date, ev.timezone); |
| 59 | + } |
| 60 | + if (ev.hasOwnProperty('start_date') && |
| 61 | + ev.hasOwnProperty('timezone')) { |
| 62 | + ev.end = new time.Date(ev.end_date, ev.timezone); |
| 63 | + } |
| 64 | + if (o.hasOwnProperty('created')) { |
| 65 | + // no timezone is specified but it appears to be |
| 66 | + // -0700 |
| 67 | + o.ctime = new time.Date(o.created, o.timezone||'US/Pacific'); |
| 68 | + } |
| 69 | + if (o.hasOwnProperty('modified')) { |
| 70 | + // no timezone is specified but it appears to be |
| 71 | + // -0700 |
| 72 | + o.mtime = new time.Date(o.modified, o.timezone||'US/Pacific'); |
| 73 | + } |
| 74 | + res.push(o); |
| 75 | + } |
| 76 | + cb(null, res); |
| 77 | + }); |
| 78 | + }; |
| 79 | + |
| 80 | + /** |
| 81 | + * Flattens JSON generated from XML that contains: |
| 82 | + * |
| 83 | + * { "orders": [ { "order" : { k1: v1, k2: k2, ... } }, ... ] } |
| 84 | + * |
| 85 | + * to: |
| 86 | + * |
| 87 | + * { "orders": [ { k1: v1, k2: k2, ... }, ... ] } |
| 88 | + */ |
| 89 | + function flattenTrivialHashes(data) { |
| 90 | + if (data === null) { |
| 91 | + return data; |
| 92 | + } else if (Array.isArray(data)) { |
| 93 | + return data.map(flattenTrivialHashes); |
| 94 | + } else if (typeof data === 'object') { |
| 95 | + var k = Object.keys(data); |
| 96 | + if (k.length == 1) { |
| 97 | + return flattenTrivialHashes(data[k[0]]); |
| 98 | + } else { |
| 99 | + var res = {}; |
| 100 | + for (var key in data) { |
| 101 | + if (data.hasOwnProperty(key)) { |
| 102 | + res[key] = flattenTrivialHashes(data[key]); |
| 103 | + } |
| 104 | + } |
| 105 | + return res; |
| 106 | + } |
| 107 | + } else { |
| 108 | + return data; |
| 109 | + } |
| 110 | + } |
| 111 | + function EventbriteQueryNode(n) { |
| 112 | + RED.nodes.createNode(this,n); |
| 113 | + this.eventbrite = RED.nodes.getNode(n.eventbrite); |
| 114 | + |
| 115 | + var credentials = this.eventbrite.credentials; |
| 116 | + var node = this; |
| 117 | + if (credentials && credentials.appKey && credentials.apiUserKey) { |
| 118 | + this.on("input",function(msg) { |
| 119 | + node.status({fill:"blue",shape:"dot",text:"querying"}); |
| 120 | + node.eventbrite.userListTickets(function(err, data) { |
| 121 | + if (err) { |
| 122 | + node.error("Error: " + err); |
| 123 | + node.status({fill:"red",shape:"ring",text:"failed"}); |
| 124 | + return; |
| 125 | + } |
| 126 | + if (data.error) { |
| 127 | + node.status({fill:"red",shape:"ring",text:data.error.error_message}); |
| 128 | + return; |
| 129 | + } |
| 130 | + node.status({}); |
| 131 | + for (var i = 0; i < data.length; i++) { |
| 132 | + msg.payload = data[i]; |
| 133 | + node.send(msg); |
| 134 | + } |
| 135 | + }); |
| 136 | + }); |
| 137 | + } |
| 138 | + } |
| 139 | + RED.nodes.registerType("eventbrite",EventbriteQueryNode); |
| 140 | +}; |
0 commit comments