retreaverjs-rails 0.0.12

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 = Retreaver.Base;
4
+ /**
5
+ * @constructor
6
+ * @memberOf Retreaver
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
+ Retreaver.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 Retreaver.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 Retreaver.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 Retreaver.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 Retreaver.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 Retreaver.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 Retreaver.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 = Retreaver.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
+ Retreaver.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
+ Retreaver.Number.prototype = new Retreaver.Base.Model();
168
+
169
+ function ping_active_numbers(callback) {
170
+ if (typeof(Retreaver.Base.Data._store) !== 'undefined') {
171
+ // get numbers
172
+ var numbers = Retreaver.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
+ Retreaver.Base.Request.connection().postJSON('/api/v1/numbers/ping', payload, [Retreaver.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
+ })();
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: retreaverjs-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.12
5
+ platform: ruby
6
+ authors:
7
+ - Blake Hilscher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: retreaver.js rails wrapper
70
+ email:
71
+ - blake@hilscher.ca
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gruntfile.js
77
+ - LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - config/README
81
+ - config/jsdocs.json
82
+ - lib/retreaverjs/compile.rb
83
+ - lib/retreaverjs/rails.rb
84
+ - lib/retreaverjs/rails/version.rb
85
+ - package.json
86
+ - src/retreaver/base/base64.js
87
+ - src/retreaver/base/cookies.js
88
+ - src/retreaver/base/data.js
89
+ - src/retreaver/base/helpers.js
90
+ - src/retreaver/base/model.js
91
+ - src/retreaver/base/request.js
92
+ - src/retreaver/base/request_number.js
93
+ - src/retreaver/cache.js
94
+ - src/retreaver/callpixels.js
95
+ - src/retreaver/campaign.js
96
+ - src/retreaver/number.js
97
+ homepage: ''
98
+ licenses:
99
+ - GPLv3
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.2.2
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: retreaver.js rails wrapper
121
+ test_files: []