<commit_before>var jsondb = require('com.irlgaming.jsondb');
jsondb.debug(true);

var exports = {
  load:function(/* string */ blogger, /* function */callback){
    initJSONDB();
    var self = this;
    var localCollection = jsondb.factory('localJSONDB', 'asunaroblog');
    // alert(localCollection.count({blogger:{$eq:blogger}}));
    var count = localCollection.count({blogger:{$eq:blogger}});
    if(count >= 1){
      var entries = localCollection.find({
        blogger:{$eq:blogger}
      },{
        $limit:5
      });
      callback(entries);
    }else{
      self.loadFromSakura(blogger,callback);
    }
  },
  loadFromSakura:function(/* string */ blogger, /* function */callback){
    if(isConnected){
      var xhr = Ti.Network.createHTTPClient();
      var url = "http://h5y1m141.info/entry/" + blogger + "/page/0";
      xhr.open('GET',url);
      xhr.onload = function(){
        var res = JSON.parse(this.responseText);
        var result = sorted(res);
        var localCollection = jsondb.factory('localJSONDB', 'asunaroblog');
        for(var i=0;i<result.length;i++){
          localCollection.save({
            permalink:result[i].permalink,
            html_body:result[i].html_body,
            blogger:result[i].blogger,
            post_date:result[i].post_date,
            title:result[i].title
          });
          localCollection.ensureIndex({'post_date':1});
          localCollection.commit();
        }
        callback(result);
      };
      xhr.error = function(){
        var dialog = Ti.UI.createAlertDialog({
          title: "ネットワーク接続エラー",
          message: "ネットワーク接続が確立されていません。再度お試しください"
        });
        dialog.show();
      };
      xhr.send();
    }else{
      var dialog = Ti.UI.createAlertDialog({
        title: "ネットワーク接続できていません"
      });
      dialog.show();
    }

  },
  findLocalJSONDB:function(blogger,callback){
    var localCollection = jsondb.factory('localJSONDB', 'asunaroblog');
    var entries = localCollection.find({
      blogger:{$eq:blogger}
    },{
      // $limit:5,
      $sort:{
        post_date:1
      }
    });
    for(var i=0;i<entries.length;i++){
      Ti.API.info(entries[i].title);
    }
    callback(sorted(entries));

  }
};
// private method

function initJSONDB(){
  Ti.API.info(Titanium.Utils.sha1("obj"));
  Ti.API.info(Ti.Filesystem.applicationDataDirectory);

};
function isConnected(){
  if(Titanium.Network.online===true){
    return true;
  }
};
function sorted(entries){
  var _ = require('lib/underscore')._;
  var value = _.chain(entries)
      .sortBy(function(item) {return item.post_date; })
      .reverse()
      .value();
  return value;
}<commit_msg>JSONDB利用時のvar localCollection = jsondb.factory・・の記述が一部冗長になっていたのでそれを使いまわすように修正<commit_after>var jsondb = require('com.irlgaming.jsondb');
jsondb.debug(true);
var localCollection = jsondb.factory('localJSONDB', 'asunaroblog');
var exports = {
  load:function(/* string */ blogger, /* function */callback){
    initJSONDB();
    var self = this;

    // alert(localCollection.count({blogger:{$eq:blogger}}));
    var count = localCollection.count({blogger:{$eq:blogger}});
    if(count >= 1){
      var entries = localCollection.find({
        blogger:{$eq:blogger}
      },{
        $limit:5
      });
      callback(entries);
    }else{
      self.loadFromSakura(blogger,callback);
    }
  },
  loadFromSakura:function(/* string */ blogger, /* function */callback){
    if(isConnected){
      var xhr = Ti.Network.createHTTPClient();
      var url = "http://h5y1m141.info/entry/" + blogger + "/page/0";
      xhr.open('GET',url);
      xhr.onload = function(){
        var res = JSON.parse(this.responseText);
        var result = sorted(res);

        for(var i=0;i<result.length;i++){
          localCollection.save({
            permalink:result[i].permalink,
            html_body:result[i].html_body,
            blogger:result[i].blogger,
            post_date:result[i].post_date,
            title:result[i].title
          });
          localCollection.ensureIndex({'post_date':1});
          localCollection.commit();
        }
        callback(result);
      };
      xhr.error = function(){
        var dialog = Ti.UI.createAlertDialog({
          title: "ネットワーク接続エラー",
          message: "ネットワーク接続が確立されていません。再度お試しください"
        });
        dialog.show();
      };
      xhr.send();
    }else{
      var dialog = Ti.UI.createAlertDialog({
        title: "ネットワーク接続できていません"
      });
      dialog.show();
    }

  },
  findLocalJSONDB:function(blogger,callback){
    var localCollection = jsondb.factory('localJSONDB', 'asunaroblog');
    var entries = localCollection.find({
      blogger:{$eq:blogger}
    },{
      // $limit:5,
      $sort:{
        post_date:1
      }
    });
    for(var i=0;i<entries.length;i++){
      Ti.API.info(entries[i].title);
    }
    callback(sorted(entries));

  }
};
// private method

function initJSONDB(){
  Ti.API.info(Titanium.Utils.sha1("obj"));
  Ti.API.info(Ti.Filesystem.applicationDataDirectory);

};
function isConnected(){
  if(Titanium.Network.online===true){
    return true;
  }
};
function sorted(entries){
  var _ = require('lib/underscore')._;
  var value = _.chain(entries)
      .sortBy(function(item) {return item.post_date; })
      .reverse()
      .value();
  return value;
}
{'fraction_non_alphanumeric': 0.16003019437629742, 'fraction_numerical': 0.005284015852047556, 'mean_word_length': 2.64010989010989, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6856106', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1774, 'n_words': 258}