-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathschool.js
143 lines (132 loc) ยท 4.65 KB
/
school.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
/**
* node-school-kr Module
*
* school.js
*
* Github : https://github.com/leegeunhyeok/node-school-kr
* NPM : https://www.npmjs.com/package/node-school-kr
*
* @description ์ ๊ตญ ๊ธ์, ํ์ฌ์ผ์ API
* @author Leegeunhyeok
* @license MIT
* @version 2.0.0
*
*/
const { DATA, TYPE, REGION } = require('./data/data.js')
const Meal = require('./src/meal.js')
const Calendar = require('./src/calendar.js')
class School {
/**
* @static TYPE Symbols
*/
static get Type () {
return TYPE
}
/**
* @static REGION Symbols
*/
static get Region () {
return REGION
}
/**
* @constructor School ์์ฑ์
*/
constructor () {
this._DATA = DATA // ๋ฐ์ดํฐ ์ ์ ๊ฐ์ฒด
this._mealUrl = DATA.mealUrl // ๊ธ์ URL
this._calendarUrl = DATA.calendarUrl // ํ์ฌ์ผ์ URL
this._meal = new Meal() // ๊ธ์ ์ธ์คํด์ค ์์ฑ
this._calendar = new Calendar() // ํ์ฌ์ผ์ ์ธ์คํด์ค ์์ฑ
}
/**
* @description ํด๋น ๊ต์ก๊ธฐ๊ด์ผ๋ก ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํ ํฉ๋๋ค.
* @doc type๊ณผ region์ data ํด๋์ data.js์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค. ์ฌ์ฉ ๋ฐฉ๋ฒ์ sample ํด๋์ sample.js ์ฐธ๊ณ
* @param {Symbol} type ๊ต์ก๊ธฐ๊ด ์ ํ(๋ณ์ค์ ์น์, ์ด, ์ค, ๊ณ )
* @param {Symbol} region ๊ต์ก์ฒญ ์ง์ญ
* @param {string} schoolCode ํ๊ต ๊ณ ์ ๋ฒํธ (https://www.meatwatch.go.kr/biz/bm/sel/schoolListPopup.do)
*/
init (type, region, schoolCode) {
/* ์ด๊ธฐํ ์ฌ๋ถ */
if (this._initialized) {
throw new Error(`๋ณธ ์ธ์คํด์ค๋ [${this._schoolCode}]๋ก ์ด๋ฏธ ์ด๊ธฐํ ๋์ด์์ต๋๋ค.`)
} else if (type && region && schoolCode) {
this._eduType = this._DATA.EDUTYPE[type]
this._region = this._DATA.REGION[region]
this._schoolCode = schoolCode
this._initialized = true
} else {
throw new Error('๊ต์ก๊ธฐ๊ด ํ์
, ์ง์ญ, ํ๊ต ์ฝ๋๋ ํ์ ๋ฐ์ดํฐ ์
๋๋ค.')
}
}
/**
* @description ํด๋น ๊ต์ก๊ธฐ๊ด์ผ๋ก ์ธ์คํด์ค๋ฅผ ์ฌ์ค์ ํฉ๋๋ค.
* @doc type๊ณผ region์ data ํด๋์ data.js์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค. ์ฌ์ฉ ๋ฐฉ๋ฒ์ sample ํด๋์ sample.js ์ฐธ๊ณ
* @param {Symbol} type ๊ต์ก๊ธฐ๊ด ์ ํ (๋ณ์ค์ ์น์, ์ด, ์ค, ๊ณ )
* @param {Symbol} region ๊ต์ก์ฒญ ์ง์ญ
* @param {string} schoolCode ํ๊ต ๊ณ ์ ๋ฒํธ (https://www.meatwatch.go.kr/biz/bm/sel/schoolListPopup.do)
*/
reset (type, region, schoolCode) {
if (!this._initialized) {
throw new Error('์ด๊ธฐํ ์ดํ ์ฌ์ค์ ํ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.')
} else if (type && region && schoolCode) {
this._eduType = this._DATA.EDUTYPE[type]
this._region = this._DATA.REGION[region]
this._schoolCode = schoolCode
// ์ฌ์ค์ ์๋ฃ
} else {
throw new Error('๊ต์ก๊ธฐ๊ด ์ ํ, ์ง์ญ, ํ๊ต ์ฝ๋๋ ํ์ ๋ฐ์ดํฐ ์
๋๋ค.')
}
}
/**
* @description type์ ํด๋นํ๋ ํ์ฑํ ํ๊ฒ URL ์์ฑ
* @param {string} type URL ์ ํ ๋ฌธ์์ด ๋ฐ์ดํฐ(meal, calendar)
* @return {string} ์ ํ์ ํด๋นํ๋ URL
*/
createUrl (type) {
if (this._initialized) {
const typeString = type.toLowerCase()
let typeUrl = ''
if (typeString === 'meal') {
typeUrl = this._mealUrl
} else if (typeString === 'calendar') {
typeUrl = this._calendarUrl
} else {
throw new Error('์ ์ ์๋ ์ ํ์
๋๋ค.')
}
return `https://${this._region}/${typeUrl}?schulCode=${this._schoolCode}&schulCrseScCode=${this._eduType}&schulKndScCode=${this._eduType}`
} else {
throw new Error('์ธ์คํด์ค๊ฐ ์ด๊ธฐํ ๋์ง ์์์ต๋๋ค.')
}
}
/**
* @description ์ด๋ฒ ๋ฌ ๊ธ์ ๋ฐ์ดํฐ๋ฅผ ํ์ฑํฉ๋๋ค.
* @return {any} ์ด๋ฒ ๋ฌ ๊ธ์ ๋ฐ์ดํฐ
*/
getMeal () {
if (this._initialized) {
return this._meal.getData(this.createUrl('meal'))
} else {
throw new Error('์ธ์คํด์ค๊ฐ ์ด๊ธฐํ ๋์ง ์์์ต๋๋ค.')
}
}
/**
* @description ์ด๋ฒ ๋ฌ ํ์ฌ์ผ์ ์ ํ์ฑํฉ๋๋ค.
* @return {any} ์ด๋ฒ ๋ฌ ํ์ฌ์ผ์ ๋ฐ์ดํฐ
*/
getCalendar () {
if (this._initialized) {
return this._calendar.getData(this.createUrl('calendar'))
} else {
throw new Error('์ธ์คํด์ค๊ฐ ์ด๊ธฐํ ๋์ง ์์์ต๋๋ค.')
}
}
/**
* @description ์ง์ ํ ์ ํ(meal, calendar)์ ํ์ฑ ํ๊ฒ ํ์ด์ง URL์ ๋ฐํํฉ๋๋ค.
* @param {string} type URL ์ ํ ๋ฌธ์์ด ๋ฐ์ดํฐ(meal, calendar)
* @return {string} ์ง์ ํ ์ ํ(meal, calendar)์ ํ๊ฒ ํ์ด์ง URL
*/
getTargetURL (type) {
return this.createUrl(type)
}
}
module.exports = School