This repository has been archived by the owner on Nov 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
143 lines (116 loc) · 3.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var https = require('https');
var path = require('path');
var proxyAgent = require('https-proxy-agent');
var querystring = require('querystring');
var url = require('url');
var name, version;
require('fs').readFile(path.join(__dirname, 'package.json'), function(err, data) {
if (err) throw err;
data = JSON.parse(data);
name = data.name + '.js';
version = data.version;
});
var ExchangeRates = function ExchangeRates(options) {
options = options || {};
this.api_key = options.api_key;
this.proxy = options.proxy || process.env.http_proxy;
if (!this.api_key) {
throw 'No API key specified';
}
};
ExchangeRates.prototype._makeProxy = function(url) {
return new proxyAgent(url)
}
ExchangeRates.prototype._getResponse = function(endpoint, callback) {
var options = {
hostname: 'www.oanda.com',
headers: {
Authorization: 'Bearer ' + this.api_key,
'User-Agent': name + '/' + version
},
path: '/rates/api/v1/' + endpoint
};
if (this.proxy) {
options.agent = this._makeProxy(this.proxy);
};
https.get(options, function(httpRes) {
var apiRes = {
statusCode: httpRes.statusCode
};
var data = '';
// buffer response data until it has arrived completely
httpRes.on('data', function(chunk) {
// chunk either a string or a Buffer
if (Buffer.isBuffer(chunk)) {
chunk = chunk.toString();
}
// now chunk always a string
data += chunk;
});
// entire response data has arrived and ready to be parsed
httpRes.on('end', function() {
apiRes.raw = data;
try {
apiRes.data = JSON.parse(data);
apiRes.success = (httpRes.statusCode === 200);
// as a convenience, set the code and message in the response wrapper level
if (!apiRes.success) {
apiRes.errorCode = apiRes.data.code;
apiRes.errorMessage = apiRes.data.message;
}
} catch (e) {
apiRes.errorMessage = 'Unable to parse JSON data: ' + e.message;
apiRes.success = false;
}
if (callback != null) {
callback(apiRes);
}
});
httpRes.on('error', function(e) {
if (callback != null) {
callback({
errorMessage: 'Problem with response: ' + e.message,
success: false
});
}
});
});
};
ExchangeRates.prototype.getCurrencies = function(data_set, callback) {
var endpoint = 'currencies.json';
if (typeof(data_set) === 'function') {
callback = data_set;
} else if (data_set !== undefined) {
endpoint += '?' + querystring.stringify({'data_set':data_set});
}
this._getResponse(endpoint, function(res) {
if (res.success) {
var currencies = {};
// rearrange the currencies data from an array to a hash for easier use
for (var i = 0; i < res.data.currencies.length; i++) {
var currency = res.data.currencies[i];
currencies[currency.code] = currency.description;
}
res.data = currencies;
}
if (callback != null) {
callback(res);
}
});
};
ExchangeRates.prototype.getRemainingQuotes = function(callback) {
this._getResponse('remaining_quotes.json', callback);
};
ExchangeRates.prototype.getRates = function(options, callback) {
var hasOptions = typeof(options) !== 'string';
var endpoint = 'rates/' + (hasOptions ? options.base : options) + '.json';
if (hasOptions) {
delete options.base;
var query = querystring.stringify(options);
if (query) {
endpoint += '?' + query;
}
}
this._getResponse(endpoint, callback);
};
module.exports = ExchangeRates;