<commit_before>'use strict';

var Promise = require('bluebird');
var Coyote = require('coyote');
var uuid = require('uuid');
var ClientResponse = require('./client/Response');
var ErrorResponse = require('./client/ErrorResponse');

function Client(connection, type) {
  if (type && (type !== 'request' && type !== 'push' && type !== 'publish')) {
    throw new Error('Invalid client type');
  }

  type = type || 'request';

  this.connection = connection;
  this.messages   = {};
  this.ready      = false;

  switch (type) {
    case 'request':
      this.type = 'REQUEST';
      break;
    case 'push':
      this.type = 'PUSH';
      break;
    case 'publish':
      this.type = 'PUBLISH';
      break;
  }
}

Client.createClient = function(amqpUrl, type) {
  return Coyote.createConnection(amqpUrl)
    .bind(this)
    .then(function(connection) {
      return new Client(connection, type);
    });
};

Client.prototype.onMessage = function(message) {
  if (this.type !== 'REQUEST') {
    return;
  }

  var correlationId = message.properties.correlationId;

  if (this.messages[correlationId]) {

    if (message.content.error) {
      this.messages[correlationId].reject(new ErrorResponse(message));
    } else {
      this.messages[correlationId].resolve(new ClientResponse(message));
    }

    delete this.messages[correlationId];
  }
};

Client.prototype.call = function(method, params) {
  return Promise.using(this._getChannel(method), function(coyote) {
    return new Promise(function(resolve, reject) {
      var correlationId = uuid.v4();

      if (this.type === 'REQUEST') {
        this.messages[correlationId] = {
          resolve : resolve,
          reject  : reject
        };
      }

      coyote.write({
        properties : {
          correlationId : correlationId
        },
        content : new Buffer(JSON.stringify({
          method : method,
          params : params
        }))
      });
    }.bind(this));
  }.bind(this));
};

Client.prototype._getChannel = function(method) {
  return new Promise(function(resolve, reject) {
    var coyote = new Coyote(this.connection, this.type, method);
    coyote.on('ready', function() {
      resolve(coyote);
    });

    coyote.on('response', this.onMessage.bind(this));
  }.bind(this)).disposer(function(coyote) {
    coyote.shutdown();
  });
};

module.exports = Client;
<commit_msg>remove unnecessary bind<commit_after>'use strict';

var Promise = require('bluebird');
var Coyote = require('coyote');
var uuid = require('uuid');
var ClientResponse = require('./client/Response');
var ErrorResponse = require('./client/ErrorResponse');

function Client(connection, type) {
  if (type && (type !== 'request' && type !== 'push' && type !== 'publish')) {
    throw new Error('Invalid client type');
  }

  type = type || 'request';

  this.connection = connection;
  this.messages   = {};
  this.ready      = false;

  switch (type) {
    case 'request':
      this.type = 'REQUEST';
      break;
    case 'push':
      this.type = 'PUSH';
      break;
    case 'publish':
      this.type = 'PUBLISH';
      break;
  }
}

Client.createClient = function(amqpUrl, type) {
  return Coyote.createConnection(amqpUrl)
    .then(function(connection) {
      return new Client(connection, type);
    });
};

Client.prototype.onMessage = function(message) {
  if (this.type !== 'REQUEST') {
    return;
  }

  var correlationId = message.properties.correlationId;

  if (this.messages[correlationId]) {

    if (message.content.error) {
      this.messages[correlationId].reject(new ErrorResponse(message));
    } else {
      this.messages[correlationId].resolve(new ClientResponse(message));
    }

    delete this.messages[correlationId];
  }
};

Client.prototype.call = function(method, params) {
  return Promise.using(this._getChannel(method), function(coyote) {
    return new Promise(function(resolve, reject) {
      var correlationId = uuid.v4();

      if (this.type === 'REQUEST') {
        this.messages[correlationId] = {
          resolve : resolve,
          reject  : reject
        };
      }

      coyote.write({
        properties : {
          correlationId : correlationId
        },
        content : new Buffer(JSON.stringify({
          method : method,
          params : params
        }))
      });
    }.bind(this));
  }.bind(this));
};

Client.prototype._getChannel = function(method) {
  return new Promise(function(resolve, reject) {
    var coyote = new Coyote(this.connection, this.type, method);
    coyote.on('ready', function() {
      resolve(coyote);
    });

    coyote.on('response', this.onMessage.bind(this));
  }.bind(this)).disposer(function(coyote) {
    coyote.shutdown();
  });
};

module.exports = Client;

{'fraction_non_alphanumeric': 0.15207275803722503, 'fraction_numerical': 0.00042301184433164127, 'mean_word_length': 2.9507101086048455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '52637', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1401, 'n_words': 289}