Commit 43722c16 authored by Alexandra Rogova's avatar Alexandra Rogova

test jio parser

parent 3d5b9c94
...@@ -2,75 +2,116 @@ ...@@ -2,75 +2,116 @@
/*global window, RSVP, rJS, jIO*/ /*global window, RSVP, rJS, jIO*/
(function(window, RSVP, rJS, jIO) { (function(window, RSVP, rJS, jIO) {
"use strict"; "use strict";
rJS(window) rJS(window)
.declareAcquiredMethod("get_model", "get_model") .declareAcquiredMethod("get_model", "get_model")
.declareMethod("read_file", function(link) { .setState({
var storage; storage : null
var gadget = this; })
gadget.get_model()
.push(function(result) { .ready(function(){
storage = result; var gadget = this;
}) return gadget.get_model()
.push(function() { .push(function(result){
var xmlhttp = new XMLHttpRequest(); return gadget.changeState({
xmlhttp.onreadystatechange = function() { storage : result
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) { });
var cur_text = xmlhttp.responseText, });
promise_list = [], })
feed_title;
feed_title = cur_text.slice(cur_text.indexOf("<title>")+7, cur_text.indexOf("</title>")); .declareMethod("read_file_new", function(link){
gadget.is_already_parsed(storage, feed_title) var gadget = this;
.push (function (resultat){ return gadget.get_rss(link);
if (!resultat){ .push(function(result){
promise_list.push(storage.loaded_doc(feed_title, {"loaded" : "true"})); var rss,
cur_text = cur_text.slice(cur_text.indexOf("<item>"), cur_text.length - 1); title;
while (cur_text.indexOf("<item>") > -1) { rss = result;
promise_list.push(gadget.parse(cur_text, storage)); title = rss.slice(rss.indexOf("<title>")+7, rss.indexOf("</title>"));
cur_text = cur_text.slice(cur_text.indexOf("</item>") + 6, cur_text.length - 1); return gadget.is_already_parsed(title)
} .push(function(is_parsed){
RSVP.all(promise_list); if (!is_parsed){
} return gadget.remove_css(rss)
}); .push(function(rss_without_css){
} return gadget.state.storage.put(rss_without_css);
}; });
xmlhttp.open("GET", link, true); }
xmlhttp.send(); });
}); });
}) })
.declareMethod("parse", function(local_cur_text, storage) { .declareMethod("get_rss", function(link){
var title_start = local_cur_text.indexOf("<title>") + 7, var xmlhttp = new XMLHttpRequest();
title_end = local_cur_text.indexOf("</title>"), xmlhttp.onreadystatechange = function() {
link_start = local_cur_text.indexOf("<link>") + 6, if (xmlhttp.status == 200 && xmlhttp.readyState == 4) return xmlhttp.responseText;
link_end = local_cur_text.indexOf("</link>"), else return null;
body_start = local_cur_text.indexOf("<description>"), }
body_end = local_cur_text.indexOf("</description>") + 14; xmlhttp.open("GET", link, true);
return this.remove_css(local_cur_text.slice(body_start, body_end)) xmlhttp.send();
.push (function(result){ })
return storage.put({
"title": local_cur_text.slice(title_start, title_end), .declareMethod("read_file", function(link) {
"link": local_cur_text.slice(link_start, link_end), var gadget,
"body": result xmlhttp,
cur_text,
promise_list = [],
feed_title;
gadget = this;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
cur_text = xmlhttp.responseText,
feed_title = cur_text.slice(cur_text.indexOf("<title>")+7, cur_text.indexOf("</title>"));
return gadget.is_already_parsed(feed_title)
.push (function (resultat){
if (!resultat){
promise_list.push(gadget.state.storage.loaded_doc(feed_title, {"loaded" : "true"}));
cur_text = cur_text.slice(cur_text.indexOf("<item>"), cur_text.length - 1);
while (cur_text.indexOf("<item>") > -1) {
promise_list.push(gadget.parse(cur_text));
cur_text = cur_text.slice(cur_text.indexOf("</item>") + 6, cur_text.length - 1);
}
RSVP.all(promise_list);
}
}); });
}); }
}) };
xmlhttp.open("GET", link, true);
xmlhttp.send();
});
})
.declareMethod("parse", function(local_cur_text) {
var title_start = local_cur_text.indexOf("<title>") + 7,
title_end = local_cur_text.indexOf("</title>"),
link_start = local_cur_text.indexOf("<link>") + 6,
link_end = local_cur_text.indexOf("</link>"),
body_start = local_cur_text.indexOf("<description>"),
body_end = local_cur_text.indexOf("</description>") + 14;
return this.remove_css(local_cur_text.slice(body_start, body_end))
.push (function(result){
return gadget.state.storage.put({
"title": local_cur_text.slice(title_start, title_end),
"link": local_cur_text.slice(link_start, link_end),
"body": result
});
});
})
.declareMethod("remove_css", function(body){ .declareMethod("remove_css", function(body){
var regEx = new RegExp('(style="(.*?)"|style=&quot(.*?)&quot)', 'gi'); var regEx = new RegExp('(style="(.*?)"|style=&quot(.*?)&quot)', 'gi');
return body.replace(regEx, ""); return body.replace(regEx, "");
}) })
.declareMethod("is_already_parsed", function(storage, title){ .declareMethod("is_already_parsed", function(title){
return storage.all_loaded_docs() return this.state.storage.all_loaded_docs()
.push(function (result){ .push(function (result){
var i; var i;
for (i = 0; i<result.data.rows.length; i+=1){ for (i = 0; i<result.data.rows.length; i+=1){
if (result.data.rows[i].id === title) return true; if (result.data.rows[i].id === title) return true;
} }
return false; return false;
})
}); });
});
}(window, RSVP, rJS, jIO)); }(window, RSVP, rJS, jIO));
<!doctype html>
<html>
<head>
<title>Jio parser test</title>
<script src="../jio/external/rsvp-2.0.4.js"></script>
<script src="../jio/dist/jio-latest.js"></script>
<script src="../renderjs/dist/renderjs-latest.js"></script>
<script src="jio-parser-test.js"></script>
</head>
<body>
</body>
</html>
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80*/
/*global window, RSVP, rJS, jIO*/
(function (window, RSVP, rJS, jIO) {
"use strict";
rJS(window)
.ready(function(){
var gadget = this;
gadget.test_storage = jIO.createJIO({
type : "parser",
document_id : "doc_id",
attachment_id :"att_id",
parser : "rss",
sub_storage : {
type : "indexeddb",
database : "test"
}
});
return gadget.test_storage.put("doc_id", {})
.push (function(){
return gadget.get_test_rss();
})
.push(function(test_rss){
return gadget.test_storage.putAttachment("doc_id", "att_id", new Blob([test_rss]));
})
.push(function(){
return gadget.test_storage.get("doc_id");
})
.push(function(result){
console.log("get result : ");
console.log(result);
});
})
.declareMethod("get_test_rss", function(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) return xmlhttp.responseText;
else return null;
}
xmlhttp.open("GET", "./test-files/vivelessvt.rss", true);
xmlhttp.send();
});
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment