|
| 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 querystring = require('querystring'); |
| 21 | + |
| 22 | + function EventbriteNode(n) { |
| 23 | + RED.nodes.createNode(this, n); |
| 24 | + } |
| 25 | + RED.nodes.registerType("eventbrite config",EventbriteNode,{ |
| 26 | + credentials: { |
| 27 | + appKey: { type:"text" }, |
| 28 | + apiUserKey: { type: "password" }, |
| 29 | + } |
| 30 | + }); |
| 31 | + function eventbriteRequest(method, options, callback) { |
| 32 | + var url = "https://www.eventbrite.com/json/" + method + |
| 33 | + '?' + querystring.stringify(options); |
| 34 | + request.get({url:url, json:true}, callback); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Flattens JSON generated from XML that contains: |
| 39 | + * |
| 40 | + * { "orders": [ { "order" : { k1: v1, k2: k2, ... } }, ... ] } |
| 41 | + * |
| 42 | + * to: |
| 43 | + * |
| 44 | + * { "orders": [ { k1: v1, k2: k2, ... }, ... ] } |
| 45 | + */ |
| 46 | + function flattenTrivialHashes(data) { |
| 47 | + if (data === null) { |
| 48 | + return data; |
| 49 | + } else if (Array.isArray(data)) { |
| 50 | + return data.map(flattenTrivialHashes); |
| 51 | + } else if (typeof data === 'object') { |
| 52 | + var k = Object.keys(data); |
| 53 | + if (k.length == 1) { |
| 54 | + return flattenTrivialHashes(data[k[0]]); |
| 55 | + } else { |
| 56 | + var res = {}; |
| 57 | + for (var key in data) { |
| 58 | + if (data.hasOwnProperty(key)) { |
| 59 | + res[key] = flattenTrivialHashes(data[key]); |
| 60 | + } |
| 61 | + } |
| 62 | + return res; |
| 63 | + } |
| 64 | + } else { |
| 65 | + return data; |
| 66 | + } |
| 67 | + } |
| 68 | + function EventbriteQueryNode(n) { |
| 69 | + RED.nodes.createNode(this,n); |
| 70 | + var credentials = RED.nodes.getCredentials(n.eventbrite); |
| 71 | + var node = this; |
| 72 | + if (credentials && credentials.appKey && credentials.apiUserKey) { |
| 73 | + this.on("input",function(msg) { |
| 74 | + node.status({fill:"blue",shape:"dot",text:"querying"}); |
| 75 | + eventbriteRequest('user_list_tickets', {'app_key': credentials.appKey, 'user_key': credentials.apiUserKey}, function(err, res, data) { |
| 76 | + if (err) { |
| 77 | + node.error("Error: " + err); |
| 78 | + node.status({fill:"red",shape:"ring",text:"failed"}); |
| 79 | + return; |
| 80 | + } |
| 81 | + if (data.error) { |
| 82 | + node.status({fill:"red",shape:"ring",text:data.error.error_message}); |
| 83 | + } else { |
| 84 | + node.status({}); |
| 85 | + } |
| 86 | + /* See: |
| 87 | + * http://developer.eventbrite.com/doc/users/user_list_tickets/ |
| 88 | + * for response details. |
| 89 | + */ |
| 90 | + var orders = flattenTrivialHashes(data.user_tickets[1].orders); |
| 91 | + msg.payload = orders; |
| 92 | + node.send(msg); |
| 93 | + }); |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | + RED.nodes.registerType("eventbrite",EventbriteQueryNode); |
| 98 | +}; |
0 commit comments