-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrssparser.js
78 lines (70 loc) · 2.33 KB
/
rssparser.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
(function() {
var _yahooPipeUrl = 'http://pipes.yahoo.com/pipes/pipe.run?_id=2FV68p9G3BGVbc7IdLq02Q&_render=json&feedcount=100&feedurl=';
var _rssFeedUrl = '';
var _objectClass;
var _success;
var _error;
var _map;
var processJSON = function(jsonString) {
var promises = [];
var json = JSON.parse(jsonString);
var items = json['value']['items'];
for (var i = 0; i < items.length; i++) {
var item = items[i];
promises.push(processItem(item));
};
return Parse.Promise.when(promises);
};
var processItem = function(item) {
var promise = new Parse.Promise();
var properties = _map(item);
var query = new Parse.Query(_objectClass);
var uniqueIdentifierKey = getFirstKeyOfArray(properties);
query.equalTo(uniqueIdentifierKey, properties[uniqueIdentifierKey]);
query.first().then(function(object) {
if (object === undefined) {
object = new _objectClass();
}
for (var key in properties) {
var property = properties[key];
object.set(key, property);
}
object.save().then(function() {
promise.resolve();
});
});
return promise;
};
var getFirstKeyOfArray = function (data) {
for (var key in data) {
return key;
}
}
module.exports = {
initialize: function(feedUrl, className) {
_rssFeedUrl = encodeURIComponent(feedUrl);
_objectClass = Parse.Object.extend(className);
return this;
},
parse: function(success, error, map) {
_success = success;
_error = error;
_map = map;
Parse.Cloud.httpRequest({
url: _yahooPipeUrl + _rssFeedUrl,
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
success: function(httpResponse) {
processJSON(httpResponse.text).then(function() {
_success();
});
},
error: function(httpResponse) {
_error(httpResponse);
}
});
},
version: '1.0.0'
}
})();