request = require 'superagent'
class BaseClass
constructor: (@config) ->
throwError: (error) ->
{error} = error
error.code or= ''
error.type or= 'Error'
error.message or= 'Server error'
new Error "#{error.code} (#{error.type}) - #{error.message}"
request: (method = 'GET', url = '', params = {}, callback) ->
xhr = request[method.toLowerCase()] "#{@config.host}#{url}"
xhr.set 'Authorization', @config.apiKey
xhr.set 'Content-Type', 'application/json'
xhr.set 'Accept', "application/vnd.datarank.#{@config.version}+json"
if method is 'POST'
xhr.type 'form'
xhr.send params
else
xhr.query params
xhr.end (err, res) =>
throw err if err
if res.ok
callback null, res.body
else
callback @throwError res.body
replaceUrlParams: (opts, endpoint) ->
for key, value of opts
endpoint = endpoint.replace ":#{key}", value
endpoint
validate:
obj: (options = {}, validation = []) ->
validation.forEach (value) ->
throw new Error "Missing #{value} parameter." unless options[value]
true
str: (param = '') ->
throw new Error "Missing parameter." unless param
true
inclusion: (param = '', possibilities) ->
unless param in possibilities
throw new Error "#{param} isn't a possibility (#{possibilities})"
true
select: (collection = [], param = {}, type = 'one') ->
@validate.str type, [ 'one', 'all' ]
property = Object.keys(param)[0]
selected = (item for item in collection when item[property] is param[property])
if selected.length
return selected[0] if type is 'one'
return selected if type is 'all'
else
{}
module.exports = BaseClass