-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpearson_sdk.js
238 lines (195 loc) · 6.22 KB
/
pearson_sdk.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
var to_query = require("querystring");
var sys = require("util");
var rest = require("restler");
var PearsonApis = function() {
// Base Url for API call http://api.pearson.com/v2/
var Apis = {
dictionaries: function(apikey, proxy) {
return new Pearson(apikey, proxy).dictionaries();
},
travel: function(apikey, proxy) {
return new Pearson(apikey, proxy).travel();
},
foodanddrink: function(apikey, proxy) {
return new Pearson(apikey, proxy).foodanddrink();
},
ftarticles: function(apikey, proxy) {
return new Pearson(apikey, proxy).ftarticles();
}
};
/// BASE OBJECT
function Pearson(apikey, proxy) {
'use strict';
if (typeof proxy == "undefined"){
this.apikey = "UpLGN1J9sY0YCynH0cFovSjGWYGvhFYH";
this.base = "http://api.pearson.com/v2/"
} else {
// apikey provided by proxy
this.base = proxy + "/v2/"
}
return this;
};
Pearson.prototype.dictionaries = function() {
'use strict';
this.api = "dictionaries";
this.url = this.base + this.api + "/";
this.entries = new Endpoint(this,"entries");
return this;
};
Pearson.prototype.foodanddrink = function() {
'use strict';
this.api = "foodanddrink";
this.url = this.base + this.api + "/";
this.recipes = new Endpoint(this, "recipes");
return this;
};
Pearson.prototype.ftarticles = function() {
'use strict';
this.api = "ft";
this.url = this.base + this.api + "/";
this.articles = new Endpoint(this, "articles");
return this;
};
Pearson.prototype.travel = function() {
'use strict';
this.api = "travel";
this.url = this.base + this.api + "/";
this.topten = new Endpoint(this, "topten");
this.streetsmart = new Endpoint(this, "streetsmart");
this.aroundtown = new Endpoint(this, "around_town");
this.places = new Endpoint(this, "places");
this.dataset = new Endpoint(this, "datasets");
this.categories = new Endpoint(this, "categories");
return this;
};
Pearson.prototype.setDatasets = function() {
// Arguments can be more than one comma delimited string. Whitespace is stripped.
var args;
var split;
var stripped;
var args = Array.prototype.slice.call(arguments);
split = args.join(",");
stripped = split.replace(/\s+/g,"");
this.dsets = stripped;
return this;
};
// fetches an item / article by the url provided on the results object.
Pearson.prototype.expandUrl = function(url) {
'use strict';
var itemUrl;
var prepend = "http://api.pearson.com"
if (typeof this.apikey === "undefined") {
itemUrl = prepend + url
} else {
itemUrl = prepend + url + "?apikey=" + this.apikey;
};
return itemUrl;
};
// Endpoint(s)
function Endpoint(pearson,path) {
this.pearson = pearson;
this.path = path;
};
Endpoint.prototype.setDatasets = function() {
var args;
var split;
var stripped;
var args = Array.prototype.slice.call(arguments);
split = args.join(",");
stripped = split.replace(/\s+/g,"");
this.pearson.dsets = stripped;
return this;
};
Endpoint.prototype.search = function(json, offset, limit){
'use strict';
var query;
var fullUrl;
if (typeof json === 'undefined') {
json = {}
}
json.limit = limit || 10;
json.offset = offset || 0;
if (typeof this.pearson.apikey === "undefined") {
query = to_query.stringify(json);
this.query = query;
} else {
json.apikey = this.pearson.apikey;
query = to_query.stringify(json);
this.query = query;
};
if (typeof this.pearson.dsets === "undefined" || this.pearson.dsets == ""){
fullUrl = this.pearson.url + this.path + "?" + query;
} else {
fullUrl = this.pearson.url + this.pearson.dsets + "/" + this.path + "?" + query;
};
rest.get(fullUrl).on('complete', function(result) {
if (result instanceof Error) {
console.log('Error: ' + result.message);
this.retry(5000); // try again after 5 sec
} else {
return result;
}
});
};
Endpoint.prototype.getSearchUrl = function(json, offset, limit){ // This is for the Frisby tests and to separate the url function from the api call if using request in node.
'use strict';
var query;
var fullUrl;
if (typeof json === 'undefined') {
json = {}
}
json.limit = limit || 10;
json.offset = offset || 0;
if (typeof this.pearson.apikey === "undefined") {
query = to_query.stringify(json);
this.query = query;
} else {
json.apikey = this.pearson.apikey;
query = to_query.stringify(json);
this.query = query;
};
if (typeof this.pearson.dsets === "undefined" || this.pearson.dsets == ""){
fullUrl = this.pearson.url + this.path + "?" + query;
} else {
fullUrl = this.pearson.url + this.pearson.dsets + "/" + this.path + "?" + query;
};
return fullUrl;
};
Endpoint.prototype.getIdUrl = function(Id) { // Again, this is purely for testing purposes so that Frisby handles the api request.
'use strict';
var fullUrl = this.pearson.url + this.path + "/"+Id;
if (typeof this.pearson.apikey !== "undefined" && this.pearson.apikey != "") {
fullUrl = fullUrl + "?apikey="+this.pearson.apikey
}
return fullUrl;
};
Endpoint.prototype.getById = function(Id) {
'use strict';
var fullUrl = this.pearson.url + this.path + "/"+Id;
if (typeof this.pearson.apikey !== "undefined" && this.pearson.apikey != "") {
fullUrl = fullUrl + "?apikey="+this.pearson.apikey
}
rest.get(fullUrl).on('complete', function(result) {
if (result instanceof Error) {
console.log('Error: ' + result.message);
this.retry(5000); // try again after 5 sec
} else {
return result;
}
});
};
// fetches an item / article by the url provided on the results object.
Endpoint.prototype.expandUrl = function(url) {
'use strict';
var itemUrl;
var prepend = "http://api.pearson.com"
if (typeof this.pearson.apikey === "undefined") {
itemUrl = prepend + url
} else {
itemUrl = prepend + url + "?apikey=" + this.pearson.apikey;
};
return itemUrl;
};
return Apis;
};
module.exports = new PearsonApis();