Skip to content

Commit

Permalink
Merge pull request #83 from voy/feature/getTimezone
Browse files Browse the repository at this point in the history
Added project.getTimezone API

CR: AKL
  • Loading branch information
akloboucnik committed Feb 3, 2015
2 parents 1184ebd + be64e49 commit ea6ed0c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
42 changes: 42 additions & 0 deletions src/project.js
Expand Up @@ -130,12 +130,54 @@ define(['xhr', 'util'], function(xhr, util) {
return d.promise();
};

/**
* Gets current timezone and its offset. Example output:
*
* {
* id: 'Europe/Prague',
* displayName: 'Central European Time',
* currentOffsetMs: 3600000
* }
*
* @method getTimezone
* @param {String} projectId - GD project identifier
*/
var getTimezone = function(projectId) {
var d = $.Deferred(),
bootstrapUrl = '/gdc/app/account/bootstrap?projectId=' + projectId;

xhr.get(bootstrapUrl).then(function(result) {
var timezone = result.bootstrapResource.current.timezone;
d.resolve(timezone);
}, d.reject);

return d.promise();
};

var setTimezone = function(projectId, timezone) {
var d = $.Deferred(),
timezoneServiceUrl = '/gdc/md/' + projectId + '/service/timezone',
data = {
service: { timezone: timezone }
};

xhr.ajax(timezoneServiceUrl, {
type: 'POST',
headers: { Accept: 'application/json' },
data: data
}).then(d.resolve, d.reject);

return d.promise();
};

return {
getCurrentProjectId: getCurrentProjectId,
getProjects: getProjects,
getDatasets: getDatasets,
getColorPalette: getColorPalette,
setColorPalette: setColorPalette,
getTimezone: getTimezone,
setTimezone: setTimezone,
DEFAULT_PALETTE: DEFAULT_PALETTE
};
});
Expand Down
57 changes: 55 additions & 2 deletions test/project_test.js
Expand Up @@ -118,7 +118,6 @@ define(['project', 'jquery'], function(project, $) {
});
});


describe('getCurrentProjectId', function() {
it('should resolve with project id', function(done) {
this.server.respondWith(
Expand Down Expand Up @@ -149,9 +148,63 @@ define(['project', 'jquery'], function(project, $) {
done();
});
});
});

describe('getTimezone', function() {
it('should return timezone using bootstrap', function(done) {
var bootstrapUrl = '/gdc/app/account/bootstrap?projectId=prjId';
var timezoneMock = {
id: 'Europe/Prague',
displayName: 'Central European Time',
currentOffsetMs: 3600000
};
var bootstrap = {
bootstrapResource: {
current: {
timezone: timezoneMock
}
}
};

this.server.respondWith('GET', bootstrapUrl, [
200,
{ 'Content-Type': 'application/json' },
JSON.stringify(bootstrap)
]);

project.getTimezone('prjId').then(function(timezone) {
expect(timezone).to.eql(timezoneMock);
done();
}, function() {
expect().fail('Should resolve with current timezone');
done();
});
});
});

describe('setTimezone', function() {
it('should save project timezone', function(done) {
var timezoneUrl = '/gdc/md/prjId/service/timezone';
var responseJSON = {
service: { timezone: 'Europe/Prague' }
};

this.server.respondWith('POST', timezoneUrl, [
200,
{ 'Content-Type': 'application/json' },
JSON.stringify(responseJSON)
]);

project.setTimezone('prjId', 'Europe/Prague').then(function(response) {
expect(response).to.eql(responseJSON);
done();
}, function() {
expect().fail('Should resolve with current timezone');
done();
});
});
});
});
});
});
});

0 comments on commit ea6ed0c

Please sign in to comment.