Permalink
Browse files

Incorporate Google-Directions travel time & GCal Reminder override

Use Directions API to search for travel time. Grab from hash, using annoying hash notation (rather than simple dot notation, which Ruby complains about). Send JSON dump of event to Google, which updates the reminder override.
  • Loading branch information...
1 parent d728a32 commit 36139faf2e64cde0eedd06206340e1efcbb062c0 @ZhangBanger committed May 29, 2012
Showing with 48 additions and 6 deletions.
  1. +21 −0 app/controllers/static_pages_controller.rb
  2. +26 −5 app/views/static_pages/home.haml
  3. +1 −1 config/initializers/google_api.rb
View
21 app/controllers/static_pages_controller.rb
@@ -10,5 +10,26 @@ def home
# Get event list
result = GOOGLE_CLIENT.execute api_method: CALENDAR_SERVICE.events.list, parameters: {calendarId: 'primary', maxResults: 999, orderBy: 'updated'}
@events = result.data.items
+
+ @last_event = @events.last
+
+ # Get travel time from home to destination
+ @origin = "16357 SE Cougar Mountain Way, Bellevue, WA 98006".gsub(/\s+/, '+')
+ @destination = @last_event['location'].gsub(/\s+/, '+')
+
+ uri = URI("http://maps.googleapis.com/maps/api/directions/json?origin=#{@origin}&destination=#{@destination}&sensor=false")
+ response = Net::HTTP.get_response(uri)
+ @duration = JSON.parse(response.body)
+ @duration = @duration['status'] == 'OK' ? @duration['routes'].first['legs'].first['duration']['text'].split(' ')[0] : 'Directions not found!'
+
+ # Update reminder time on last event
+ @reminder_time = @last_event['reminders']['overrides'].any? ? @last_event['reminders']['overrides'].first['minutes'] : 'Default'
+ @last_event['reminders']['overrides'].first['minutes'] = (@duration.to_i * 1.25).to_i
+ @last_event['reminders']['useDefault'] = false
+ @new_reminder_time = @last_event['reminders']['overrides'].first['minutes']
+
+ # Send up to GoogleCalendar
+ result = GOOGLE_CLIENT.execute api_method: CALENDAR_SERVICE.events.patch, parameters: {calendarId: 'primary', eventId: "#{@last_event['id']}"}, body: JSON.dump(@last_event), headers: {'Content-Type' => 'application/json'}
+ @new_last_event = result.data['reminders']['overrides'].first.minutes
end
end
View
31 app/views/static_pages/home.haml
@@ -16,14 +16,35 @@
%h3 All Events
- @events.each do |event|
- = event.summary
+ = event['summary']
%br
- = @events.last.start.dateTime
+ = @last_event['start']['dateTime']
%br
- = @events.last.end.dateTime
+ = @last_event['end']['dateTime']
%br
- ID:
- = @events.last.id
+
+ Last event:
+ = @last_event['id']
+ %br
+ = @last_event['location']
+ %br
+
+ Travel time:
+ = @duration
+ %br
+
+ Reminder time:
+ = @reminder_time
+ %br
+
+ Adjusted reminder time:
+ = @new_reminder_time
+ %br
+
+ New Event
+ = @new_last_event
+ %br
+
-else
= link_to "Sign in with Google", "/auth/google_oauth2"
- flash.each do |name, msg|
View
2 config/initializers/google_api.rb
@@ -9,7 +9,7 @@
GOOGLE_CLIENT.authorization.client_secret = oauth_yaml["client_secret"]
GOOGLE_CLIENT.authorization.scope = oauth_yaml["scope"]
GOOGLE_CLIENT.authorization.refresh_token = oauth_yaml["refresh_token"]
-GOOGLE_CLIENT.authorization.access_token = oauth_yaml["access_token"]
+GOOGLE_CLIENT.authorization.access_token ||= oauth_yaml["access_token"]
# Initialize Calendar Service
CALENDAR_SERVICE = GOOGLE_CLIENT.discovered_api('calendar', 'v3')

0 comments on commit 36139fa

Please sign in to comment.