1
\$\begingroup\$

I wrote code to get data from HTTP service and store it in MongoDB.
Please, can someone let me know how efficient my code is and whether it is good from the OOP point of view?

/*global Promise, getData, httpReq, searchData, getHotData, storeData*/

var http = require('http');
var CoubVideo = require('./models/coubdb.js');

function CoubApi (url, numPerPage, numOfPages) {
  this.url = url;
  this.numOfPages = numOfPages;
  this.numPerPage = numPerPage;
  this.getData = getData;
  this.httpReq = httpReq;
  this.searchData = searchData;
  this.getHotData = getHotData;
  this.storeData = storeData;
}

// Get data from server
function httpReq (url) {
  var promise = new Promise (function (resolve, reject) {

    http.get(url, function (res) {
      var data = '';

      res.on('data', function (chunk) {
        data += chunk;
      });

      res.on('end', function () {
        if(data.length > 0) {
          resolve(JSON.parse(data));
        } else {
          reject("Error: HTTP request rejected!");
        }
      });

    }).on('error', function (err) {
      console.log(err);
    });

  });

  return promise;
}

// Store data in MongoDB
function storeData (data, per_page) {

  function insertVideoDoc (i) {
    CoubVideo.count({id: data.coubs[i].id}, function (err, count) {
      if (err) {
        console.log(err);
      }

      if (count === 0 || count === null) {
        var video = new CoubVideo(data.coubs[i]);

        video.save(function (err) {
          if (err) {
            console.log(err);
          }
          console.log("Saved successfully!");
        });
      } else { 
        console.log("Duplicate");
      }
    });
  }

  var i;
  for(i = 0; i < per_page; i++) {
    insertVideoDoc(i);
  }
}

// Search for coubs
function searchData (searchtext, order, page, per_page) {
  var url = this.url +
            "search?q=" + searchtext +
            "&order_by=" + order +
            "&per_page=" + per_page + 
            "&page=" + page;

  this.httpReq(url).then(function (data) {
    this.storeData(data, per_page);
  }.bind(this));
}

// Get hot coubs
function getHotData (order, page, per_page) {
  var url = this.url +
            "timeline/hot?page=" + page +
            "&per_page=" + per_page + 
            "&order_by=" + order;

  this.httpReq(url).then(function (data) {
    this.storeData(data, per_page);
  }.bind(this));
}

// Get data
function getData () { 
  var i;
  for(i = 0; i < this.numOfPages; i++) {
    this.getHotData("newest_popular", i, this.numPerPage);
  }
}

var coub = new CoubApi("http://coub.com/api/v2/", 2, 50);
coub.getData();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.