Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fetching toggl data works again
  • Loading branch information
Swizec committed Nov 12, 2013
1 parent 40fe30c commit 06cd5ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
32 changes: 15 additions & 17 deletions fetching/toggl.js
Expand Up @@ -22,6 +22,8 @@ exports.fetch_data = function (callback) {
parse_data(hourly_rates, data, function (err, parsed) {
if (err) return callback(err);

console.log(parsed);

callback(null, parsed);
});
});
Expand Down Expand Up @@ -99,38 +101,34 @@ var hourlies = function (callback) {


var fetch = function (callback) {
request.get({protocol: 'https',
hostname: 'www.toggl.com',
pathname: '/api/v6/time_entries.json',
query: {
start_date: (new Date('2012-09-01')).toISOString(),
end_date: (new Date()).toISOString()
},
auth: require('./secrets').toggl_api+':api_token'})
.set('Accept-Charset', 'utf-8')
.set('Accept', 'application/json')
.end(function (err, res) {
callback(err, res.body);
});

__request('/api/v8/time_entries',
{
start_date: (new Date('2012-09-01')).toISOString(),
end_date: (new Date()).toISOString()
},
function (err, res) {
callback(err, res.body);
});
};

var parse_data = function (hourly_rates, data, callback) {
data = _.groupBy(data.data.filter(function (e) { return !!e.project; }),
data = _.groupBy(data.filter(function (e) { return !!e.pid; }),
function (entry) {
return moment(new Date(entry.start)).format('YYYY-DDD');
});

console.log(data);

var income = function (entry) {
var hours = entry.duration/3600,
earned = 0,
rate = hourly_rates[entry.project.id];
rate = hourly_rates[entry.pid];

if (!rate.max_h || rate.max_h > 0) {
earned = rate.rate*hours;
}

hourly_rates[entry.project.id].max_h -= hours;
hourly_rates[entry.pid].max_h -= hours;

return earned;
};
Expand Down
2 changes: 1 addition & 1 deletion test/toggl.js
Expand Up @@ -13,7 +13,7 @@ describe('toggl fetching', function () {
toggl.fetch_data(function (err, data) {

var Y = new Date().getFullYear();
for (var i=1; i<=366; i++) {
for (var i=1; i<=365; i++) {
data.should.have.property(Y+'-'+i);
}

Expand Down

0 comments on commit 06cd5ec

Please sign in to comment.