<commit_before>'use strict';

const _ = require('lodash');

_.mixin({
  denote: (object) => {
    const value = _.get(object, _.head(_.keys(object)));
    return _.isObjectLike(value) ? value : object;
  },
});

const wrap = (path, request) => {
  this[path] = request.defaults({ url: `/${path}` });
};

const $ = (path, method, args) => new Promise((resolve, reject) => {
  _.invoke(this, `${path}.${method}`, {
    [method === 'get' ? 'qs' : 'form']: _.omitBy(args, _.isNil),
  }, (error, response, body) => {
    const e = error || body.error;

    !e ? resolve(_.denote(body)) : reject(e);
  });
});

// @class  PushJet
class PushJet {
  constructor(baseUrl) {
    // create private wrappers
    const request = require('request').defaults({ baseUrl, json: true });

    _.each(['message', 'service', 'subscription', 'gcm'], (path) => {
      wrap(path, request);
    });
  };

  sendMessage(secret, message, title, level, link) {
    return $('message', 'post', { secret, message, title, level, link });
  }

  fetchUnreadMessages(uuid) {
    return $('message', 'get', { uuid });
  }

  markMessagesAsRead(uuid) {
    return $('message', 'delete', { uuid });
  }

  createService(name, icon) {
    return $('service', 'post', { name, icon });
  }

  getServiceInfo(service, secret) {
    return $('service', 'get', { service, secret });
  }

  updateServiceInfo(secret, name, icon) {
    return $('service', 'patch', { secret, name, icon });
  }

  deleteService(secret) {
    return $('service', 'delete', { secret });
  }

  subscribeToService(uuid, service) {
    return $('subscription', 'post', { uuid, service });
  }

  getSubscriptions(uuid) {
    return $('subscription', 'get', { uuid });
  }

  unsubscribe(uuid, service) {
    return $('subscription', 'delete', { uuid, service });
  }

  registerDeviceForGCM(uuid, regid, pubkey) {
    return $('gcm', 'post', {  uuid, regid, pubkey });
  }

  removingGCMRegistration(uuid) {
    return $('gcm', 'delete', { uuid });
  }
}

module.exports = PushJet;<commit_msg>namespace for wrappers<commit_after>'use strict';

const _ = require('lodash');

_.mixin({
  denote: (object) => {
    const value = _.get(object, _.head(_.keys(object)));
    return _.isObjectLike(value) ? value : object;
  },
});

const internal = new WeakMap();

const $$ = (object) => {
  if (!internal.has(object)) {
    internal.set(object, {});
  }
  return internal.get(object);
};

const wrap = (pushjet, path, request) => {
  $$(pushjet)[path] = request.defaults({ url: `/${path}` });
};

const $ = (pushjet, path, method, args) => new Promise((resolve, reject) => {
  _.invoke($$(pushjet), `${path}.${method}`, {
    [method === 'get' ? 'qs' : 'form']: _.omitBy(args, _.isNil),
  }, (error, response, body) => {
    const e = error || body.error;

    !e ? resolve(_.denote(body)) : reject(e);
  });
});

// @class  PushJet
class PushJet {
  constructor(baseUrl) {
    // create private wrappers
    const request = require('request').defaults({ baseUrl, json: true });

    _.each(['message', 'service', 'subscription', 'gcm'], (path) => {
      wrap(this, path, request);
    });
  };

  sendMessage(secret, message, title, level, link) {
    return $(this, 'message', 'post', { secret, message, title, level, link });
  }

  fetchUnreadMessages(uuid) {
    return $(this, 'message', 'get', { uuid });
  }

  markMessagesAsRead(uuid) {
    return $(this, 'message', 'delete', { uuid });
  }

  createService(name, icon) {
    return $(this, 'service', 'post', { name, icon });
  }

  getServiceInfo(service, secret) {
    return $(this, 'service', 'get', { service, secret });
  }

  updateServiceInfo(secret, name, icon) {
    return $(this, 'service', 'patch', { secret, name, icon });
  }

  deleteService(secret) {
    return $(this, 'service', 'delete', { secret });
  }

  subscribeToService(uuid, service) {
    return $(this, 'subscription', 'post', { uuid, service });
  }

  getSubscriptions(uuid) {
    return $(this, 'subscription', 'get', { uuid });
  }

  unsubscribe(uuid, service) {
    return $(this, 'subscription', 'delete', { uuid, service });
  }

  registerDeviceForGCM(uuid, regid, pubkey) {
    return $(this, 'gcm', 'post', {  uuid, regid, pubkey });
  }

  removingGCMRegistration(uuid) {
    return $(this, 'gcm', 'delete', { uuid });
  }
}

module.exports = PushJet;
{'fraction_non_alphanumeric': 0.20705556836522943, 'fraction_numerical': 0.0, 'mean_word_length': 3.619808306709265, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '6448063', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1442, 'n_words': 337}