callpixelsjs-rails 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,206 @@
1
+ (function () {
2
+ // Dependencies
3
+ var Base = Callpixels.Base;
4
+ /**
5
+ * @constructor
6
+ * @memberOf Callpixels
7
+ * @param {Object} attributes - Attributes
8
+ * @property {Object} attributes
9
+ * @property {Number} attributes.id - The CallPixels internal number ID.
10
+ * @property {String} attributes.formatted_number - Nationally formatted phone number.
11
+ * @property {String} attributes.number - E.164 formatted phone number.
12
+ * @property {String} attributes.plain_number - The unformatted phone number digits.
13
+ * @property {Boolean} attributes.target_open - Whether there is an open, available target.
14
+ */
15
+ Callpixels.Number = function (options) {
16
+
17
+ var self = this;
18
+ self.type = 'numbers';
19
+
20
+ function initialize(data) {
21
+ self.store(data);
22
+ self.set('is_active', 'true');
23
+ }
24
+
25
+ /**
26
+ * Add tags to a number.
27
+ * @memberOf Callpixels.Number
28
+ * @function add_tags
29
+ * @instance
30
+ * @param {Object} tags - A collection of tags {key: 'value', tag2: 'value2'}
31
+ * @param {Function} callback - Callback that will be fired after request.
32
+ * @throws Will throw an error if attempting to modify tags on a number that doesn't belong to a number pool
33
+ * with per-visitor numbers enabled.
34
+ */
35
+ self.add_tags = function (tags, callback) {
36
+ ensure_is_per_visitor();
37
+ self.post_data('numbers/tag', tags_payload(tags), callback);
38
+ };
39
+
40
+ /**
41
+ * Remove tags from a number.
42
+ * @memberOf Callpixels.Number
43
+ * @function remove_tags
44
+ * @instance
45
+ * @param {Object} tags - A collection of tags {key: 'value', tag2: 'value2'}
46
+ * @param {Function} callback - Callback that will be fired after request.
47
+ * @throws Will throw an error if attempting to modify tags on a number that doesn't belong to a number pool
48
+ * with per-visitor numbers enabled.
49
+ */
50
+ self.remove_tags = function (tags, callback) {
51
+ ensure_is_per_visitor();
52
+ self.post_data('numbers/untag', tags_payload(tags), callback);
53
+ };
54
+
55
+ /**
56
+ * Removes all tags with given keys from a number.
57
+ * @memberOf Callpixels.Number
58
+ * @function remove_tags_by_keys
59
+ * @instance
60
+ * @param {Array} keys - An array of keys to remove. eg: ['key1', 'key2']
61
+ * @param {Function} callback - Callback that will be fired after request.
62
+ * @throws Will throw an error if attempting to modify tags on a number that doesn't belong to a number pool
63
+ * with per-visitor numbers enabled.
64
+ */
65
+ self.remove_tags_by_keys = function (keys, callback) {
66
+ ensure_is_per_visitor();
67
+ if (typeof(keys) === 'string') keys = keys.split(',');
68
+ var payload = {
69
+ tag_keys: keys,
70
+ ids: [ get('id') ],
71
+ campaign_key: get('campaign_key')
72
+ };
73
+ self.post_data('numbers/untag/keys', payload, callback);
74
+ };
75
+
76
+ /**
77
+ * Clear all tags from a number.
78
+ * @memberOf Callpixels.Number
79
+ * @function clear_tags
80
+ * @instance
81
+ * @param {Function} callback - Callback that will be fired after request.
82
+ * @throws Will throw an error if attempting to modify tags on a number that doesn't belong to a number pool
83
+ * with per-visitor numbers enabled.
84
+ */
85
+ self.clear_tags = function (callback) {
86
+ ensure_is_per_visitor();
87
+ var payload = {
88
+ ids: [ get('id') ],
89
+ campaign_key: get('campaign_key'),
90
+ all: 'true'
91
+ };
92
+ self.post_data('numbers/untag', payload, callback);
93
+ };
94
+
95
+ /**
96
+ * Release number back to pool.
97
+ * @memberOf Callpixels.Number
98
+ * @function release
99
+ * @instance
100
+ */
101
+ self.release = function () {
102
+ self.set('is_active', 'false');
103
+ };
104
+
105
+ /**
106
+ * Start a call immediately by having a campaign target dial the visitor.
107
+ * @memberOf Callpixels.Number
108
+ * @function initiate_call
109
+ * @instance
110
+ * @param {String} dial - The number to call.
111
+ * @param {Object} payload - A collection of tags as key-value pairs and optional secure override properties.
112
+ * @param {string} [payload.target_map] - A string mapping a placeholder number to a phone number.
113
+ * @param {string} [payload.target_map_cs] - A SHA1 checksum of the target_map concatenated with your CallPixels API
114
+ * key.
115
+ * @param {number} [payload.timer_offset] - Number of seconds to offset the "connect" duration timers by.
116
+ * @param {string} [payload.timer_offset_cs] - An SHA1 checksum of the timer_offset concatenated with your
117
+ * CallPixels API key.
118
+ * @param {(string|number)} [payload.*] - Key value pairs treated as tags.
119
+ * @param {Function} callback - Callback that will be fired after request.
120
+ * @example
121
+ * number.initiate_call('4166686980', {company_name: 'CallPixels'}, function (call) {
122
+ * alert('Call started with UUID ' + call.uuid)
123
+ * });
124
+ */
125
+ self.initiate_call = function (dial, payload, callback) {
126
+ if (typeof(payload) === 'undefined') payload = {};
127
+ // assign dial to payload
128
+ payload.dial = dial;
129
+ // merge payload into payload
130
+ payload = Base.merge(self.get('id', 'campaign_key'), payload);
131
+ // post the payload
132
+ self.post_data('numbers/initiate_call', payload, callback);
133
+ };
134
+
135
+ function tags_payload(tags) {
136
+ if (typeof(tags) === 'string') tags = Callpixels.Number.extract_tags_from_string(tags);
137
+ return {
138
+ tag_values: tags,
139
+ ids: [ get('id') ],
140
+ campaign_key: get('campaign_key')
141
+ };
142
+ }
143
+
144
+ function get(key) {
145
+ return self.get(key);
146
+ }
147
+
148
+ function ensure_is_per_visitor() {
149
+ if (self.get('is_per_visitor') === false) {
150
+ throw "Error: Tried to add tags to non per-visitor number.";
151
+ }
152
+ }
153
+
154
+ initialize(options);
155
+ };
156
+
157
+ Callpixels.Number.extract_tags_from_string = function (tags) {
158
+ var output = {};
159
+ var tags = tags.split(",");
160
+ for (var i = 0; i < tags.length; i++) {
161
+ var tag = tags[i].split(":");
162
+ output[tag[0]] = tag[1]
163
+ }
164
+ return output;
165
+ };
166
+
167
+ Callpixels.Number.prototype = new Callpixels.Base.Model();
168
+
169
+ function ping_active_numbers(callback) {
170
+ if (typeof(Callpixels.Base.Data._store) !== 'undefined') {
171
+ // get numbers
172
+ var numbers = Callpixels.Base.Data._store['numbers'];
173
+ // for each number
174
+ if (typeof(numbers) !== 'undefined') {
175
+ // group number_ids by campaign_key
176
+ var grouped = {};
177
+ for (var primary_key in numbers) {
178
+ var number = numbers[primary_key];
179
+ if (number.is_active === 'true') {
180
+ if (typeof(grouped[number.campaign_key]) === 'undefined'){
181
+ grouped[number.campaign_key] = [];
182
+ grouped[number.campaign_key]['ids'] = [];
183
+ grouped[number.campaign_key]['hashes'] = [];
184
+ }
185
+ grouped[number.campaign_key]['ids'].push(number.id);
186
+ grouped[number.campaign_key]['hashes'].push(number.id_checksum);
187
+ }
188
+ }
189
+ // ping each group of number_ids
190
+ for (var campaign_key in grouped) {
191
+ var payload = {
192
+ ids: grouped[campaign_key].ids,
193
+ hashes: grouped[campaign_key].hashes
194
+ };
195
+ Callpixels.Base.Request.connection().postJSON('/api/v1/numbers/ping', payload, [Callpixels.Base.Model.update, callback], this);
196
+ }
197
+ }
198
+ }
199
+ // call recursively
200
+ setTimeout(ping_active_numbers, 15000);
201
+ }
202
+
203
+ // always ping active numbers
204
+ ping_active_numbers();
205
+
206
+ })();
@@ -3,7 +3,7 @@
3
3
  <head>
4
4
  <meta charset="utf-8">
5
5
  <title>JSDoc: Class: Campaign</title>
6
-
6
+
7
7
  <script src="scripts/prettify/prettify.js"> </script>
8
8
  <script src="scripts/prettify/lang-css.js"> </script>
9
9
  <!--[if lt IE 9]>
@@ -16,70 +16,70 @@
16
16
  <body>
17
17
 
18
18
  <div id="main">
19
-
19
+
20
20
  <h1 class="page-title">Class: Campaign</h1>
21
-
21
+
22
22
 
23
23
 
24
24
 
25
25
 
26
26
  <section>
27
-
27
+
28
28
  <header>
29
29
  <h2>
30
30
  Campaign
31
31
  </h2>
32
32
 
33
- </header>
33
+ </header>
34
34
 
35
35
  <article>
36
36
  <div class="container-overview">
37
37
 
38
-
38
+
39
39
 
40
40
 
41
41
  <dt>
42
42
  <h4 class="name" id="Campaign"><span class="type-signature"></span>new Campaign<span class="signature">(options)</span><span class="type-signature"></span></h4>
43
-
43
+
44
44
 
45
45
  </dt>
46
46
  <dd>
47
-
48
47
 
49
-
50
48
 
51
49
 
52
50
 
53
-
51
+
52
+
53
+
54
54
 
55
55
  <h5>Parameters:</h5>
56
56
 
57
57
 
58
58
  <table class="params">
59
59
  <thead>
60
- <tr>
61
-
62
- <th>Name</th>
63
-
64
-
65
- <th>Type</th>
66
-
67
-
68
-
69
-
70
-
71
- <th class="last">Description</th>
72
- </tr>
73
- </thead>
74
-
75
- <tbody>
76
-
77
-
60
+ <tr>
61
+
62
+ <th>Name</th>
63
+
64
+
65
+ <th>Type</th>
66
+
67
+
68
+
69
+
70
+
71
+ <th class="last">Description</th>
72
+ </tr>
73
+ </thead>
74
+
75
+ <tbody>
76
+
77
+
78
78
  <tr>
79
79
 
80
80
  <td class="name"><code>options</code></td>
81
81
 
82
-
82
+
83
83
  <td class="type">
84
84
 
85
85
 
@@ -88,40 +88,40 @@
88
88
 
89
89
 
90
90
  </td>
91
-
92
91
 
93
-
94
92
 
95
-
93
+
94
+
95
+
96
96
  <td class="description last">
97
97
  <h6>Properties</h6>
98
98
 
99
99
 
100
100
  <table class="params">
101
101
  <thead>
102
- <tr>
103
-
104
- <th>Name</th>
105
-
106
-
107
- <th>Type</th>
108
-
109
-
110
-
111
-
112
-
113
- <th class="last">Description</th>
114
- </tr>
115
- </thead>
116
-
117
- <tbody>
118
-
119
-
102
+ <tr>
103
+
104
+ <th>Name</th>
105
+
106
+
107
+ <th>Type</th>
108
+
109
+
110
+
111
+
112
+
113
+ <th class="last">Description</th>
114
+ </tr>
115
+ </thead>
116
+
117
+ <tbody>
118
+
119
+
120
120
  <tr>
121
121
 
122
122
  <td class="name"><code>campaign_key</code></td>
123
123
 
124
-
124
+
125
125
  <td class="type">
126
126
 
127
127
 
@@ -130,72 +130,70 @@
130
130
 
131
131
 
132
132
  </td>
133
-
134
133
 
135
-
136
134
 
137
-
135
+
136
+
137
+
138
138
  <td class="description last">Campaign key</td>
139
139
  </tr>
140
-
141
-
142
- </tbody>
140
+
141
+
142
+ </tbody>
143
143
  </table>
144
144
  </td>
145
145
  </tr>
146
-
147
-
148
- </tbody>
146
+
147
+
148
+ </tbody>
149
149
  </table>
150
150
 
151
-
151
+
152
152
 
153
153
  <dl class="details">
154
154
 
155
-
155
+
156
156
 
157
-
158
157
 
159
-
160
158
 
161
159
 
162
160
 
163
-
164
161
 
165
-
166
162
 
167
-
168
163
 
169
-
170
164
 
171
-
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
172
 
173
173
  <dt class="tag-source">Source:</dt>
174
174
  <dd class="tag-source"><ul class="dummy"><li>
175
175
  <a href="campaign.js.html">campaign.js</a>, <a href="campaign.js.html#line12">line 12</a>
176
176
  </li></ul></dd>
177
177
 
178
-
179
178
 
180
-
181
179
 
182
-
180
+
181
+
182
+
183
183
 
184
184
  </dl>
185
185
 
186
-
187
186
 
188
-
189
187
 
190
188
 
191
189
 
192
190
 
193
191
 
194
-
195
192
 
196
-
197
193
 
198
-
194
+
195
+
196
+
199
197
 
200
198
  <h5>Example</h5>
201
199
 
@@ -206,33 +204,31 @@
206
204
 
207
205
 
208
206
  </div>
209
-
210
207
 
211
-
212
208
 
213
-
214
209
 
215
-
216
210
 
217
-
218
-
219
-
220
211
 
221
-
222
212
 
223
-
213
+
214
+
215
+
216
+
217
+
218
+
219
+
224
220
 
225
221
  <h3 class="subsection-title">Methods</h3>
226
-
222
+
227
223
  <dl>
228
224
 
229
225
  <dt>
230
226
  <h4 class="name" id="request_number"><span class="type-signature"></span>request_number<span class="signature">(tags, callback, error_callback)</span><span class="type-signature"></span></h4>
231
-
227
+
232
228
 
233
229
  </dt>
234
230
  <dd>
235
-
231
+
236
232
 
237
233
  <div class="description">
238
234
  Fetch a campaign number.
@@ -240,38 +236,38 @@
240
236
 
241
237
 
242
238
 
243
-
244
239
 
245
-
240
+
241
+
246
242
 
247
243
  <h5>Parameters:</h5>
248
244
 
249
245
 
250
246
  <table class="params">
251
247
  <thead>
252
- <tr>
253
-
254
- <th>Name</th>
255
-
256
-
257
- <th>Type</th>
258
-
259
-
260
-
261
-
262
-
263
- <th class="last">Description</th>
264
- </tr>
265
- </thead>
266
-
267
- <tbody>
268
-
269
-
248
+ <tr>
249
+
250
+ <th>Name</th>
251
+
252
+
253
+ <th>Type</th>
254
+
255
+
256
+
257
+
258
+
259
+ <th class="last">Description</th>
260
+ </tr>
261
+ </thead>
262
+
263
+ <tbody>
264
+
265
+
270
266
  <tr>
271
267
 
272
268
  <td class="name"><code>tags</code></td>
273
269
 
274
-
270
+
275
271
  <td class="type">
276
272
 
277
273
 
@@ -280,21 +276,21 @@
280
276
 
281
277
 
282
278
  </td>
283
-
284
279
 
285
-
286
280
 
287
-
281
+
282
+
283
+
288
284
  <td class="description last">A collection of tags as key-value pairs. The number returned will match these tags.</td>
289
285
  </tr>
290
-
291
-
292
-
286
+
287
+
288
+
293
289
  <tr>
294
290
 
295
291
  <td class="name"><code>callback</code></td>
296
292
 
297
-
293
+
298
294
  <td class="type">
299
295
 
300
296
 
@@ -303,21 +299,21 @@
303
299
 
304
300
 
305
301
  </td>
306
-
307
302
 
308
-
309
303
 
310
-
304
+
305
+
306
+
311
307
  <td class="description last">Callback fired if the request completes successfully.</td>
312
308
  </tr>
313
-
314
-
315
-
309
+
310
+
311
+
316
312
  <tr>
317
313
 
318
314
  <td class="name"><code>error_callback</code></td>
319
315
 
320
-
316
+
321
317
  <td class="type">
322
318
 
323
319
 
@@ -326,66 +322,64 @@
326
322
 
327
323
 
328
324
  </td>
329
-
330
325
 
331
-
332
326
 
333
-
327
+
328
+
329
+
334
330
  <td class="description last">Callback fired if the request raises an error.</td>
335
331
  </tr>
336
-
337
-
338
- </tbody>
332
+
333
+
334
+ </tbody>
339
335
  </table>
340
336
 
341
-
337
+
342
338
 
343
339
  <dl class="details">
344
340
 
345
-
341
+
346
342
 
347
-
348
343
 
349
-
350
344
 
351
345
 
352
346
 
353
-
354
347
 
355
-
356
348
 
357
-
358
349
 
359
-
360
350
 
361
-
351
+
352
+
353
+
354
+
355
+
356
+
357
+
362
358
 
363
359
  <dt class="tag-source">Source:</dt>
364
360
  <dd class="tag-source"><ul class="dummy"><li>
365
361
  <a href="campaign.js.html">campaign.js</a>, <a href="campaign.js.html#line24">line 24</a>
366
362
  </li></ul></dd>
367
363
 
368
-
369
364
 
370
-
371
365
 
372
-
366
+
367
+
368
+
373
369
 
374
370
  </dl>
375
371
 
376
-
377
372
 
378
-
379
373
 
380
374
 
381
375
 
382
376
 
383
377
 
384
-
385
378
 
386
-
387
379
 
388
-
380
+
381
+
382
+
389
383
 
390
384
  <h5>Example</h5>
391
385
 
@@ -400,13 +394,13 @@
400
394
 
401
395
  </dl>
402
396
 
403
-
404
397
 
405
-
398
+
399
+
406
400
 
407
401
  </article>
408
402
 
409
- </section>
403
+ </section>
410
404
 
411
405
 
412
406
 
@@ -420,7 +414,7 @@
420
414
  <br clear="both">
421
415
 
422
416
  <footer>
423
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha9</a> on Thu Jan 29 2015 13:09:13 GMT-0500 (EST)
417
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Thu Jan 29 2015 13:49:34 GMT-0500 (EST)
424
418
  </footer>
425
419
 
426
420
  <script> prettyPrint(); </script>