<commit_before>module.exports = function(Subscriber) {

  // Google Maps API has a rate limit of 10 requests per second
  // Seems we need to enforce a lower rate to prevent errors
  var lookupGeo = require('function-rate-limit')(5, 1000, function() {
    var geoService = Subscriber.app.dataSources.geo;
    geoService.geocode.apply(geoService, arguments);
  });


  Subscriber.beforeRemote('prototype.updateAttributes', function(ctx, user, next) {
    var body = ctx.req.body;
    console.log('before updateAttributes...');
    if (body                    &&
        body.preferences        &&
        body.preferences.street &&
        body.preferences.city   &&
        body.preferences.state ) {

      var loc = body.preferences;
      console.log('preferences present');

      // geo code the address
      lookupGeo(loc.street, loc.city, loc.state,
        function(err, result) {
           if (result && result[0]) {
            body.preferences.geo = result[0];
            next();
          } else {
             //TODO: Need to find out how to handle this with better a UX experience
            next(new Error('could not find location'));
          }
        });

    } else {
      console.log('NO preferences present');
      next();
    }

  });


  Subscriber.getWeather = function(subscriberId, cb){
    Subscriber.findById(subscriberId, function (err, instance) {

      response = 'Subscriber is ' + instance.username;
      console.log(response);
      if (instance && instance.preferences && instance.preferences.geo) {
        var lat = instance.preferences.geo.lat;
        var lon = instance.preferences.geo.lng;

        var openWeatherMap = Subscriber.app.dataSources.openweathermap;
        openWeatherMap.getweather
        (
          lat,
          lon,
          function(err, results){

            if (err) {
              cb(err);
            } else {
              console.log(results);
              cb(null, results);
            }

          }
        );

      } else {
        cb(null, {});
      }



    })
  };


  Subscriber.remoteMethod('getWeather', {
    accepts: [
      {arg: 'id', type: 'string', required: true}
    ],
    // mixing ':id' into the rest url allows $owner to be determined and used for access control
    http: {path: '/:id/weather', verb: 'get'},
    returns: {arg: 'weather', type: 'object'}
  });

};
<commit_msg>cleanup.<commit_after>module.exports = function(Subscriber) {

  // Google Maps API has a rate limit of 10 requests per second
  // Seems we need to enforce a lower rate to prevent errors
  var lookupGeo = require('function-rate-limit')(5, 1000, function() {
    var geoService = Subscriber.app.dataSources.geo;
    geoService.geocode.apply(geoService, arguments);
  });


  Subscriber.beforeRemote('prototype.updateAttributes', function(ctx, user, next) {
    var body = ctx.req.body;
    if (body                    &&
        body.preferences        &&
        body.preferences.street &&
        body.preferences.city   &&
        body.preferences.state ) {

      var loc = body.preferences;

      // geo code the address
      lookupGeo(loc.street, loc.city, loc.state,
        function(err, result) {
           if (result && result[0]) {
            body.preferences.geo = result[0];
            next();
          } else {
             //TODO: Need to find out how to handle this with better a UX experience
            next(new Error('could not find location'));
          }
        });

    } else {
      next();
    }

  });


  Subscriber.getWeather = function(subscriberId, cb){
    Subscriber.findById(subscriberId, function (err, instance) {

      if (err) {
        return cb(err);
      }

      if (instance && instance.preferences && instance.preferences.geo) {
        var lat = instance.preferences.geo.lat;
        var lon = instance.preferences.geo.lng;

        var openWeatherMap = Subscriber.app.dataSources.openweathermap;
        openWeatherMap.getweather
        (
          lat,
          lon,
          function(err, results){

            if (err) {
              cb(err);
            } else {
              console.log(results);
              cb(null, results);
            }

          }
        );

      } else {
        cb(null, {});
      }

    })
  };


  Subscriber.remoteMethod('getWeather', {
    accepts: [
      {arg: 'id', type: 'string', required: true}
    ],
    http: {path: '/:id/weather', verb: 'get'},
    returns: {arg: 'weather', type: 'object'}
  });

};

{'fraction_non_alphanumeric': 0.12472209871053802, 'fraction_numerical': 0.004001778568252557, 'mean_word_length': 1.9424460431654675, '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': '7559418', 'n_tokens_mistral': 1343, 'n_tokens_neox': 1268, 'n_words': 312}