Slide 8
Slide 8 text
REDUCE DUPLICATION
apiUser: (path='', data={}, method='GET', callback) ->
start = new Date().getTime()
# … param munging omitted
parameters =
user: app.settings.user
path: path
unless _.isUndefined(data) or _.isFunction(data)
parameters.data = data
log.debug 'REQ', "#{method} user/#{path}", parameters
request(method, '/pp.php')
.send(parameters)
.end((res) =>
log.debug 'RES', "#{method} user/#{path}", res
# If the response has a 5xx status code, record this.
if res.status >= 500
recordAPIFail(method, path, data, res)
# Record how long the api request took.
app.util.apiTiming(path, new Date().getTime() - start)
if res.status >= 400
if res.body?
error = res.body
else
error = res.status
else
error = null
if _.isFunction callback
return callback error, res.body, res
)
makeSiteApiFunction: (siteId) ->
return (path='', data={}, method = 'GET') ->
start = new Date().getTime()
# … param munging omitted
parameters =
site: siteId
path: path
unless _.isUndefined(data) or _.isFunction(data)
parameters.data = data
log.debug 'REQ', "#{method} site/#{path}", parameters
request(method, '/pp.php')
.send(parameters)
.end((res) =>
log.debug 'RES', "#{method} site/#{path}", res
# Record how long the api request took.
app.util.apiTiming(path, new Date().getTime() - start)
# If the response has a 5xx status code, record this.
if res.status >= 500
recordAPIFail(method, path, data, res)
if res.status >= 400
error = res.status
else
error = null
if _.isFunction callback
return callback error, res.body, res
)
memoized
different param
slightly different error checking code
same code, different order