1 | var serial,__hasProp = {}.hasOwnProperty;
|
2 |
|
3 | serial = 0;
|
4 |
|
5 | Payload.prototype.locale_format = /^[a-z]{2}_[A-Z]{2}$/;
|
6 |
|
7 | function Payload(data) {
|
8 | var key, prefix, subkey, sum, type, value, _i, _len, _ref, _ref1;
|
9 | if (typeof data !== 'object') {
|
10 | throw new Error('Invalid payload');
|
11 | }
|
12 | this.id = serial++;
|
13 | this.compiled = false;
|
14 | this.title = {};
|
15 | this.msg = {};
|
16 | this.data = {};
|
17 | this["var"] = {};
|
18 | for (key in data) {
|
19 | if (!__hasProp.call(data, key)) continue;
|
20 | value = data[key];
|
21 | if (typeof key !== 'string' || key.length === 0) {
|
22 | throw new Error("Invalid field (empty)");
|
23 | }
|
24 | if (typeof value !== 'string') {
|
25 | throw new Error("Invalid value for `" + key + "'");
|
26 | }
|
27 | switch (key) {
|
28 | case 'title':
|
29 | this.title["default"] = value;
|
30 | break;
|
31 | case 'msg':
|
32 | this.msg["default"] = value;
|
33 | break;
|
34 | case 'sound':
|
35 | this.sound = value;
|
36 | break;
|
37 | default:
|
38 | if ((_ref = key.split('.', 2), prefix = _ref[0], subkey = _ref[1], _ref).length === 2) {
|
39 | this[prefix][subkey] = value;
|
40 | } else {
|
41 | throw new Error("Invalid field: " + key);
|
42 | }
|
43 | }
|
44 | }
|
45 | sum = 0;
|
46 | _ref1 = ['title', 'msg', 'data'];
|
47 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
48 | type = _ref1[_i];
|
49 | sum += ((function () {
|
50 | var _ref2, _results;
|
51 | _ref2 = this[type];
|
52 | _results = [];
|
53 | for (key in _ref2) {
|
54 | if (!__hasProp.call(_ref2, key)) continue;
|
55 | _results.push(key);
|
56 | }
|
57 | return _results;
|
58 | }).call(this)).length;
|
59 | }
|
60 | if (sum === 0) {
|
61 | throw new Error('Empty payload');
|
62 | }
|
63 | }
|
64 |
|
65 | Payload.prototype.localizedTitle = function (lang) {
|
66 | return this.localized('title', lang);
|
67 | };
|
68 |
|
69 | Payload.prototype.localizedMessage = function (lang) {
|
70 | return this.localized('msg', lang);
|
71 | };
|
72 |
|
73 | Payload.prototype.localized = function (type, lang) {
|
74 | if (!this.compiled) {
|
75 | this.compile();
|
76 | }
|
77 | if (this[type][lang] != null) {
|
78 | return this[type][lang];
|
79 | } else if (Payload.prototype.locale_format.test(lang) && (this[type][lang.slice(0, 2)] != null)) {
|
80 | return this[type][lang.slice(0, 2)];
|
81 | } else if (this[type]["default"]) {
|
82 | return this[type]["default"];
|
83 | }
|
84 | };
|
85 |
|
86 | Payload.prototype.compile = function () {
|
87 | var lang, msg, type, _i, _len, _ref, _ref1;
|
88 | _ref = ['title', 'msg'];
|
89 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
90 | type = _ref[_i];
|
91 | _ref1 = this[type];
|
92 | for (lang in _ref1) {
|
93 | if (!__hasProp.call(_ref1, lang)) continue;
|
94 | msg = _ref1[lang];
|
95 | this[type][lang] = this.compileTemplate(msg);
|
96 | }
|
97 | }
|
98 | return this.compiled = true;
|
99 | };
|
100 |
|
101 | Payload.prototype.compileTemplate = function (tmpl) {
|
102 | var _this = this;
|
103 | return tmpl.replace(/\$\{(.*?)\}/g, function (match, keyPath) {
|
104 | return _this.variable(keyPath);
|
105 | });
|
106 | };
|
107 |
|
108 | Payload.prototype.variable = function (keyPath) {
|
109 | var key, prefix, _ref, _ref1, _ref2;
|
110 | if (keyPath === 'event.name') {
|
111 | if ((_ref = this.event) != null ? _ref.name : undefined) {
|
112 | return (_ref1 = this.event) != null ? _ref1.name : undefined;
|
113 | } else {
|
114 | throw new Error("The ${" + keyPath + "} does not exist");
|
115 | }
|
116 | }
|
117 | _ref2 = keyPath.split('.', 2), prefix = _ref2[0], key = _ref2[1];
|
118 | if (prefix !== 'var' && prefix !== 'data') {
|
119 | throw new Error("Invalid variable type for ${" + keyPath + "}");
|
120 | }
|
121 | if (this[prefix][key] == null) {
|
122 | throw new Error("The ${" + keyPath + "} does not exist");
|
123 | }
|
124 | return this[prefix][key];
|
125 | };
|
126 |
|
127 |
|
128 | exports.Payload = Payload;
|