-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard-games-data.js
86 lines (77 loc) · 2.71 KB
/
board-games-data.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
const responseMapper = require('./internal-errors')
function mapGame(game) {
return {
id: game.id,
name: game.name,
image_url: game['image_url'],
max_playtime: game['max_playtime'],
description: game.description
}
}
function mapGames(games) {
return games.map(mapGame)
}
module.exports = function (config) {
const requestPromise = require('./request-promise')(config)
const BGA_CLIENT_ID = config.CLIENT_ID
const BGA_HOST = config.HOST
const BGA_SEARCH = `${BGA_HOST}search?client_id=${BGA_CLIENT_ID}`
const BGA_SEARCH_GAME_ID = `${BGA_SEARCH}&ids=%s`
const BGA_SEARCH_GAME_NAME = `${BGA_SEARCH}&name=%s`
const BGA_POPULAR_GAMES = `${BGA_SEARCH}&order_by=popularity`
return {
getPopularGames,
getSpecificGameWithName,
getSpecificGame,
getSpecificGames
}
function getSpecificGame(id) {
const options = requestPromise.buildOptions(BGA_SEARCH_GAME_ID.replace("%s", id), 'GET')
return requestPromise.buildRequestPromise(options)
.then(result => {
const parsedBody = JSON.parse(result.body)
if (!parsedBody.games) result.response.statusCode = 404
return responseMapper.mapResponse(result.response.statusCode,
() => {
return { game: mapGame(parsedBody.games[0]) }
}, 200, "Game")
}
)
}
function getPopularGames() {
const options = requestPromise.buildOptions(BGA_POPULAR_GAMES, 'GET')
return requestPromise.buildRequestPromise(options).then(
result => responseMapper.mapResponse(result.response.statusCode,
() => {
const parsedBody = JSON.parse(result.body)
const games = parsedBody.games.map(mapGame)
return { games }
})
)
}
function getSpecificGameWithName(name) {
const options = requestPromise.buildOptions(BGA_SEARCH_GAME_NAME.replace("%s", name), 'GET')
return requestPromise.buildRequestPromise(options)
.then(result => {
const parsedBody = JSON.parse(result.body)
if (!parsedBody.games) result.response.statusCode = 404
return responseMapper.mapResponse(result.response.statusCode,
() => {
return { games: mapGames(parsedBody.games) }
}, 200, "Game")
})
}
function getSpecificGames(ids) {
ids = ids.reduce((accumulator, id2) => accumulator + id2 + ",", "")
const options = requestPromise.buildOptions(BGA_SEARCH_GAME_ID.replace("%s", ids), 'GET')
return requestPromise.buildRequestPromise(options).then(
result => {
const parsedBody = JSON.parse(result.body)
return responseMapper.mapResponse(result.response.statusCode, () => {
const games = parsedBody.games.map(mapGame)
return games
})
}
)
}
}