-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
240 lines (200 loc) · 7.69 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*
* Copyright © 2017 Polyledger. All rights reserved.
*/
var request = require('request-promise');
var constants = require('./lib/constants');
var Coinage = function () {
function Coinage() {
_classCallCheck(this, Coinage);
}
_createClass(Coinage, null, [{
key: 'getCoins',
/**
* @function getCoins
* @returns {array}
* Returns an array of the names of coins.
*/
value: function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var response;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return request({
method: 'GET',
url: 'https://www.coincap.io/coins/'
});
case 2:
response = _context.sent;
response = JSON.parse(response);
return _context.abrupt('return', response);
case 5:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function getCoins() {
return _ref.apply(this, arguments);
}
return getCoins;
}()
/**
* @function getHistory
* @param {string} coin
* @param {string} type
* @param {string} period
* @returns {object|array}
* Gets the type of history for a coin during a period. If no type is specified,
* then returns an object with market cap, price, and volume history arrays. If
* a type is specified, an array is returned.
*/
}, {
key: 'getHistory',
value: function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(coin) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var period = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var options, response;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (coin) {
_context2.next = 2;
break;
}
throw new Error("Expected argument 'coin' is missing.");
case 2:
options = {
base: 'https://www.coincap.io/',
path: ['history', period, coin]
};
_context2.next = 5;
return request({
method: 'GET',
url: Coinage.appendUrlPath(options)
});
case 5:
response = _context2.sent;
response = Coinage.pruneResponse(JSON.parse(response), type);
return _context2.abrupt('return', response);
case 8:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function getHistory(_x3) {
return _ref2.apply(this, arguments);
}
return getHistory;
}()
/**
* @function getTickers
* @param {string[]} coins
* @param {string} currency
* @param {number} limit
* @returns {array}
* Gets ticker data for the top 100 (default limit) coins and filters results
* based on the 'coins' parameter. The currency defaults to USD.
*/
}, {
key: 'getTickers',
value: function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
var coins = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var currency = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'USD';
var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : constants.LIMIT;
var options, response;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
options = {
url: 'https://api.coinmarketcap.com/v1/ticker/',
params: ['convert=' + currency, 'limit=' + limit]
};
_context3.next = 3;
return request({
method: 'GET',
url: Coinage.appendUrlParams(options)
});
case 3:
response = _context3.sent;
response = JSON.parse(response);
if (!coins.length) {
_context3.next = 7;
break;
}
return _context3.abrupt('return', response.filter(function (element) {
return coins.includes(element.symbol);
}));
case 7:
return _context3.abrupt('return', response);
case 8:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function getTickers() {
return _ref3.apply(this, arguments);
}
return getTickers;
}()
/**
* @function pruneResponse
* @param {array|object} response
* @param {string} key
* @returns {array|object}
* Prunes the response object to a single array if a type was specified.
*/
}, {
key: 'pruneResponse',
value: function pruneResponse(response, key) {
if (!key) return response;else if (key in response) return response[key];
throw new Error('There is no key \'' + key + '\' in the response object.');
}
/**
* @function appendUrlParams
* @param {object} options
* @returns {string}
* Formats the URL given the components of its parameters.
*/
}, {
key: 'appendUrlParams',
value: function appendUrlParams(options) {
var url = options.url;
var params = options.params.join('&');
params = '?' + params;
return url + params;
}
/**
* @function appendUrlPath
* @param {object} options
* @returns {string}
* Formats the URL given the components of its path.
*/
}, {
key: 'appendUrlPath',
value: function appendUrlPath(options) {
var url = options.base;
var path = options.path.filter(function (element) {
return !!element;
});
return url + path.join('/');
}
}]);
return Coinage;
}();
module.exports = Coinage;