Permalink
Browse files

Squashed commit of the following:

commit 7921b36
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Wed Dec 5 09:34:26 2012 -0800

    CLC-696 Tweak log levels, set default log level back to WARN

commit b423ffd
Merge: 1645939 88478ab
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Wed Dec 5 09:04:11 2012 -0800

    Merge pull request #2 from yuhunglin/CLC-696

    Timezone aware fixes

commit 88478ab
Author: Yu-Hung Lin <yuhung@yuhunglin.com>
Date:   Wed Dec 5 05:44:32 2012 -0800

    fixes on element on edge case of day_after_tomorrow, not so wonderful fudging of google task dates

commit 6478bb5
Author: Yu-Hung Lin <yuhung@yuhunglin.com>
Date:   Wed Dec 5 04:08:29 2012 -0800

    fixes

commit 1645939
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 18:32:09 2012 -0800

    CLC-696 In test, instantiating starting_date as a Time object should make the test pass even when the machine is in UTC

commit 0d88d47
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 18:00:33 2012 -0800

    CLC-696 Using Date.civil instead of Date.new may give us the right GMT timezone

commit 3f5f4c0
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 17:42:00 2012 -0800

    CLC-696 Add more log to determine_bucket method

commit 567fe4c
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 17:23:08 2012 -0800

    CLC-696 Increase log level another way

commit 48fe04a
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 16:53:34 2012 -0800

    CLC-696 Log more chattily so I can see what's going on

commit 5cc1499
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 16:37:04 2012 -0800

    CLC-696 Attempt to fix Travis test

commit 3762aeb
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 16:22:54 2012 -0800

    CLC-696 Fix test

commit 9064462
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 16:15:39 2012 -0800

    CLC-696 Get all the Canvas data into task feed, and test it

commit 25e30c3
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 11:36:15 2012 -0800

    CLC-696 Make some CanvasProxy tests part of testext

commit 53aaf91
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 09:46:38 2012 -0800

    CLC-696 Make MyTasks model more object oriented

commit 8326291
Author: Chris Tweney <chris@media.berkeley.edu>
Date:   Tue Dec 4 09:20:08 2012 -0800

    CLC-696 Add coming_up feed to Canvas proxy
  • Loading branch information...
1 parent 0bda5db commit e17e9d468f7af2bf7ab770d9a052614362afd21d Chris Tweney committed Dec 5, 2012
View
@@ -5,3 +5,6 @@ rvm:
env:
- JRUBY_OPTS="--client -Xcext.enabled=false" DISPLAY=:99.0
before_install: sh -e /etc/init.d/xvfb start
+
+logger:
+ level: <%= DEBUG %>
@@ -2,10 +2,11 @@ class MyTasksController < ApplicationController
def get_feed
if session[:user_id]
- render :json => MyTasks.get_feed(session[:user_id]).to_json
+ my_tasks_model = MyTasks.new session[:user_id]
+ render :json => my_tasks_model.get_feed.to_json
else
render :json => {}.to_json
end
end
-end
+end
View
@@ -1,86 +1,139 @@
class MyTasks
include ActiveAttr::Model
- def self.get_feed(uid, starting_date=Date.today, opts={})
- Rails.cache.fetch(self.cache_key(uid)) do
- my_tasks = {}
- buckets = {
+ def initialize(uid, starting_date=Date.today.to_time_in_current_zone)
+ @uid = uid
+ #To avoid issues with tz, use time or DateTime instead of Date (http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails)
+ @starting_date = starting_date
+ @buckets = {
"due" => {"title" => "Due", "tasks" => []},
"due_tomorrow" => {"title" => "Due Tomorrow", "tasks" => []},
"upcoming" => {"title" => "Upcoming", "tasks" => []},
"unscheduled" => {"title" => "Unscheduled", "tasks" => []}
+ }
+ end
+
+ def get_feed
+ Rails.cache.fetch(self.class.cache_key(@uid)) do
+ fetch_google_tasks
+ fetch_canvas_tasks
+ my_tasks = {
+ "sections" => [@buckets["due"], @buckets["due_tomorrow"], @buckets["upcoming"], @buckets["unscheduled"]]
}
+ logger.debug "#{self.class.name} get_feed is #{my_tasks.inspect}"
+ my_tasks
+ end
+ end
- if GoogleProxy.access_granted?(uid)
- google_proxy = GoogleProxy.new(user_id: uid)
+ def self.cache_key(uid)
+ key = "user/#{uid}/#{self.name}"
+ logger.debug "#{self.class.name} cache_key will be #{key}"
+ key
+ end
- google_tasks_results = google_proxy.tasks_list(opts)
- google_tasks_results.each do |response_page|
- next unless response_page.response.status == 200
+ def self.expire(uid)
+ Rails.cache.delete(self.cache_key(uid), :force => true)
+ end
- response_page.data["items"].each do |entry|
- next if entry["title"].blank?
- formatted_entry = {
+ private
+
+ def fetch_google_tasks
+ if GoogleProxy.access_granted?(@uid)
+ google_proxy = GoogleProxy.new(user_id: @uid)
+
+ google_tasks_results = google_proxy.tasks_list
+ logger.info "#{self.class.name} Sorting Google tasks into buckets with starting_date #{@starting_date}"
+ google_tasks_results.each do |response_page|
+ next unless response_page.response.status == 200
+
+ response_page.data["items"].each do |entry|
+ next if entry["title"].blank?
+ formatted_entry = {
"type" => "task",
"title" => entry["title"] || "",
"emitter" => "Google Tasks",
"link_url" => "https://mail.google.com/tasks/canvas?pli=1",
"source_url" => entry["selfLink"] || "",
"class" => "class2"
- }
-
- status = "needs_action" if entry["status"] == "needsAction"
- status ||= "completed"
- formatted_entry["status"] = status
- due_date = entry["due"]
- if !due_date.blank?
- formatted_entry["due_date"] = {
- "epoch" => due_date.to_i,
- "datetime" => DateTime.parse(due_date.to_s).rfc3339(3),
- "date_string" => DateTime.parse(due_date.to_s).strftime("%-m/%d")
- }
- end
+ }
- bucket = determine_bucket(starting_date, due_date, status, formatted_entry)
- buckets[bucket]["tasks"].push(formatted_entry)
- end
+ status = "needs_action" if entry["status"] == "needsAction"
+ status ||= "completed"
+ formatted_entry["status"] = status
+ # Google task dates have misleading datetime accuracy. There is no way to record a specific due time
+ # for tasks (through the UI), thus the reported time+tz is always 00:00:00+0000. Stripping off the false
+ # accuracy so the application will apply the proper timezone when needed.
+ due_date = Date.parse(entry["due"].to_s) unless entry["due"].blank?
+ convert_due_date(due_date, formatted_entry)
+ bucket = determine_bucket(due_date, status, formatted_entry)
+ logger.info "#{self.class.name} Putting Google task with due_date #{formatted_entry["due_date"]} in #{bucket} bucket: #{formatted_entry}"
+ @buckets[bucket]["tasks"].push(formatted_entry)
end
end
- my_tasks["sections"] = [buckets["due"], buckets["due_tomorrow"], buckets["upcoming"], buckets["unscheduled"]]
-
- logger.debug "#{self.class.name} get_feed is #{my_tasks.inspect}"
- my_tasks
end
end
- def self.cache_key(uid)
- key = "user/#{uid}/#{self.name}"
- logger.debug "#{self.class.name} cache_key will be #{key}"
- key
+ def fetch_canvas_tasks
+ if CanvasProxy.access_granted?(@uid)
+ canvas_proxy = CanvasProxy.new(:user_id => @uid)
+ response = canvas_proxy.coming_up
+ if response.status == 200
+ results = JSON.parse response.body
+ logger.info "#{self.class.name} Sorting Canvas tasks into buckets with starting_date #{@starting_date}"
+ results.each do |result|
+ formatted_entry = {
+ "type" => result["type"].downcase,
+ "title" => result["title"],
+ "emitter" => CanvasProxy::APP_ID,
+ "link_url" => result["html_url"],
+ "source_url" => result["html_url"],
+ "color_class" => "class1",
+ "status" => "inprogress"
+ }
+ due_date = result["start_at"]
+ convert_due_date(due_date, formatted_entry)
+ bucket = determine_bucket(due_date, "inprogress", formatted_entry)
+ logger.info "#{self.class.name} Putting Canvas task with due_date #{formatted_entry["due_date"]} in #{bucket} bucket: #{formatted_entry}"
+ @buckets[bucket]["tasks"].push(formatted_entry)
+ end
+ end
+ end
end
- def self.expire(uid)
- Rails.cache.delete(self.cache_key(uid), :force => true)
+ def convert_due_date(due_date, formatted_entry)
+ if !due_date.blank?
+ due = due_date.to_time_in_current_zone.to_datetime if due_date.is_a?(Date)
+ due ||= DateTime.parse(due_date.to_s)
+ formatted_entry["due_date"] = {
+ "epoch" => due.to_i,
+ "datetime" => due.rfc3339(3),
+ "date_string" => due.strftime("%-m/%d")
+ }
+ end
end
- private
-
# Helps determine what section category for a task
- def self.determine_bucket(starting_date, due_date, status, formatted_entry)
+ def determine_bucket(due_date, status, formatted_entry)
bucket = "unscheduled"
- today = starting_date.to_time.to_i
- tomorrow = (starting_date + 1).to_time.to_i
- day_after_tomorrow = (starting_date + 2).to_time.to_i
+ if !due_date.blank?
+ due = due_date.to_time_in_current_zone if due_date.is_a?(Date)
+ due ||= DateTime.parse(due_date.to_s)
+ @starting_date = @starting_date.to_time.in_time_zone.to_i unless @starting_date.is_a?(Time)
+ today = @starting_date.to_i
+ tomorrow = @starting_date.advance(:days => 1).to_i
+ day_after_tomorrow = @starting_date.advance(:days => 2).to_i
- if !due_date.blank? && (due_date.to_i < tomorrow)
- bucket = "due"
- formatted_entry["status"] = "overdue" if (status == "needs_action" && (due_date.to_i < today))
- elsif !due_date.blank? && (due_date.to_i >= tomorrow) && (due_date.to_i < day_after_tomorrow)
- bucket = "due_tomorrow"
- elsif !due_date.blank? && (due_date.to_i > day_after_tomorrow)
- bucket = "upcoming"
- end
+ if due.to_i < tomorrow
+ bucket = "due"
+ formatted_entry["status"] = "overdue" if (status == "needs_action" && (due.to_i < today))
+ elsif due.to_i >= tomorrow && due.to_i < day_after_tomorrow
+ bucket = "due_tomorrow"
+ elsif due.to_i >= day_after_tomorrow
+ bucket = "upcoming"
+ end
+ logger.debug "#{self.class.name} In determine_bucket, @starting_date = #{@starting_date}, today = #{today}; formatted entry = #{formatted_entry}"
+ end
bucket
end
end
@@ -31,7 +31,7 @@ class Application < Rails::Application
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
- # config.time_zone = 'Central Time (US & Canada)'
+ config.time_zone = 'Pacific Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
View
@@ -14,7 +14,7 @@ canvas_proxy:
admin_access_token: "someMumboJumbo"
url_root: "http://localhost:12345"
fake: false
- client_id: 1
+ client_id: "300939"
client_secret: "someMumboJumbo"
token_credential_uri: "http://localhost:12345/login/oauth2/token"
authorization_uri: "http://localhost:12345/login/oauth2/auth"
Oops, something went wrong.

0 comments on commit e17e9d4

Please sign in to comment.