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

test jio parser

parent 3d5b9c94
......@@ -2,75 +2,116 @@
/*global window, RSVP, rJS, jIO*/
(function(window, RSVP, rJS, jIO) {
"use strict";
rJS(window)
rJS(window)
.declareAcquiredMethod("get_model", "get_model")
.declareAcquiredMethod("get_model", "get_model")
.declareMethod("read_file", function(link) {
var storage;
var gadget = this;
gadget.get_model()
.push(function(result) {
storage = result;
})
.push(function() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
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>"));
gadget.is_already_parsed(storage, feed_title)
.push (function (resultat){
if (!resultat){
promise_list.push(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, storage));
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();
.setState({
storage : null
})
.ready(function(){
var gadget = this;
return gadget.get_model()
.push(function(result){
return gadget.changeState({
storage : result
});
});
})
.declareMethod("read_file_new", function(link){
var gadget = this;
return gadget.get_rss(link);
.push(function(result){
var rss,
title;
rss = result;
title = rss.slice(rss.indexOf("<title>")+7, rss.indexOf("</title>"));
return gadget.is_already_parsed(title)
.push(function(is_parsed){
if (!is_parsed){
return gadget.remove_css(rss)
.push(function(rss_without_css){
return gadget.state.storage.put(rss_without_css);
});
}
});
});
})
})
.declareMethod("parse", function(local_cur_text, storage) {
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 storage.put({
"title": local_cur_text.slice(title_start, title_end),
"link": local_cur_text.slice(link_start, link_end),
"body": result
.declareMethod("get_rss", function(link){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) return xmlhttp.responseText;
else return null;
}
xmlhttp.open("GET", link, true);
xmlhttp.send();
})
.declareMethod("read_file", function(link) {
var gadget,
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){
var regEx = new RegExp('(style="(.*?)"|style=&quot(.*?)&quot)', 'gi');
return body.replace(regEx, "");
})
.declareMethod("remove_css", function(body){
var regEx = new RegExp('(style="(.*?)"|style=&quot(.*?)&quot)', 'gi');
return body.replace(regEx, "");
})
.declareMethod("is_already_parsed", function(storage, title){
return storage.all_loaded_docs()
.push(function (result){
var i;
for (i = 0; i<result.data.rows.length; i+=1){
if (result.data.rows[i].id === title) return true;
}
return false;
})
.declareMethod("is_already_parsed", function(title){
return this.state.storage.all_loaded_docs()
.push(function (result){
var i;
for (i = 0; i<result.data.rows.length; i+=1){
if (result.data.rows[i].id === title) return true;
}
return false;
});
});
}(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