Skip to content

Commit

Permalink
generic, crossplatform provider config
Browse files Browse the repository at this point in the history
  • Loading branch information
morganherlocker committed Apr 17, 2019
1 parent b23ec46 commit ce03e43
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 94 deletions.
2 changes: 0 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,6 @@ <h2 class="subtitle is-4">Flows</h2>
<script>
mapboxgl.accessToken =
"pk.eyJ1IjoibW9yZ2FuaGVybG9ja2VyIiwiYSI6Ii1zLU4xOWMifQ.FubD68OEerk74AYCLduMZQ";


</script>
</body>
</html>
15 changes: 0 additions & 15 deletions src/config.js

This file was deleted.

128 changes: 60 additions & 68 deletions src/providers/bird.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,80 @@
const fs = require("fs");
const request = require("request");
const config = require("../config");
const config = require("./../../config.json");

const PROVIDER = "bird";
var provider = config.providers.bird;

function trips(stream, start, stop) {
config(PROVIDER, (err, conf) => {
if (err) throw err;

var opts = {
url: conf.trips + "?start_time=" + start + "&end_time=" + stop,
headers: {
"Content-Type": "application/json",
"APP-Version": "3.0.0",
Authorization: "Bird " + conf.token
}
};

scan(opts, () => {
stream.end();
});
var opts = {
url: provider.trips + "?start_time=" + start + "&end_time=" + stop,
headers: {
"Content-Type": "application/json",
"APP-Version": "3.0.0",
Authorization: "Bird " + provider.token
}
};

// recursive scan across
function scan(opts, cb) {
request.get(opts, (err, res, body) => {
if (err) throw err;
scan(opts, () => {
stream.end();
});

var data = JSON.parse(body);
// recursive scan across
function scan(opts, cb) {
request.get(opts, (err, res, body) => {
if (err) throw err;

// write any returned trips to stream
data.data.trips.forEach(trip => {
stream.write(trip);
});
var data = JSON.parse(body);

// continue scan if another page is present
if (data.links.next) {
opts.url = data.links.next;
scan(opts, cb);
} else {
cb();
}
// write any returned trips to stream
data.data.trips.forEach(trip => {
stream.write(trip);
});
}
});
}

function changes(stream, start, stop) {
config(PROVIDER, (err, conf) => {
if (err) throw err;

var opts = {
url: conf.changes + "?start_time=" + start + "&end_time=" + stop,
headers: {
"Content-Type": "application/json",
"APP-Version": "3.0.0",
Authorization: "Bird " + conf.token
// continue scan if another page is present
if (data.links.next) {
opts.url = data.links.next;
scan(opts, cb);
} else {
cb();
}
};

scan(opts, () => {
stream.end();
});
}
}

function changes(stream, start, stop) {
var opts = {
url: provider.status_changes + "?start_time=" + start + "&end_time=" + stop,
headers: {
"Content-Type": "application/json",
"APP-Version": "3.0.0",
Authorization: "Bird " + provider.token
}
};

// recursive scan across
function scan(opts, cb) {
request.get(opts, (err, res, body) => {
if (err) throw err;
scan(opts, () => {
stream.end();
});

var data = JSON.parse(body);
// write any returned changes to stream
data.data.status_changes.forEach(change => {
stream.write(change);
});
// recursive scan across
function scan(opts, cb) {
request.get(opts, (err, res, body) => {
if (err) throw err;

// continue scan if another page is present
if (data.links.next) {
opts.url = data.links.next;
scan(opts, cb);
} else {
cb();
}
var data = JSON.parse(body);
// write any returned changes to stream
data.data.status_changes.forEach(change => {
stream.write(change);
});
}
});

// continue scan if another page is present
if (data.links.next) {
opts.url = data.links.next;
scan(opts, cb);
} else {
cb();
}
});
}
}

module.exports.trips = trips;
Expand Down
26 changes: 17 additions & 9 deletions test/config.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
const test = require("tap").test;
const config = require("../src/config.js");
const config = require("../config.json");

test("config - bird", t => {
config("bird", (err, conf) => {
t.notOk(err, "loaded config without error");
t.ok(conf, "loaded config");
t.ok(conf.token, "has token");
t.ok(conf.trips, "has trips url");
t.ok(conf.changes, "has changes url");
test("config", t => {
t.equal(config.boundary.length, 4, "has valid boundary bbox");

t.end();
Object.keys(config.providers).forEach(provider => {
if (config.providers[provider].enabled) {
t.ok(
config.providers[provider].trips.length,
provider + " has trips url"
);
t.ok(
config.providers[provider].status_changes.length,
provider + " has status_changes url"
);
t.ok(config.providers[provider].token.length, provider + " has token");
}
});

t.end();
});

0 comments on commit ce03e43

Please sign in to comment.