Created
May 24, 2013 17:26
-
-
Save tag-dmasse/5645096 to your computer and use it in GitHub Desktop.
Based on http://blog.baugues.com/google-calendar-api-oauth2-and-ruby-on-rails. Get week's worth of time entries for a calendar and aggregate them, similar to http://gtimereport.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CalendarController < ApplicationController | |
def index | |
client = Google::APIClient.new | |
client.authorization.access_token = session[:access_token] | |
service = client.discovered_api('calendar', 'v3') | |
now = Time.now | |
start_day = 1 | |
end_day = now.wday | |
if end_day > start_day | |
diff = end_day - start_day | |
else | |
diff = 0 | |
end | |
@result = client.execute( | |
:api_method => service.events.list, | |
:parameters => { | |
:calendarId => 'YOUR_CALENDAR_ID', | |
:singleEvents => true, | |
:timeMax => now.end_of_day.utc.iso8601, | |
:timeMin => (now - diff.days).beginning_of_day.utc.iso8601 | |
}, | |
:headers => {'Content-Type' => 'application/json'}) | |
@grouped_days = Hash.new | |
@ungrouped_days = Hash.new | |
@totals = Hash.new | |
@week_total = 0 | |
@result.data.items.each do |item| | |
day = item.start.dateTime.strftime('%A') | |
event_title = item.summary.split(':')[0] | |
full_event_title = item.summary | |
mins = item.end.dateTime - item.start.dateTime | |
if not @grouped_days[day] | |
@grouped_days[day] = Hash.new | |
end | |
if not @ungrouped_days[day] | |
@ungrouped_days[day] = Hash.new | |
end | |
if not @grouped_days[day][event_title] | |
@grouped_days[day][event_title] = 0 | |
end | |
if not @ungrouped_days[day][full_event_title] | |
@ungrouped_days[day][full_event_title] = 0 | |
end | |
@grouped_days[day][event_title] += mins | |
@ungrouped_days[day][full_event_title] += mins | |
if not @totals[day] | |
@totals[day] = 0 | |
end | |
@totals[day] += mins | |
# Track week's hours | |
@week_total += mins | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
gem 'rails', '3.0.3' | |
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3' | |
gem 'nifty-generators' | |
gem 'google-api-client', :require => 'google/api_client' | |
gem 'omniauth', '1.1.0' | |
gem 'omniauth-google-oauth2', :git => 'https://github.com/zquestz/omniauth-google-oauth2.git' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% title "Timesheet" %> | |
<h2>Total: <%= (@week_total / 3600).to_int %>:<%= "%02d" % ((@week_total % 3600) / 60).to_int %>h</h2> | |
<h3>Grouped</h3> | |
<table> | |
<thead> | |
<tr> | |
<th>Day</th> | |
<th>Job</th> | |
<th>Time</th> | |
</tr> | |
</thead> | |
<tbody> | |
<% @grouped_days.each do |day, times| %> | |
<tr> | |
<td><%= day %></td> | |
</tr> | |
<% times.each do |job, time| %> | |
<tr> | |
<td></td> | |
<td><%= job %></td> | |
<td><%= (time / 3600).to_int %>:<%= "%02d" % ((time % 3600) / 60).to_int %>h</td> | |
<% end %> | |
<tr> | |
<td colspan="2"></td> | |
<td><%= (@totals[day] / 3600).to_int %>:<%= ((@totals[day] % 3600) / 60).to_int %>h</td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> | |
<h3>Ungrouped</h3> | |
<table> | |
<thead> | |
<tr> | |
<th>Day</th> | |
<th>Job</th> | |
<th>Time</th> | |
</tr> | |
</thead> | |
<tbody> | |
<% @ungrouped_days.each do |day, times| %> | |
<tr> | |
<td><%= day %></td> | |
</tr> | |
<% times.each do |job, time| %> | |
<tr> | |
<td></td> | |
<td><%= job %></td> | |
<td><%= (time / 3600).to_int %>:<%= "%02d" % ((time % 3600) / 60).to_int %>h</td> | |
<% end %> | |
<tr> | |
<td colspan="2"></td> | |
<td><%= (@totals[day] / 3600).to_int %>:<%= ((@totals[day] % 3600) / 60).to_int %>h</td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment