Skip to content

Commit

Permalink
Remove zipcodes. Remove unnecessary scopedQuerySelector
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Vializ committed May 2, 2017
1 parent 0b70a90 commit 82e22af
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4,485 deletions.
69 changes: 18 additions & 51 deletions build-system/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var BBPromise = require('bluebird');
var app = require('express')();
var bacon = require('baconipsum');
var bodyParser = require('body-parser');
var csv = require('fast-csv');
var fs = BBPromise.promisifyAll(require('fs'));
var formidable = require('formidable');
var jsdom = require('jsdom');
Expand Down Expand Up @@ -230,60 +229,28 @@ app.use('/form/verify-search-json/post', function(req, res) {
if (fields.error === 'true') {
errors.push('You asked for an error, you get an error.');
}
const addressPromise = fields.city && fields.zip ?
verifyAddress(fields.city, fields.zip) : Promise.resolve(false);
addressPromise.then(function(found) {
if (!found) {
errors.push({name: 'city', message: 'City doesn\'t match zip'});
}

if (errors.length === 0) {
res.end(JSON.stringify({
results: [
{title: 'Result 1'},
{title: 'Result 2'},
{title: 'Result 3'},
],
}));
} else {
res.statusCode = 400;
res.end(JSON.stringify({errors: errors}));
}
}, function(error) {
res.statusCode = 500;
res.end({errors: [error]});
});
});
});

const ZIP_PATH = 'examples/form/zipcodes.csv';
function verifyAddress(city, zip) {
return new Promise(function(resolve, reject) {
if (!city || !zip) {
resolve(false);
if (fields.city !== 'Mountain View' || fields.zip !== '94043') {
errors.push({
name: 'city',
message: 'City doesn\'t match zip (Mountain View and 94043)',
});
}

const fileStream = fs.createReadStream(ZIP_PATH);
const csvStream = csv({headers: true})
.on('data', function(row) {
if (equalsIgnoreCase(row.City, city) &&
equalsIgnoreCase(row.Zipcode, zip)) {
resolve(true);
fileStream.destroy();
}
})
.on('end', function() {
resolve(false);
})
.on('error', reject);
if (errors.length === 0) {
res.end(JSON.stringify({
results: [
{title: 'Result 1'},
{title: 'Result 2'},
{title: 'Result 3'},
],
}));
} else {
res.statusCode = 400;
res.end(JSON.stringify({errors: errors}));
}

fileStream.pipe(csvStream);
});
}

function equalsIgnoreCase(a, b) {
return a.toUpperCase() === b.toUpperCase();
}
});

app.use('/share-tracking/get-outgoing-fragment', function(req, res) {
res.setHeader('AMP-Access-Control-Allow-Source-Origin',
Expand Down
Loading

0 comments on commit 82e22af

Please sign in to comment.