Skip to content

Instantly share code, notes, and snippets.

@sfpgmr sfpgmr/app.js
Created Sep 27, 2014

Embed
What would you like to do?
var fs = require("fs");
var https = require("https");
var q = require("q");
var d3 = require("d3");
var outputDir = "../html/";
var apiKey = null;
var apiUrl = "https://api.tokyometroapp.jp/api/v2/";
q.nfcall(fs.readFile, "apikey.json")
.then(function (key)
{
apiKey = JSON.parse(key).apiKey;
var url = apiUrl + "datapoints?rdf:type=odpt:Train&acl:consumerKey=" + apiKey;
return callMetroAPI(url);
})
.then(function (d) {
// JSONデータの読み込みができた
console.log(d);
})
.catch(function (err) {
// エラー処理
console.log("エラーが発生しました。" + err.toString());
});
// 東京MetroAPIの呼び出し
function callMetroAPI(url) {
var d = q.defer();
https.get(url, function (res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function (res) {
ret = JSON.parse(body);
d.resolve(ret);
});
}).on('error', function (e) {
d.reject(e);
});
return d.promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.