<commit_before>NSB.API = new function() {
  /*
   * Generates the URL to retrieve results for a given parcel
   */
  this.getSurveyURL = function() {
    return NSB.settings.api.baseurl + "/surveys/" + NSB.settings.surveyid;
  };
  
  this.getParcelDataURL = function(parcel_id) {
    return NSB.settings.api.baseurl + '/surveys/' + NSB.settings.surveyid + '/parcels/' + parcel_id + '/responses';
  };
  
  this.getGeoPointInfoURL = function(lat, lng) {
    return NSB.settings.api.geo + '/parcels/parcel?lat=' + lat + '&lng=' + lng;
  };
  
  this.getGeoBoundsObjectsURL = function(southwest, northeast) {
    return NSB.settings.api.geo + '/parcels/bounds?lowerleft=' + southwest.lat + "," + southwest.lng + "&topright=" + northeast.lat + "," + northeast.lng;
  };
  
  // Given a Leaflet latlng object, return a JSON object that describes the 
  // parcel.
  this.getObjectDataAtPoint = function(latlng, callback) {
    console.log("Waiting for PostGIS data");
    var lat = latlng.lat;
    var lng = latlng.lng; 
    
    var url = this.getGeoPointInfoURL(lat, lng);
    
    $.getJSON(url, function(data){
      // Process the results. Strip whitespace. Convert the polygon to geoJSON
      // TODO: This will need to be genercized (id column, addres, etc.)
      console.log("Got PostGIS data");
      callback(NSB.API.parseObjectData(data));
    }, this);
  };
  
  // Deal with the formatting of the geodata API.
  // In the future, this will be more genericized. 
  // parcel_id => object_id
  // address => object_location
  this.parseObjectData = function(data) {
    return {
      parcelId: data.parcelId, 
      address: data.address,
      polygon: data.polygon,
      centroid: data.centroid
    };
  };
  
  // Take an address string. 
  // Add "Detroit" to the end.
  // Return the first result as a lat-lng for convenience.
  // Or Null if Bing is being a jerk / we're dumb. 
  this.codeAddress = function(address, callback) {
    console.log("Coding an address");
    console.log(address);
    var detroitAddress = address + " Detroit, MI"; // for ease of geocoding
    var geocodeEndpoint = "http://dev.virtualearth.net/REST/v1/Locations/" + detroitAddress + "?o=json&key=" + NSB.settings.bing_key + "&jsonp=?";

    $.getJSON(geocodeEndpoint, function(data){
      if(data.resourceSets.length > 0){
        var point = data.resourceSets[0].resources[0].point;
        var latlng = new L.LatLng(point.coordinates[0], point.coordinates[1]);
        callback(latlng);
      };
    });    
  };
  
  // Take a map bounds object
  // Find the objects in the bounds
  // Feed those objects to the callback
  this.getResponsesInBounds = function(bounds, callback) {
    var southwest = bounds.getSouthWest();
    var northeast = bounds.getNorthEast();
    
    // Given the bounds, generate a URL to ge the responses from the API.
    serializedBounds = southwest.lat + "," + southwest.lng + "," + northeast.lat + "," + northeast.lng;
    var url = NSB.API.getSurveyURL() + "/responses/in/" + serializedBounds;

    // Give the callback the responses.
    $.getJSON(url, function(data){
      if(data.responses) {
        callback(data.responses);
      };
    });
  };
  
  // Take a map bounds object
  // Find the parcels in the bounds
  // Feed those objects to the callback
  this.getObjectsInBounds = function(bounds, callback) {
    bufferedBounds = addBuffer(bounds);
    var southwest = bufferedBounds.getSouthWest();
    var northeast = bufferedBounds.getNorthEast();
    
    // Given the bounds, generate a URL to ge the responses from the API.
    var url = this.getGeoBoundsObjectsURL(southwest, northeast);
    console.log(url);

    // Give the callback the responses.
    $.getJSON(url, function(data){
      if(data) {
        callback(data);
      };
    });
  };
    
  // Add a 100% buffer to a bounds object.
  // Makes parcels render faster when the map is moved
  var addBuffer = function(bounds) {    
    sw = bounds.getSouthWest();
    ne = bounds.getNorthEast();
    
    lngDiff = ne.lng - sw.lng;
    latDiff = ne.lat - sw.lat;
    
    lngMod = lngDiff;
    latMod = latDiff;
    
    var newSW = new L.LatLng(sw.lat - latMod, sw.lng - lngMod);
    var newNE = new L.LatLng(ne.lat + latMod, ne.lng + lngMod);
    
    return new L.LatLngBounds(newSW, newNE);
  };
  
};<commit_msg>Get forms in API<commit_after>NSB.API = new function() {
  /*
   * Generates the URL to retrieve results for a given parcel
   */
  this.getSurveyURL = function() {
    return NSB.settings.api.baseurl + "/surveys/" + NSB.settings.surveyid;
  };
  
  this.getParcelDataURL = function(parcel_id) {
    return NSB.settings.api.baseurl + '/surveys/' + NSB.settings.surveyid + '/parcels/' + parcel_id + '/responses';
  };
  
  this.getGeoPointInfoURL = function(lat, lng) {
    return NSB.settings.api.geo + '/parcels/parcel?lat=' + lat + '&lng=' + lng;
  };
  
  this.getGeoBoundsObjectsURL = function(southwest, northeast) {
    return NSB.settings.api.geo + '/parcels/bounds?lowerleft=' + southwest.lat + "," + southwest.lng + "&topright=" + northeast.lat + "," + northeast.lng;
  };
  
  this.getForm = function(callback) {
    url = this.getSurveyURL() + "/forms";
    $.getJSON(url, function(data){
      
      // Get only the mobile forms
      var mobileForms = _.filter(data.forms, function(form) {
        if (_.has(form, 'type')) {
          if (form.type === 'mobile'){
            return true;
          }
        }
        return false; 
      });
      
      // Sort by date
      var sortedForms = _.sortBy(mobileForms, "created");
      
      // Get only the most recent
      callback(_.last(sortedForms));
    });
  };
  
  
  // Given a Leaflet latlng object, return a JSON object that describes the 
  // parcel.
  this.getObjectDataAtPoint = function(latlng, callback) {
    console.log("Waiting for PostGIS data");
    var lat = latlng.lat;
    var lng = latlng.lng; 
    
    var url = this.getGeoPointInfoURL(lat, lng);
    
    $.getJSON(url, function(data){
      // Process the results. Strip whitespace. Convert the polygon to geoJSON
      // TODO: This will need to be genercized (id column, addres, etc.)
      console.log("Got PostGIS data");
      callback(NSB.API.parseObjectData(data));
    }, this);
  };
  
  // Deal with the formatting of the geodata API.
  // In the future, this will be more genericized. 
  // parcel_id => object_id
  // address => object_location
  this.parseObjectData = function(data) {
    return {
      parcelId: data.parcelId, 
      address: data.address,
      polygon: data.polygon,
      centroid: data.centroid
    };
  };
  
  // Take an address string. 
  // Add "Detroit" to the end.
  // Return the first result as a lat-lng for convenience.
  // Or Null if Bing is being a jerk / we're dumb. 
  this.codeAddress = function(address, callback) {
    console.log("Coding an address");
    console.log(address);
    var detroitAddress = address + " Detroit, MI"; // for ease of geocoding
    var geocodeEndpoint = "http://dev.virtualearth.net/REST/v1/Locations/" + detroitAddress + "?o=json&key=" + NSB.settings.bing_key + "&jsonp=?";

    $.getJSON(geocodeEndpoint, function(data){
      if(data.resourceSets.length > 0){
        var point = data.resourceSets[0].resources[0].point;
        var latlng = new L.LatLng(point.coordinates[0], point.coordinates[1]);
        callback(latlng);
      };
    });    
  };
  
  // Take a map bounds object
  // Find the objects in the bounds
  // Feed those objects to the callback
  this.getResponsesInBounds = function(bounds, callback) {
    var southwest = bounds.getSouthWest();
    var northeast = bounds.getNorthEast();
    
    // Given the bounds, generate a URL to ge the responses from the API.
    serializedBounds = southwest.lat + "," + southwest.lng + "," + northeast.lat + "," + northeast.lng;
    var url = NSB.API.getSurveyURL() + "/responses/in/" + serializedBounds;

    // Give the callback the responses.
    $.getJSON(url, function(data){
      if(data.responses) {
        callback(data.responses);
      };
    });
  };
  
  // Take a map bounds object
  // Find the parcels in the bounds
  // Feed those objects to the callback
  this.getObjectsInBounds = function(bounds, callback) {
    bufferedBounds = addBuffer(bounds);
    var southwest = bufferedBounds.getSouthWest();
    var northeast = bufferedBounds.getNorthEast();
    
    // Given the bounds, generate a URL to ge the responses from the API.
    var url = this.getGeoBoundsObjectsURL(southwest, northeast);
    console.log(url);

    // Give the callback the responses.
    $.getJSON(url, function(data){
      if(data) {
        callback(data);
      };
    });
  };
    
  // Add a 100% buffer to a bounds object.
  // Makes parcels render faster when the map is moved
  var addBuffer = function(bounds) {    
    sw = bounds.getSouthWest();
    ne = bounds.getNorthEast();
    
    lngDiff = ne.lng - sw.lng;
    latDiff = ne.lat - sw.lat;
    
    lngMod = lngDiff;
    latMod = latDiff;
    
    var newSW = new L.LatLng(sw.lat - latMod, sw.lng - lngMod);
    var newNE = new L.LatLng(ne.lat + latMod, ne.lng + lngMod);
    
    return new L.LatLngBounds(newSW, newNE);
  };
  
};
{'fraction_non_alphanumeric': 0.12947688300412416, 'fraction_numerical': 0.0019535489472541786, 'mean_word_length': 2.6308116627265563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1480961', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2949, 'n_words': 774}