Skip to content

Commit 87a6f76

Browse files
committed
added new unsubscribe plugin
1 parent feda8ad commit 87a6f76

File tree

4 files changed

+281
-2
lines changed

4 files changed

+281
-2
lines changed

dist/breinify-unsubscribe.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"use strict";
2+
3+
(function () {
4+
if (typeof Breinify !== 'object') {
5+
return;
6+
}
7+
// make sure the plugin isn't loaded yet
8+
else if (Breinify.plugins._isAdded('unsubscribe')) {
9+
return;
10+
}
11+
12+
// bind the jQuery default object $
13+
var $ = Breinify.UTL._jquery();
14+
var overload = Breinify.plugins._overload();
15+
16+
var _private = {
17+
tokenCodeValidation: null,
18+
tokenCodeUnsubscribe: null,
19+
20+
getValidationToken: function () {
21+
if (this.tokenCodeValidation === null) {
22+
this.tokenCodeValidation = this._determineValidationToken();
23+
}
24+
25+
return this.tokenCodeValidation;
26+
},
27+
28+
getUnsubscribeToken: function () {
29+
if (this.tokenCodeUnsubscribe === null) {
30+
this.tokenCodeUnsubscribe = this._determineUnsubscribeToken();
31+
}
32+
33+
return this.tokenCodeUnsubscribe;
34+
},
35+
36+
getCode: function (code) {
37+
if (code === null || typeof code !== 'string') {
38+
code = Breinify.UTL.loc.param('c');
39+
}
40+
41+
// make sure that after we read a code, we check the validity
42+
if (code === null || typeof code !== 'string') {
43+
return null;
44+
} else {
45+
return code;
46+
}
47+
},
48+
49+
_determineValidationToken: function () {
50+
return this._determineToken('tokenCodeValidation', 'testTokenCodeValidation');
51+
},
52+
53+
_determineUnsubscribeToken: function () {
54+
return this._determineToken('tokenCodeUnsubscribe', 'testTokenCodeUnsubscribe');
55+
},
56+
57+
_determineToken: function (cfgName, cfgTestName) {
58+
var token = null;
59+
if (Breinify.plugins._isAdded('api') && Breinify.plugins.api.determineMode() !== 'prod') {
60+
token = this.getConfig(cfgTestName, null);
61+
}
62+
63+
if (token === null) {
64+
token = this.getConfig(cfgName, null);
65+
}
66+
67+
return token;
68+
}
69+
};
70+
71+
var Unsubscribe = {
72+
73+
validate: function (callback, code) {
74+
var token = _private.getValidationToken();
75+
code = _private.getCode(code);
76+
77+
$.ajax({
78+
'url': 'https://api.breinify.com/res/' + token,
79+
'type': 'GET',
80+
'crossDomain': true,
81+
'data': {
82+
'code': code
83+
},
84+
'success': function (data) {
85+
86+
if ($.isPlainObject(data) && $.isPlainObject(data.payload) &&
87+
typeof data.payload.email === 'string' && data.payload.email.trim() !== '') {
88+
callback(null, data.payload);
89+
} else {
90+
callback(new Error('failed to validate the specified code'));
91+
}
92+
},
93+
'error': function (jqXHR, text, exception) {
94+
callback(new Error('failed to validate the specified code: ' + text));
95+
},
96+
'timeout': 60000
97+
});
98+
},
99+
100+
unsubscribe: function (status, callback, code) {
101+
var token = _private.getUnsubscribeToken();
102+
code = _private.getCode(code);
103+
104+
if (typeof token !== 'string') {
105+
callback(new Error('unsubscribe token not configured'));
106+
return;
107+
} else if (typeof code !== 'string') {
108+
callback(new Error('unsubscribe code not specified or found'));
109+
return;
110+
}
111+
112+
$.ajax({
113+
'url': 'https://api.breinify.com/res/' + token,
114+
'type': 'GET',
115+
'crossDomain': true,
116+
'data': {
117+
'code': code,
118+
'status': status
119+
},
120+
'success': function (data) {
121+
if ($.isPlainObject(data) && $.isPlainObject(data.payload) &&
122+
typeof data.payload.email === 'string' && data.payload.email.trim() !== '') {
123+
callback(null, data.payload);
124+
} else {
125+
callback(new Error('failed to unsubscribe'));
126+
}
127+
},
128+
'error': function (jqXHR, text, exception) {
129+
callback(new Error('failed to unsubscribe: ' + text));
130+
},
131+
'timeout': 60000
132+
});
133+
}
134+
};
135+
136+
// bind the module
137+
Breinify.plugins._add('unsubscribe', Unsubscribe);
138+
})();

dist/breinify-unsubscribe.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gruntfile.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ module.exports = function (grunt) {
298298
'dist/breinify-ui-popup.min.js': 'src/plugins/UiPopup.js',
299299
'dist/breinify-ui-validator.min.js': 'src/plugins/UiValidator.js',
300300
'dist/breinify-opt-status.min.js': 'src/plugins/OptStatus.js',
301-
'dist/breinify-youtube.min.js': 'src/plugins/YouTube.js'
301+
'dist/breinify-youtube.min.js': 'src/plugins/YouTube.js',
302+
'dist/breinify-unsubscribe.min.js': 'src/plugins/Unsubscribe.js'
302303
}
303304
}
304305
},
@@ -330,7 +331,8 @@ module.exports = function (grunt) {
330331
{expand: true, cwd: 'src/plugins', src: 'UiPopup.js', dest: 'dist', rename: function(dest) { return dest + '/breinify-ui-popup.js' } },
331332
{expand: true, cwd: 'src/plugins', src: 'UiValidator.js', dest: 'dist', rename: function(dest) { return dest + '/breinify-ui-validator.js' } },
332333
{expand: true, cwd: 'src/plugins', src: 'OptStatus.js', dest: 'dist', rename: function(dest) { return dest + '/breinify-opt-status.js' } },
333-
{expand: true, cwd: 'src/plugins', src: 'YouTube.js', dest: 'dist', rename: function(dest) { return dest + '/breinify-youtube.js' } }
334+
{expand: true, cwd: 'src/plugins', src: 'YouTube.js', dest: 'dist', rename: function(dest) { return dest + '/breinify-youtube.js' } },
335+
{expand: true, cwd: 'src/plugins', src: 'Unsubscribe.js', dest: 'dist', rename: function(dest) { return dest + '/breinify-unsubscribe.js' } }
334336
]
335337
}
336338
},

src/plugins/Unsubscribe.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"use strict";
2+
3+
(function () {
4+
if (typeof Breinify !== 'object') {
5+
return;
6+
}
7+
// make sure the plugin isn't loaded yet
8+
else if (Breinify.plugins._isAdded('unsubscribe')) {
9+
return;
10+
}
11+
12+
// bind the jQuery default object $
13+
var $ = Breinify.UTL._jquery();
14+
var overload = Breinify.plugins._overload();
15+
16+
var _private = {
17+
tokenCodeValidation: null,
18+
tokenCodeUnsubscribe: null,
19+
20+
getValidationToken: function () {
21+
if (this.tokenCodeValidation === null) {
22+
this.tokenCodeValidation = this._determineValidationToken();
23+
}
24+
25+
return this.tokenCodeValidation;
26+
},
27+
28+
getUnsubscribeToken: function () {
29+
if (this.tokenCodeUnsubscribe === null) {
30+
this.tokenCodeUnsubscribe = this._determineUnsubscribeToken();
31+
}
32+
33+
return this.tokenCodeUnsubscribe;
34+
},
35+
36+
getCode: function (code) {
37+
if (code === null || typeof code !== 'string') {
38+
code = Breinify.UTL.loc.param('c');
39+
}
40+
41+
// make sure that after we read a code, we check the validity
42+
if (code === null || typeof code !== 'string') {
43+
return null;
44+
} else {
45+
return code;
46+
}
47+
},
48+
49+
_determineValidationToken: function () {
50+
return this._determineToken('tokenCodeValidation', 'testTokenCodeValidation');
51+
},
52+
53+
_determineUnsubscribeToken: function () {
54+
return this._determineToken('tokenCodeUnsubscribe', 'testTokenCodeUnsubscribe');
55+
},
56+
57+
_determineToken: function (cfgName, cfgTestName) {
58+
var token = null;
59+
if (Breinify.plugins._isAdded('api') && Breinify.plugins.api.determineMode() !== 'prod') {
60+
token = this.getConfig(cfgTestName, null);
61+
}
62+
63+
if (token === null) {
64+
token = this.getConfig(cfgName, null);
65+
}
66+
67+
return token;
68+
}
69+
};
70+
71+
var Unsubscribe = {
72+
73+
validate: function (callback, code) {
74+
var token = _private.getValidationToken();
75+
code = _private.getCode(code);
76+
77+
$.ajax({
78+
'url': 'https://api.breinify.com/res/' + token,
79+
'type': 'GET',
80+
'crossDomain': true,
81+
'data': {
82+
'code': code
83+
},
84+
'success': function (data) {
85+
86+
if ($.isPlainObject(data) && $.isPlainObject(data.payload) &&
87+
typeof data.payload.email === 'string' && data.payload.email.trim() !== '') {
88+
callback(null, data.payload);
89+
} else {
90+
callback(new Error('failed to validate the specified code'));
91+
}
92+
},
93+
'error': function (jqXHR, text, exception) {
94+
callback(new Error('failed to validate the specified code: ' + text));
95+
},
96+
'timeout': 60000
97+
});
98+
},
99+
100+
unsubscribe: function (status, callback, code) {
101+
var token = _private.getUnsubscribeToken();
102+
code = _private.getCode(code);
103+
104+
if (typeof token !== 'string') {
105+
callback(new Error('unsubscribe token not configured'));
106+
return;
107+
} else if (typeof code !== 'string') {
108+
callback(new Error('unsubscribe code not specified or found'));
109+
return;
110+
}
111+
112+
$.ajax({
113+
'url': 'https://api.breinify.com/res/' + token,
114+
'type': 'GET',
115+
'crossDomain': true,
116+
'data': {
117+
'code': code,
118+
'status': status
119+
},
120+
'success': function (data) {
121+
if ($.isPlainObject(data) && $.isPlainObject(data.payload) &&
122+
typeof data.payload.email === 'string' && data.payload.email.trim() !== '') {
123+
callback(null, data.payload);
124+
} else {
125+
callback(new Error('failed to unsubscribe'));
126+
}
127+
},
128+
'error': function (jqXHR, text, exception) {
129+
callback(new Error('failed to unsubscribe: ' + text));
130+
},
131+
'timeout': 60000
132+
});
133+
}
134+
};
135+
136+
// bind the module
137+
Breinify.plugins._add('unsubscribe', Unsubscribe);
138+
})();

0 commit comments

Comments
 (0)