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

test jio parser

parent 3d5b9c94
...@@ -6,28 +6,69 @@ ...@@ -6,28 +6,69 @@
.declareAcquiredMethod("get_model", "get_model") .declareAcquiredMethod("get_model", "get_model")
.declareMethod("read_file", function(link) { .setState({
var storage; storage : null
})
.ready(function(){
var gadget = this; var gadget = this;
gadget.get_model() return gadget.get_model()
.push(function(result) { .push(function(result){
storage = result; return gadget.changeState({
storage : result
});
});
}) })
.push(function() {
.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("get_rss", function(link){
var xmlhttp = new XMLHttpRequest(); var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() { xmlhttp.onreadystatechange = function() {
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) { if (xmlhttp.status == 200 && xmlhttp.readyState == 4) return xmlhttp.responseText;
var cur_text = xmlhttp.responseText, else return null;
}
xmlhttp.open("GET", link, true);
xmlhttp.send();
})
.declareMethod("read_file", function(link) {
var gadget,
xmlhttp,
cur_text,
promise_list = [], promise_list = [],
feed_title; 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>")); feed_title = cur_text.slice(cur_text.indexOf("<title>")+7, cur_text.indexOf("</title>"));
gadget.is_already_parsed(storage, feed_title) return gadget.is_already_parsed(feed_title)
.push (function (resultat){ .push (function (resultat){
if (!resultat){ if (!resultat){
promise_list.push(storage.loaded_doc(feed_title, {"loaded" : "true"})); 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); cur_text = cur_text.slice(cur_text.indexOf("<item>"), cur_text.length - 1);
while (cur_text.indexOf("<item>") > -1) { while (cur_text.indexOf("<item>") > -1) {
promise_list.push(gadget.parse(cur_text, storage)); promise_list.push(gadget.parse(cur_text));
cur_text = cur_text.slice(cur_text.indexOf("</item>") + 6, cur_text.length - 1); cur_text = cur_text.slice(cur_text.indexOf("</item>") + 6, cur_text.length - 1);
} }
RSVP.all(promise_list); RSVP.all(promise_list);
...@@ -38,9 +79,9 @@ ...@@ -38,9 +79,9 @@
xmlhttp.open("GET", link, true); xmlhttp.open("GET", link, true);
xmlhttp.send(); xmlhttp.send();
}); });
}) })
.declareMethod("parse", function(local_cur_text, storage) { .declareMethod("parse", function(local_cur_text) {
var title_start = local_cur_text.indexOf("<title>") + 7, var title_start = local_cur_text.indexOf("<title>") + 7,
title_end = local_cur_text.indexOf("</title>"), title_end = local_cur_text.indexOf("</title>"),
link_start = local_cur_text.indexOf("<link>") + 6, link_start = local_cur_text.indexOf("<link>") + 6,
...@@ -49,28 +90,28 @@ ...@@ -49,28 +90,28 @@
body_end = local_cur_text.indexOf("</description>") + 14; body_end = local_cur_text.indexOf("</description>") + 14;
return this.remove_css(local_cur_text.slice(body_start, body_end)) return this.remove_css(local_cur_text.slice(body_start, body_end))
.push (function(result){ .push (function(result){
return storage.put({ return gadget.state.storage.put({
"title": local_cur_text.slice(title_start, title_end), "title": local_cur_text.slice(title_start, title_end),
"link": local_cur_text.slice(link_start, link_end), "link": local_cur_text.slice(link_start, link_end),
"body": result "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