-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
106 lines (106 loc) · 2.5 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
const https = require('https')
/**
* @description Yandex Raspisanie Node API
* @see https://tech.yandex.ru/rasp/doc/reference/schedule-point-point-docpage/
*/
module.exports = class Raspisanie {
/**
* @private
* @returns {String}
*/
get _options() {
const self = this
return {
host: 'api.rasp.yandex.net',
method: 'GET',
agent: false,
get path() {
let path = '/v1.0/search/'
path += `?apikey=${self.RASP_API_KEY}`
path += `&format=${self.format}`
path += `&from=${self.from}`
path += `&to=${self.to}`
path += `&lang=${self.lang}`
if (self.date) path += `&date=${self.date}`
if (self.transportTypes)
path += `&transport_types${self.transportTypes}`
if (self.system) path += `&system${self.system}`
if (self.path) path += `&path${self.path}`
return path
}
}
}
/**
* @public
* @param {object} obj
* @returns {Promise}
*/
request({ from, to, path, system, transportTypes, date, lang = 'ru', format = 'json' }) {
this.from = from
this.to = to
this.lang = lang
this.format = format
this.date = date
this.transportTypes = transportTypes
this.system = system
this.path = path
return new Promise((resolve, reject) => {
https
.request(this._options)
.on('error', error => {
return reject(error)
})
.on('response', async res => {
try {
const data = await this._processResponse(res)
return resolve(data)
} catch (error) {
return reject(error)
}
})
.end()
})
}
/**
* @constructor
* @param {String} RASP_API_KEY
*/
constructor(RASP_API_KEY) {
this.RASP_API_KEY = RASP_API_KEY
}
/**
* @private
* @param {*} res
* @returns {Promise}
*/
_processResponse(res) {
return new Promise((resolve, reject) => {
let data = ''
res
.on('error', onError)
.on('data', onData)
.on('end', onEnd)
/**
* @param {Error} error
*/
function onError(error) {
reject(error)
}
/**
* @param {Buffer} chunk
*/
function onData(chunk) {
data += chunk
}
function onEnd() {
try {
data = JSON.parse(data.toString('utf-8'))
if (data.error) reject(data.error)
else resolve(data)
} catch (error) {
reject(error)
}
}
})
}
}