<commit_before>/* jshint node: true */
'use strict';
var CoreObject = require('core-object');
var Client = require('node-rest-client').Client;
var Promise = require('ember-cli/lib/ext/promise');
var chalk = require('chalk');
var moment = require('moment');

var green = chalk.green;
var red = chalk.red;

var DEFAULT_MANIFEST_SIZE = 10;

module.exports = CoreObject.extend({
  init: function(options) {
    this._super(options);

    this.manifestSize = this.manifestSize || DEFAULT_MANIFEST_SIZE;

    this.serviceHost = this.config.serviceHost;
    this.serviceNamespace = this.config.serviceNamespace;
    this.serviceIndexVersionResource = this.config.serviceIndexVersionResource;

    this.client = new Client({
      mimetypes: {
        json:["application/json","application/json; charset=utf-8"]
      }
    });
  },

  upload: function(buffer) {
    var key = this.taggingAdapter.createTag();
    var indexHtml = buffer.toString('utf8');
    return this._upload(key, indexHtml)
      .then(function() {
        this._writeLineInGreen("Your revision '" + key + "' was uploaded");
      }.bind(this))
      .catch(function(err) {
        this._writeLineInRed(err);
      }.bind(this));
  },

  list: function() {
    return this._list()
      .then(function(data) {
        this._writeLineInGreen("Your revisions: ");
        this._writeRevisions(data);
      }.bind(this))
      .catch(function(err) {
        this._writeLineInRed(err);
      }.bind(this));
  },

  activate: function(revision) {
    return this._activate(revision)
      .then(function() {
        this._writeLineInGreen('Revision ' + revision + ' was activated');
      }.bind(this))
      .catch(function(err) {
        this._writeLineInRed(err);
      }.bind(this));
  },

  _upload: function(key, indexHtml) {
    return new Promise(function(resolve, reject) {
      var path = this._buildPath();

      this._writeLineInGreen("POST '" + path + "' to add revision");
      this._writeEmptyLine();

      var args = {
        headers:{"Content-Type": "application/json"},
        data: {
          id: key,
          indexHtml: indexHtml
        }
      };

      this.client.post(path, args, function(data, response) {
        if (this._isValidPostResponse(response)) {
          resolve(data);
        } else {
          reject(data);
        }
      }.bind(this));

    }.bind(this));
  },

  _list: function() {
    return new Promise(function(resolve, reject) {
      var path = this._buildPath();

      this._writeLineInGreen("GET '" + path + "' to get revisions");
      this._writeEmptyLine();

      this.client.get(path, function(data, response) {
        if (response.statusCode === 200) {
          resolve(data);
        }
        else {
          reject(data);
        }
      }.bind(this));

    }.bind(this));
  },

  _activate: function(revision) {
    return new Promise(function(resolve, reject) {
      var path = this._buildPath() + '/' + encodeURIComponent(revision);

      this._writeLineInGreen("PUT '" + path + "' to activate revision");
      this._writeEmptyLine();

      var args = {
        headers: {"Content-Type": "application/json"}
      };

      this.client.put(path, args, function(data, response) {
        if (this._isValidPutResponse(response)) {
          resolve(data);
        }
        else {
          reject(data);
        }
      }.bind(this));
    }.bind(this));
  },

  _writeRevisions: function(revisions) {
    revisions
    .sort(function(a, b) {
      var date1 = new Date(a.createdOn);
      var date2 = new Date(b.createdOn);
      return date2 - date1;
    })
    .slice(0, 10)
    .forEach(function(revision, i) {
      var counter = i + 1;
      var message = counter + ". " + revision.id + " (" + moment(revision.createdOn).fromNow() + ")";
      this._writeLineInGreen(message);
    }.bind(this));

    this._writeEmptyLine();
    this._writeLineInGreen("Run deploy:activate to set one of your revisions as current");
  },

  _writeLineInGreen: function(message) {
    this.ui.writeLine(green(message));
  },

  _writeLineInRed: function(message) {
    this.ui.writeLine(red(message));
  },

  _writeEmptyLine: function() {
    this.ui.writeLine('');
  },

  _buildPath: function() {
    return this.serviceHost + '/' + this.serviceNamespace + '/' + this.serviceIndexVersionResource;
  },

  _isValidPostResponse: function(response) {
    var statusCode = response.statusCode;
    return statusCode === 200 || statusCode === 201 || statusCode === 204;
  },

  _isValidPutResponse: function(response) {
    var statusCode = response.statusCode;
    return statusCode === 200 || statusCode === 204;
  }
});
<commit_msg>added support for http basic auth<commit_after>/* jshint node: true */
'use strict';
var CoreObject = require('core-object');
var Client = require('node-rest-client').Client;
var Promise = require('ember-cli/lib/ext/promise');
var chalk = require('chalk');
var moment = require('moment');

var green = chalk.green;
var red = chalk.red;

var DEFAULT_MANIFEST_SIZE = 10;

module.exports = CoreObject.extend({
  init: function(options) {
    this._super(options);

    this.manifestSize = this.manifestSize || DEFAULT_MANIFEST_SIZE;

    this.serviceHost = this.config.serviceHost;
    this.serviceNamespace = this.config.serviceNamespace;
    this.serviceIndexVersionResource = this.config.serviceIndexVersionResource;
    this.basicAuthUser = this.config.basicAuthUser;
    this.basicAuthPassword = this.config.basicAuthPassword;

    var clientOpts = {
      mimetypes: {
        json: ["application/json", "application/json;charset=utf-8", "application/json; charset=utf-8"]
      }
    };
    if (this.basicAuthUser) { clientOpts.user = this.basicAuthUser; }
    if (this.basicAuthPassword) { clientOpts.password = <PASSWORD>; }

    this.client = new Client(clientOpts);
  },

  upload: function(buffer) {
    var key = this.taggingAdapter.createTag();
    var indexHtml = buffer.toString('utf8');
    return this._upload(key, indexHtml)
      .then(function() {
        this._writeLineInGreen("Your revision '" + key + "' was uploaded");
      }.bind(this))
      .catch(function(err) {
        this._writeLineInRed(err);
      }.bind(this));
  },

  list: function() {
    return this._list()
      .then(function(data) {
        this._writeLineInGreen("Your revisions: ");
        this._writeRevisions(data);
      }.bind(this))
      .catch(function(err) {
        this._writeLineInRed(err);
      }.bind(this));
  },

  activate: function(revision) {
    return this._activate(revision)
      .then(function() {
        this._writeLineInGreen('Revision ' + revision + ' was activated');
      }.bind(this))
      .catch(function(err) {
        this._writeLineInRed(err);
      }.bind(this));
  },

  _upload: function(key, indexHtml) {
    return new Promise(function(resolve, reject) {
      var path = this._buildPath();

      this._writeLineInGreen("POST '" + path + "' to add revision");
      this._writeEmptyLine();

      var args = {
        headers:{"Content-Type": "application/json"},
        data: {
          id: key,
          indexHtml: indexHtml
        }
      };

      this.client.post(path, args, function(data, response) {
        if (this._isValidPostResponse(response)) {
          resolve(data);
        } else {
          reject(data);
        }
      }.bind(this));

    }.bind(this));
  },

  _list: function() {
    return new Promise(function(resolve, reject) {
      var path = this._buildPath();

      this._writeLineInGreen("GET '" + path + "' to get revisions");
      this._writeEmptyLine();

      this.client.get(path, function(data, response) {
        if (response.statusCode === 200) {
          resolve(data);
        }
        else {
          reject(data);
        }
      }.bind(this));

    }.bind(this));
  },

  _activate: function(revision) {
    return new Promise(function(resolve, reject) {
      var path = this._buildPath() + '/' + encodeURIComponent(revision);

      this._writeLineInGreen("PUT '" + path + "' to activate revision");
      this._writeEmptyLine();

      var args = {
        headers: {"Content-Type": "application/json"}
      };

      this.client.put(path, args, function(data, response) {
        if (this._isValidPutResponse(response)) {
          resolve(data);
        }
        else {
          reject(data);
        }
      }.bind(this));
    }.bind(this));
  },

  _writeRevisions: function(revisions) {
    revisions
    .sort(function(a, b) {
      var date1 = new Date(a.createdOn);
      var date2 = new Date(b.createdOn);
      return date2 - date1;
    })
    .slice(0, 10)
    .forEach(function(revision, i) {
      var counter = i + 1;
      var message = counter + ". " + revision.id + " (" + moment(revision.createdOn).fromNow() + ")";
      this._writeLineInGreen(message);
    }.bind(this));

    this._writeEmptyLine();
    this._writeLineInGreen("Run deploy:activate to set one of your revisions as current");
  },

  _writeLineInGreen: function(message) {
    this.ui.writeLine(green(message));
  },

  _writeLineInRed: function(message) {
    this.ui.writeLine(red(message));
  },

  _writeEmptyLine: function() {
    this.ui.writeLine('');
  },

  _buildPath: function() {
    return this.serviceHost + '/' + this.serviceNamespace + '/' + this.serviceIndexVersionResource;
  },

  _isValidPostResponse: function(response) {
    var statusCode = response.statusCode;
    return statusCode === 200 || statusCode === 201 || statusCode === 204;
  },

  _isValidPutResponse: function(response) {
    var statusCode = response.statusCode;
    return statusCode === 200 || statusCode === 204;
  }
});

{'fraction_non_alphanumeric': 0.14824016563146997, 'fraction_numerical': 0.006314699792960662, 'mean_word_length': 2.778255768478686, 'pattern_counts': {'":': 4, '<': 4, '<?xml version=': 0, '>': 4, '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': '7178262', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2891, 'n_words': 560}