blob: ef4a7c1e9469b83eb0fbf8531680a1e21f9006db [file] [log] [blame]
'use strict'
var ok = require('objectkit'),
UsergridEntity = require('./entity.js'),
UsergridUser = require('./user.js'),
UsergridResponseError = require('./responseError.js'),
helpers = require('../helpers'),
_ = require('underscore')
_.mixin(require('underscore.string'))
function UsergridResponse(response) {
if (ok(response.body).has('entities')) {
var entities = response.body.entities.map(function(en) {
var entity = new UsergridEntity(en)
return (entity.isUser) ? new UsergridUser(entity) : entity
})
_.extend(response, {
metadata: _.map(response.body, _.clone),
entities: entities
})
delete response.metadata.entities
response.first = _.first(entities) || undefined
response.entity = response.first
response.last = _.last(entities) || undefined
if (ok(response).has('first.isUser') === true) {
response.user = response.first
response.users = response.entities
}
helpers.setReadOnly(response.metadata)
} else {
response.error = new UsergridResponseError(response.body)
}
return response;
}
module.exports = UsergridResponse