-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32.js
More file actions
111 lines (97 loc) · 3.01 KB
/
Copy path32.js
File metadata and controls
111 lines (97 loc) · 3.01 KB
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
/**
* Copyright reelyActive 2016-2017
* We believe in an open Internet of Things
*/
angular.module('reelyactive.cormorant', [])
.factory('cormorant', function cormorantFactory($http) {
var stories = {};
function extractFromHtml(html) {
var tagIndex = html.search(/(<script\s*?type\s*?=\s*?"application\/ld\+json">)/);
if(tagIndex < 0) {
return null;
}
var startIndex = html.indexOf('>', tagIndex) + 1;
var stopIndex = html.indexOf('</script>', startIndex);
var jsonString = html.substring(startIndex, stopIndex);
try {
json = JSON.parse(jsonString);
}
catch(e) {
console.log(e);
console.log(jsonString);
return null;
}
return json;
}
function getStoryTypes(story) {
var types = [];
if(story && story.hasOwnProperty('@graph') &&
story['@graph'] instanceof Array) {
for(var cType = 0; cType < story['@graph'].length; cType++) {
types.push(story['@graph'][cType]['@type']);
}
}
return types;
}
function combine(story1, story2) {
var types1 = getStoryTypes(story1);
var types2 = getStoryTypes(story2);
if(types1.length > 0) {
var combined = Object.assign({}, story1);
for(var cType = 0; cType < types2.length; cType++) {
var index = types1.indexOf(types2[cType]);
if(index < 0) {
combined['@graph'].push(story2['@graph'][cType]);
}
else {
combined['@graph'][index] = story2['@graph'][cType];
}
}
return combined;
}
return story1;
}
var get = function(url, callback) {
if(!url || (typeof url !== 'string')) {
return callback(null, null);
}
if(stories.hasOwnProperty(url)) {
return callback(stories[url], url);
}
$http.defaults.headers.common.Accept = 'application/json, text/plain';
$http({ method: 'GET', url: url })
.then(function(response) { // Success
switch(typeof response.data) {
case 'string':
response.data = extractFromHtml(response.data);
case 'object':
stories[url] = response.data;
callback(response.data, url);
}
}, function(response) { // Error
console.log('cormorant: GET ' + url + ' returned status ' +
response.status);
stories[url] = null;
callback(null, url);
});
};
var getCombined = function(url1, url2, id, callback) {
get(url1, function(story1) {
if(!story1) {
return callback(null, id);
}
get(url2, function(story2) {
if(!story2) {
return callback(story1, id);
}
var combinedStory = combine(story1, story2);
callback(combinedStory, id);
});
});
};
return {
getStory: get,
getCombinedStory: getCombined,
getStories: function() { return stories; }
}
});