<commit_before>var _ = require('lodash');
var request = require('request');

var secrets = require('./secret/secrets');
var stations = require('./resources/stations');
var utils = require('./lib/utils');

var APP_ID = secrets.applicationId;
var AlexaSkill = require('./lib/AlexaSkill');

var wmataReq = request.defaults({
  baseUrl: 'https://api.wmata.com',
  qs: secrets.apiKey
});

var getWmataResponse = function(endpoint, response, callback) {
  wmataReq(endpoint, function(error, res, body) {
    if (!error && res.statusCode === 200) {
      callback(JSON.parse(body));
    } else {
      console.error(endpoint.concat(': Error with WMATA'), error);
      response.tell('There was an error making the WMATA request. Please try again later.');
    }
  });
};

var MetroTransit = function () {
  AlexaSkill.call(this, APP_ID);
};

MetroTransit.prototype = Object.create(AlexaSkill.prototype);
MetroTransit.prototype.constructor = MetroTransit;

MetroTransit.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
  console.log('MetroTransit onSessionStarted requestId: ' + sessionStartedRequest.requestId +
       ', sessionId: ' + session.sessionId);
  // any initialization logic goes here
};

MetroTransit.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
  console.log('MetroTransit onLaunch requestId: ' + launchRequest.requestId + ', sessionId: ' + session.sessionId);
  var speechOutput = 'Welcome to the DC Metro App! How can I help you?';
  var repromptText = 'How can I help you?';
  response.ask(speechOutput, repromptText);
};

MetroTransit.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
  console.log('MetroTransit onSessionEnded requestId: ' + sessionEndedRequest.requestId +
      ', sessionId: ' + session.sessionId);
  // any cleanup logic goes here
};

MetroTransit.prototype.intentHandlers = {

  GetStation: function (intent, session, response) {
    var stationName = utils.changeStationName(intent.slots.station.value, 'correction');
    console.info('Station Name: ' + stationName);

    if (_.has(stations, stationName)) {
      var stationCode = stations[stationName].Code;

      getWmataResponse('/StationPrediction.svc/json/GetPrediction/' + stationCode, response, function(body) {
        var trainArrivals = _.reduce(body.Trains, function(result, train) {
          if (train.DestinationName === 'Train' || train.DestinationName == 'No Passenger') return result;

          var arrivals = result[train.DestinationName] || [];
          arrivals.push(train.Min);
          result[utils.changeStationName(train.DestinationName, 'abbreviation').toLowerCase()] = arrivals;
          return result;
        }, {});

        console.info('Train Arrivals', trainArrivals);
        if (_.size(trainArrivals) === 0) {
          response.tell('Sorry, there are no trains running at this time.');
        } else {
          var destinationNeededText = 'Are you going to ' + utils.joinListConjuction(_.keys(trainArrivals), ', ', ' or ');
          session.attributes = trainArrivals;
          response.ask(destinationNeededText, destinationNeededText);
        }
      });
    }
  },

  GetDestinationStation: function (intent, session, response) {
    var sessionAttributes = session.attributes;
    var destinationStationName = intent.slots.destinationStation.value;
    console.info('Station Name: ' + destinationStationName);

    if (_.has(sessionAttributes, destinationStationName)) {
      var arrivalTimes = sessionAttributes[destinationStationName];
      var respText = 'The next ' + (arrivalTimes.length === 1 ?
        'train' : arrivalTimes.length + ' trains') + ' heading to ' + destinationStationName +
        (arrivalTimes.length === 1 ? ' arrives' : ' arrive') + ' in ' + utils.joinListConjuction(arrivalTimes, ', ', ' and ') + ' minutes.';
      response.tell(respText);
    } else {
      console.error('Could not find destination station name: ' + destinationStationName);
      response.tell('Sorry, I couldn\'t find the destination station ' + destinationStationName + '.');
    }
  }

  GetServiceAdvisories = function(intent, session, response) {
    wmataReq('/Incidents.svc/json/Incidents', function (error, wmataResponse, body) {
      if (!error && wmataResponse.statusCode === 200) {
        var incidents = JSON.parse(body).Incidents;
        if (!incidents) {
          console.error('GetServiceAdvisories: Error parsing incidents from WMATA.');
          response.tell('Hmm... I am having trouble getting the information. Try again in a few minutes.');
          return;
        }
        var incidentList = _.reduce(incidents, function (descriptions, incident) {
          return incident.Description ? descriptions.push(incident.Description) : descriptions;
        }, []);
        response.tell(incidentList.join('\n'));
      } else {
        console.error('GetServiceAdvisories: Bad request', error);
        response.tell('Sorry, an error has occured. Please try again.');
      }
    });
  };
};

// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {
    // Create an instance of the MetroTransit skill.
    var helloWorld = new MetroTransit();<commit_msg>Use getWmataResponse in GetServiceAdvisories<commit_after>var _ = require('lodash');
var request = require('request');

var secrets = require('./secret/secrets');
var stations = require('./resources/stations');
var utils = require('./lib/utils');

var APP_ID = secrets.applicationId;
var AlexaSkill = require('./lib/AlexaSkill');

var wmataReq = request.defaults({
  baseUrl: 'https://api.wmata.com',
  qs: secrets.apiKey
});

var getWmataResponse = function(endpoint, response, callback) {
  wmataReq(endpoint, function(error, res, body) {
    if (!error && res.statusCode === 200) {
      callback(JSON.parse(body));
    } else {
      console.error(endpoint.concat(': Error with WMATA'), error);
      response.tell('There was an error making the WMATA request. Please try again later.');
    }
  });
};

var MetroTransit = function () {
  AlexaSkill.call(this, APP_ID);
};

MetroTransit.prototype = Object.create(AlexaSkill.prototype);
MetroTransit.prototype.constructor = MetroTransit;

MetroTransit.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
  console.log('MetroTransit onSessionStarted requestId: ' + sessionStartedRequest.requestId +
       ', sessionId: ' + session.sessionId);
  // any initialization logic goes here
};

MetroTransit.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
  console.log('MetroTransit onLaunch requestId: ' + launchRequest.requestId + ', sessionId: ' + session.sessionId);
  var speechOutput = 'Welcome to the DC Metro App! How can I help you?';
  var repromptText = 'How can I help you?';
  response.ask(speechOutput, repromptText);
};

MetroTransit.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
  console.log('MetroTransit onSessionEnded requestId: ' + sessionEndedRequest.requestId +
      ', sessionId: ' + session.sessionId);
  // any cleanup logic goes here
};

MetroTransit.prototype.intentHandlers = {

  GetStation: function (intent, session, response) {
    var stationName = utils.changeStationName(intent.slots.station.value, 'correction');
    console.info('Station Name: ' + stationName);

    if (_.has(stations, stationName)) {
      var stationCode = stations[stationName].Code;

      getWmataResponse('/StationPrediction.svc/json/GetPrediction/' + stationCode, response, function(body) {
        var trainArrivals = _.reduce(body.Trains, function(result, train) {
          if (train.DestinationName === 'Train' || train.DestinationName == 'No Passenger') return result;

          var arrivals = result[train.DestinationName] || [];
          arrivals.push(train.Min);
          result[utils.changeStationName(train.DestinationName, 'abbreviation').toLowerCase()] = arrivals;
          return result;
        }, {});

        console.info('Train Arrivals', trainArrivals);
        if (_.size(trainArrivals) === 0) {
          response.tell('Sorry, there are no trains running at this time.');
        } else {
          var destinationNeededText = 'Are you going to ' + utils.joinListConjuction(_.keys(trainArrivals), ', ', ' or ');
          session.attributes = trainArrivals;
          response.ask(destinationNeededText, destinationNeededText);
        }
      });
    }
  },

  GetDestinationStation: function (intent, session, response) {
    var sessionAttributes = session.attributes;
    var destinationStationName = intent.slots.destinationStation.value;
    console.info('Station Name: ' + destinationStationName);

    if (_.has(sessionAttributes, destinationStationName)) {
      var arrivalTimes = sessionAttributes[destinationStationName];
      var respText = 'The next ' + (arrivalTimes.length === 1 ?
        'train' : arrivalTimes.length + ' trains') + ' heading to ' + destinationStationName +
        (arrivalTimes.length === 1 ? ' arrives' : ' arrive') + ' in ' + utils.joinListConjuction(arrivalTimes, ', ', ' and ') + ' minutes.';
      response.tell(respText);
    } else {
      console.error('Could not find destination station name: ' + destinationStationName);
      response.tell('Sorry, I couldn\'t find the destination station ' + destinationStationName + '.');
    }
  }

  GetServiceAdvisories = function(intent, session, response) {
    getWmataResponse('/Incidents.svc/json/Incidents', response, function(body) {
      var incidents = body.Incidents;
      if (!incidents) {
        console.error('GetServiceAdvisories: Error parsing incidents from WMATA.');
        response.tell('Hmm... I am having trouble getting the information. Try again in a few minutes.');
        return;
      }
      var incidentList = _.reduce(incidents, function (descriptions, incident) {
        return incident.Description ? descriptions.push(incident.Description) : descriptions;
      }, []);
      response.tell(incidentList.join('\n'));
    });
  };
};

// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {
    // Create an instance of the MetroTransit skill.
    var helloWorld = new MetroTransit();
{'fraction_non_alphanumeric': 0.12763068567549218, 'fraction_numerical': 0.0014547570555717194, 'mean_word_length': 3.7367937528709234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3531927', 'n_tokens_mistral': 2954, 'n_tokens_neox': 2899, 'n_words': 679}