forked from xtian/node-armory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
182 lines (140 loc) · 4.92 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
var request = require('request')
, crypto = require('crypto')
, url = require('url')
, utils = require('./utils')
var armory = { auth: { privateKey: null, publicKey: null } }
// Makes the request.
armory._get = function(path, options, callback) {
options.headers = options.headers || {}
options.jar = false
options.json = true
path = encodeURI('/api/wow' + path)
if (options.locale) { options._query.locale = options.locale }
if (!options.region) { throw new Error('region must be provided') }
options.uri = url.format({
protocol: 'http:'
, hostname: options.region + '.battle.net'
, pathname: path
, query: options._query
})
// Authentication
if (this.auth.privateKey && this.auth.publicKey) {
var signature = crypto.createHmac('sha1', this.auth.privateKey)
, date = new Date().toUTCString()
signature.update(['GET', date, path].join('\n') + '\n')
options.headers['Date'] = date
options.headers['Authorization'] = 'BNET ' + this.auth.publicKey + ':' +
signature.digest('base64')
}
if (callback) {
var cb = function(err, res, body) {
if (body && body.status === 'nok') {
err = err || new Error(body.reason)
}
callback.call(this, err, body, res)
}
}
return request(options, cb)
}
// Retrieves an object describing an arena team.
armory.arena = function(options, callback) {
var path = '/arena/' + [options.realm, options.size, options.id].join('/')
return this._get(path, options, callback)
}
// Retrieves an array of arena ladder information.
armory.arenaLadder = function(options, callback) {
var path = '/pvp/arena/' + options.battlegroup + '/' + options.id
options._query = utils.pick(options, ['asc', 'page', 'size'])
if (callback) {
var cb = function(err, body, res) {
var data = utils.getKey(body, 'arenateam')
callback.call(this, err, data, res)
}
}
return this._get(path, options, cb)
}
// Retrieves object describing a battle pet.
armory.battlePetStats = function(options, callback) {
var path = '/battlePet/stats/' + options.id
options._query = utils.pick(options, ['breedId', 'level', 'qualityId'])
return this._get(path, options, callback)
}
// Retrieves an array of challenge mode leaderboard information.
armory.challengeRegion = function(options) {
options.id = 'region'
return this.challenge.apply(this, arguments)
}
// Returns wrapped module where every method has default options applied
armory.defaults = function(defaults) {
defaults.id = defaults.id || defaults.name
delete defaults.name
var wrapped = utils.wrap(this, function(fn, context) {
return function(options, callback) {
if (options.toString() === '[object Object]') {
options = utils.merge(options, defaults)
return fn.call(context, options, callback)
}
return utils.initParams(function(options, callback) {
options = utils.merge(options, defaults)
return fn.call(context, options, callback)
}, context)(options, callback)
}
})
return wrapped
}
// Retrieves an array of rated battleground ladder information.
armory.rbgLadder = function(options, callback) {
var path = '/pvp/ratedbg/ladder'
options._query = utils.pick(options, ['asc', 'page', 'size'])
if (callback) {
var cb = function(err, body, res) {
var data = utils.getKey(body, 'bgRecord')
callback.call(this, err, data, res)
}
}
return this._get(path, options, cb)
}
// Retrieves array of realm status information.
armory.realmStatus = function(options, callback) {
var path = '/realm/status'
if (options.id) { options._query.realm = options.id }
if (callback) {
var cb = function(err, body, res) {
var data = utils.getKey(body, 'realms')
callback.call(this, err, data, res)
}
}
return this._get(path, options, cb)
}
// Retrieves an object describing a character or guild.
;['character', 'guild'].forEach(function(method) {
armory[method] = function(options, callback) {
if (options.fields) { options._query.fields = options.fields }
if (options.lastModified) {
options.headers['If-Modified-Since'] = new Date(options.lastModified)
.toUTCString()
}
var path = '/' + [method, options.realm, options.id].join('/')
return this._get(path, options, callback)
}
})
// Definitions for generic functions.
require('./methods').forEach(function(definition) {
definition.url = definition.url || definition.method
armory[definition.method] = function(options, callback) {
var id = options.id ? '/' + options.id : ''
, path = '/' + definition.url + id
, cb
if (definition.trailingSlash) { path += '/' }
if (callback && definition.key) {
cb = function(err, body, res) {
var data = utils.getKey(body, definition.key)
callback.call(this, err, data, res)
}
} else {
cb = callback
}
return this._get(path, options, cb)
}
})
module.exports = utils.wrap(armory, utils.initParams)