ymdp 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -0,0 +1,130 @@
1
+
2
+ // DO NOT USE the @view instance variable in any files in /app/javascripts/base.
3
+ // The way they are cached makes it not safe to do so.
4
+
5
+ var ABTesting;
6
+
7
+ ABTesting = {
8
+ on: true,
9
+ languages: <%= english_languages.to_json %>,
10
+
11
+ enable: function() {
12
+ ABTesting.on = true;
13
+ },
14
+
15
+ disable: function() {
16
+ ABTesting.on = false;
17
+ },
18
+
19
+ randomAB: function() {
20
+ return Math.floor(Math.random()*2) ? "a" : "b";
21
+ },
22
+
23
+ get: function(content_id) {
24
+ var url, host;
25
+
26
+ url = "ymdp/experiment";
27
+
28
+ OIB.get(url, {
29
+ "domain": View.domain
30
+ }, function(response) {
31
+ ABTesting.success(content_id, response);
32
+ }, function(response) {
33
+ ABTesting.error(response);
34
+ });
35
+ },
36
+
37
+ post: function(params) {
38
+ params = params || {};
39
+ OIB.post("ymdp/view", params, function(response) {
40
+ Debug.log("ABTesting.post success", response);
41
+ }, function(response) {
42
+ Debug.error("ABTesting.post error", response);
43
+ });
44
+ },
45
+
46
+ postView: function(experimentId) {
47
+ var params;
48
+
49
+ params = {
50
+ "var": ABTesting.variable
51
+ };
52
+ if (experimentId) {
53
+ params["experiment_id"] = experimentId;
54
+ }
55
+ Debug.log("ABTesting.postView: ", params);
56
+ ABTesting.post(params);
57
+ },
58
+
59
+ setVariable: function(value) {
60
+ ABTesting.variable = value;
61
+ },
62
+
63
+ apply: function(content_id, language) {
64
+ try {
65
+ Debug.log("ABTesting.apply", $A(arguments));
66
+ if (ABTesting.on && ABTesting.languages.include(language)) {
67
+ var index;
68
+
69
+ index = ABTesting.randomAB();
70
+ ABTesting.setVariable(index);
71
+
72
+ ABTesting.get(content_id);
73
+ } else {
74
+ YAHOO.init.showAndFinish();
75
+ }
76
+ }
77
+ catch(e) {
78
+ Debug.log(e);
79
+ YAHOO.init.showAndFinish();
80
+ }
81
+ },
82
+
83
+ error: function(data) {
84
+ Debug.log("applyError", data);
85
+ if (data.error !== 3002) {
86
+ YAHOO.logger.error("Received body contents fetch error on page " + View.name + ": " + data.error + ' - ' + data.errorMsg);
87
+ }
88
+ YAHOO.init.showAndFinish();
89
+ },
90
+
91
+ success: function(content_id, response) {
92
+ try {
93
+ var content, experiment, experimentId;
94
+ Debug.log("ABTesting.success", response);
95
+
96
+ experiment = response.experiment;
97
+
98
+ if (experiment) {
99
+ content = ABTesting.content(experiment);
100
+ experimentId = response.experiment.id;
101
+
102
+ ABTesting.postView(experimentId);
103
+ ABTesting.replaceContents(content_id, content);
104
+ } else {
105
+ Debug.log("No experiment running");
106
+ }
107
+
108
+ YAHOO.init.showAndFinish();
109
+ } catch(e) {
110
+ Debug.log("ABTesting.success error" + e);
111
+ }
112
+ },
113
+
114
+ replaceContents: function(content_id, content) {
115
+ openmail.Application.filterHTML({html: content}, function(response) {
116
+ if (response.html && response.html !== '') {
117
+ try {
118
+ $(content_id).update(response.html);
119
+ } catch(omg) {
120
+ YAHOO.logger.error(omg);
121
+ }
122
+ }
123
+ });
124
+ },
125
+
126
+ content: function(experiment) {
127
+ return experiment["content_" + ABTesting.variable];
128
+ }
129
+ };
130
+
@@ -0,0 +1,165 @@
1
+ /*
2
+ CALL OIB
3
+
4
+ global to every view. sends Ajax call to OtherInbox.
5
+ */
6
+
7
+ // send an Ajax call to OtherInbox.
8
+ // takes as parameters:
9
+ // - the path to call
10
+ // - any additional query string params
11
+ // - a callback function to run when the Ajax call is a success
12
+ // - an optional error function
13
+ // - a base URL to use, if you don't want this call going to YAHOO.constants.base_url
14
+ // TODO refactor this function to take a second params hash where we could stick success_function, error_function, and base_url
15
+
16
+ OIB = {
17
+ get: function(oib_path, params, success_function, error_function, base_url) {
18
+ params.method = "GET";
19
+ OIB.call(oib_path, params, success_function, error_function, base_url);
20
+ },
21
+
22
+ post: function(oib_path, params, success_function, error_function, base_url) {
23
+ params.method = "POST";
24
+ OIB.call(oib_path, params, success_function, error_function, base_url);
25
+ },
26
+
27
+ call: function(oib_path, params, success_function, error_function, base_url) {
28
+ var success;
29
+
30
+ success = function(response) {
31
+ response = YAHOO.lang.JSON.parse(response.data);
32
+ if (response.error) {
33
+ if (error_function) {
34
+ error_function(response);
35
+ } else {
36
+ Debug.error("No error_function", response);
37
+ YAHOO.oib.showError(
38
+ {
39
+ "method": "OIB.call",
40
+ "description": "error callback"
41
+ }
42
+ ); // OIB.call response: error
43
+ }
44
+ } else {
45
+ if (success_function) {
46
+ success_function(response);
47
+ } else {
48
+ Debug.error("no success function", response);
49
+ YAHOO.oib.showError(
50
+ {
51
+ "method": "OIB.call",
52
+ "description": "success callback error"
53
+ }
54
+ ); // OIB.call response: success error
55
+ }
56
+ }
57
+ };
58
+ OIB.request(oib_path, params, success, error_function, base_url);
59
+ },
60
+
61
+ ajax_response: false,
62
+
63
+ ajax: function(url, method, params, success_function, error_function) {
64
+ params = params || {};
65
+ params["application"] = View.application;
66
+
67
+ Debug.log("OIB.ajax: About to call openmail.Application.callWebService: ", {
68
+ "method": method,
69
+ "url": url + "?" + Object.toQueryString(params)
70
+ });
71
+
72
+ openmail.Application.callWebService(
73
+ {
74
+ url: url,
75
+ method: method,
76
+ parameters: params
77
+ },
78
+ function(response) {
79
+ // response from Ajax call was a 200 response
80
+ //
81
+ Debug.log("inside response from openMail.Application.callWebService", response);
82
+ if (response.error) {
83
+ // response has a parameter called "error"
84
+ //
85
+ if (error_function) {
86
+ error_function(response);
87
+ } else {
88
+ OIB.error(url, params, response);
89
+ }
90
+ } else {
91
+ // SUCCESSFUL RESPONSE
92
+ //
93
+ // response doesn't have a parameter called "error"
94
+ //
95
+ Debug.log("success response inside openMail.Application.callWebService", response);
96
+ try {
97
+ success_function(response);
98
+ } catch(e) {
99
+ Debug.log("Error in OIB.request success function", e);
100
+ YAHOO.oib.showError({
101
+ "method": "OIB.request",
102
+ "description": "exception caught in OIB.request success callback",
103
+ "error": e
104
+ });
105
+ }
106
+ }
107
+ });
108
+ },
109
+
110
+ request: function(oib_path, params, success_function, error_function, base_url) {
111
+ Debug.log("inside OIB.request: ", {
112
+ "oib_path": oib_path,
113
+ "params": Object.toJSON(params)
114
+ });
115
+ var oib_url, method;
116
+
117
+ oib_url = base_url ? base_url : YAHOO.constants.base_url;
118
+
119
+ if (!(oib_path && typeof(oib_path) === "string")) {
120
+ throw("OIB.request must define oib_path");
121
+ }
122
+ if (!(params && typeof(params) === "object")) {
123
+ throw("OIB.request must define params");
124
+ }
125
+
126
+ oib_url = oib_url + oib_path;
127
+ method = "GET";
128
+ if (!params.format) {
129
+ params.format = 'json';
130
+ }
131
+ if (params.method) {
132
+ method = params.method;
133
+ delete params.method;
134
+ }
135
+ params.version = params.version || <%= @version %>;
136
+
137
+ Debug.log("about to call OIB.ajax");
138
+
139
+ OIB.ajax(oib_url, method, params, success_function, error_function);
140
+ },
141
+
142
+ // overwrite this function locally if you need to
143
+ error: function(url, params, response) {
144
+ var message;
145
+
146
+ message = "OIB.error: " + Object.toJSON(response) + " calling url: " + url + "?" + Object.toQueryString(params);
147
+ Debug.error(message);
148
+ },
149
+
150
+ // advance the user to the next state in the signup process
151
+ //
152
+ advance: function(success_function, error_function) {
153
+ OIB.post("ymdp/state", {}, function(response) {
154
+ Debug.log("Scanning.next success", response);
155
+ if (success_function) {
156
+ success_function(response);
157
+ }
158
+ }, function(response) {
159
+ Debug.error("Scanning.next error", response);
160
+ if (error_function) {
161
+ error_function(response);
162
+ }
163
+ });
164
+ }
165
+ };
@@ -0,0 +1,139 @@
1
+ /*
2
+ APPLICATION
3
+
4
+ // DO NOT USE the @view instance variable in any files in /app/javascripts/base.
5
+ // The way they are cached makes it not safe to do so.
6
+
7
+ */
8
+
9
+ var OIB;
10
+
11
+ /*
12
+ GLOBAL CONSTANTS
13
+ */
14
+
15
+ var View = {
16
+ application: "<%= @application_name %>",
17
+ domain: "<%= @domain %>",
18
+
19
+ authorized: function(user) {
20
+ return (user[View.application + "_user"]);
21
+ }
22
+ };
23
+
24
+ YAHOO.namespace("oib");
25
+ YAHOO.namespace("constants");
26
+ YAHOO.namespace("images");
27
+ YAHOO.namespace("init");
28
+
29
+ YAHOO.oib.page_loaded = false;
30
+
31
+ function unixTimeToDate(unixtime) {
32
+ return new Date(unixtime * 1000);
33
+ }
34
+
35
+ function formatUnixDate(unixtime) {
36
+ var date;
37
+ date = unixTimeToDate(unixtime);
38
+ return date.toString("MMMM d, yyyy");
39
+ }
40
+
41
+ // Shows the error view.
42
+ //
43
+ // YAHOO.oib.showError({
44
+ // heading: "optional heading text can overwrite the error view's heading",
45
+ // message: "optional message can overwrite the view's first paragraph",
46
+ // retry: "hide"
47
+ // });
48
+ //
49
+ // Set the "retry" option to "hide" to hide the Retry button.
50
+ //
51
+
52
+ YAHOO.oib.showError = function(options) {
53
+ var params;
54
+
55
+ Debug.log("YAHOO.oib.showError", options);
56
+
57
+ options = options || {};
58
+
59
+ if (options["heading"]) {
60
+ $("error_1").update(options["heading"]);
61
+ }
62
+ if (options["message"]) {
63
+ $("error_2").update(options["message"]);
64
+ }
65
+ if (options["retry"] && options["retry"] === "hide") {
66
+ $("retry_button_container").hide();
67
+ }
68
+
69
+ params = {};
70
+ params["description"] = options["description"];
71
+ params["method_name"] = options["method"];
72
+ params["error"] = Object.toJSON(options["error"]);
73
+ params["page"] = View.name;
74
+
75
+ Reporter.error(YAHOO.oib.guid, params);
76
+ $('main').hide();
77
+ $('utility').show();
78
+ $('loading').hide();
79
+ $('error').show();
80
+ };
81
+
82
+ YAHOO.oib.showLoading = function() {
83
+ $('main').hide();
84
+ $('utility').show();
85
+ $('error').hide();
86
+ $('loading').show();
87
+ };
88
+
89
+ YAHOO.init.upgradeCheck = function(success_callback, failure_callback) {
90
+ // test for Minty
91
+ //
92
+ openmail.Application.getParameters(function(response) {
93
+ if (response.version === "2") {
94
+
95
+ // Minty-only code goes here
96
+
97
+ try {
98
+ Debug.log("Minty found");
99
+
100
+ success_callback();
101
+ } catch(wtf) {
102
+ Debug.error(wtf);
103
+ }
104
+ } else {
105
+ // non-Minty
106
+
107
+ if (failure_callback) {
108
+ failure_callback();
109
+ } else {
110
+ YAHOO.init.upgrade();
111
+ }
112
+ }
113
+ });
114
+ };
115
+
116
+ YAHOO.init.upgrade = function() {
117
+ YAHOO.oib.showTranslations();
118
+
119
+ YAHOO.oib.page_loaded = true;
120
+
121
+ $('loading').hide();
122
+ $('error').hide();
123
+ $('upgrade').show();
124
+ };
125
+
126
+ YAHOO.oib.setTimeoutInSeconds = function(callback_function, interval) {
127
+ setTimeout(callback_function, interval * 1000);
128
+ };
129
+
130
+ YAHOO.oib.showTranslations = function() {
131
+ Debug.log("begin YAHOO.oib.showTranslations");
132
+ Try.these(I18n.findAndTranslateAll);
133
+
134
+ // define I18n.localTranslations in the view template
135
+ Try.these(I18n.localTranslations);
136
+
137
+ Debug.log("end YAHOO.oib.showTranslations");
138
+ };
139
+
@@ -0,0 +1,36 @@
1
+ var Browser;
2
+
3
+ Browser = {
4
+ version: function(v) {
5
+ var version, app_version;
6
+ app_version = navigator.appVersion;
7
+ Debug.log("Browser app_version: ", app_version);
8
+
9
+ if (app_version.match("MSIE 6.0")) {
10
+ version = 6.0;
11
+ }
12
+
13
+ if (v) {
14
+ return (version === v);
15
+ } else {
16
+ return version;
17
+ }
18
+ },
19
+
20
+ ie: function() {
21
+ return navigator.appName.match("Internet Explorer");
22
+ },
23
+
24
+ ie6: function() {
25
+ return (Browser.ie() && Browser.version(6.0));
26
+ },
27
+
28
+ ie7: function() {
29
+ return (Browser.ie() && Browser.version(7.0));
30
+ },
31
+
32
+ ie8: function() {
33
+ return (Browser.ie() && Browser.version(8.0));
34
+ }
35
+ };
36
+