-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.lymbix.js
136 lines (113 loc) · 4.61 KB
/
jquery.lymbix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(function($) {
/**
* @param {string} authenticationKey your Lymbix authentication key
*/
$.lymbix = function (authenticationKey) {
var API_BASE = 'https://gyrus.lymbix.com/';
var TONALIZE_MULTIPLE = 'tonalize_multiple';
var TONALIZE_DETAILED = 'tonalize_detailed';
var TONALIZE = 'tonalize';
var FLAG_RESPONSE = 'flag_response';
if (!authenticationKey) {
$.error('You must include your authentication key');
}
/* utility functions */
var getHeaders = function () {
var headers = {
authentication: authenticationKey,
accept: 'application/json',
version: '2.2'
};
return headers;
}
var jsonConcat = function (o1, o2) {
for (var key in o2) {
o1[key] = o2[key];
}
return o1;
}
/* api methods */
return {
/**
* tonalize multiple articles
*
* @param {array} articles articles to tonalize
* @param {function} callback the function to call when the method returns
* @param {array} options additional parameters (reference_ids and return_fields)
* @return {object} see the api documentation for the format of this object
*/
tonalizeMultiple: function (articles, callback, options) {
if (!articles) {
$.error('You must include articles to tonalize');
}
var url = API_BASE + TONALIZE_MULTIPLE + '?callback=?';
articles = JSON.stringify(articles);
var data = {'articles': articles};
data = jsonConcat(data, options);
data = jsonConcat(data, getHeaders());
$.getJSON(url, data, callback);
},
/**
* tonalize an article
*
* @param {string} article article to tonalize
* @param {function} callback the function to call when the method returns
* @param {array} options additional parameters (reference_id and return_fields)
* @return {object} see the api documentation for the format of this object
*/
tonalizeDetailed: function (article, callback, options) {
if (!article) {
$.error('You must include an article to tonalize');
}
var url = API_BASE + TONALIZE_DETAILED + '?callback=?';
var data = {'article': article};
data = jsonConcat(data, options);
data = jsonConcat(data, getHeaders());
$.getJSON(url, data, callback);
},
/**
* tonalize an article
*
* @param {string} article article to tonalize
* @param {function} callback the function to call when the method returns
* @param {array} options additional parameters (reference_id and return_fields)
* @return {object} see the api documentation for the format of this object
*/
tonalize: function (article, callback, options) {
if (!article) {
$.error('You must include an article to tonalize');
}
var url = API_BASE + TONALIZE + '?callback=?';
var data = {'article': article};
data = jsonConcat(data, options);
data = jsonConcat(data, getHeaders());
$.getJSON(url, data, callback);
},
/**
* flag a response as inaccurate
*
* @param {string} phrase the phrase that returns an inaccurate response
* @param {function} callback the function to call when the method returns
* @param {string} apiMethod the method that returns an inaccurate response
* @param {string} apiVersion the version that returns an inaccurate response
* @param {string} callbackUrl a url to call when the phrase has been re-rated
* @param {array} options additional parameters (reference_id)
* @return {string} see the api documentation for this method's response
*/
flagResponse: function (phrase, callback, apiMethod, apiVersion, callbackUrl, options) {
if (!phrase) {
$.error('You must include a phrase to flag');
}
if (!apiVersion) apiVersion = '2.2';
var url = API_BASE + FLAG_RESPONSE + '?callback=?';
var data = {'phrase': phrase};
if (apiMethod) data['apiMethod'] = apiMethod;
if (apiVersion) data['apiVersion'] = apiVersion;
if (callbackUrl) data['callbackUrl'] = callbackUrl;
data = jsonConcat(data, options);
data = jsonConcat(data, getHeaders());
$.getJSON(url, data, callback);
}
};
};
})(jQuery);