Permalink
Browse files

Get color from Google Calendar and also allow creating all day events.

  • Loading branch information...
1 parent 497496f commit 781368bb4a418936533be7a98418431cbd4258cb Tyler Gannon committed Mar 27, 2012
Showing with 41 additions and 4 deletions.
  1. +14 −4 lib/google/calendar.rb
  2. +9 −0 lib/google/event.rb
  3. +18 −0 test/test_google_calendar.rb
View
@@ -158,6 +158,10 @@ def reload
:auth_url => auth_url)
self
end
+
+ def display_color
+ calendar_data.xpath("//entry[title='#{@calendar}']/color/@value").first.value
+ end
protected
@@ -188,15 +192,21 @@ def calendar_id #:nodoc:
#
def events_url
if @calendar and !@calendar.include?("@")
- xml = @connection.send(Addressable::URI.parse("https://www.google.com/calendar/feeds/default/allcalendars/full"), :get)
- doc = Nokogiri::XML(xml.body)
- doc.remove_namespaces!
- link = doc.xpath("//entry[title='#{@calendar}']/link[contains(@rel, '#eventFeed')]/@href").to_s
+ link = calendar_data.xpath("//entry[title='#{@calendar}']/link[contains(@rel, '#eventFeed')]/@href").to_s
link.empty? ? raise(Google::InvalidCalendar) : link
else
"https://www.google.com/calendar/feeds/#{calendar_id}/private/full"
end
end
+
+ def calendar_data
+ unless @calendar_data
+ xml = @connection.send(Addressable::URI.parse("https://www.google.com/calendar/feeds/default/allcalendars/full"), :get)
+ @calendar_data = Nokogiri::XML(xml.body)
+ @calendar_data.remove_namespaces!
+ end
+ @calendar_data
+ end
def setup_event(event) #:nodoc:
event.calendar = self
View
@@ -36,6 +36,7 @@ def initialize(params = {})
@where = params[:where]
@start_time = params[:start_time]
@end_time = params[:end_time]
+ self.all_day= params[:all_day] if params[:all_day]
@calendar = params[:calendar]
@raw_xml = params[:raw_xml]
@quickadd = params[:quickadd]
@@ -79,6 +80,14 @@ def all_day?
duration == 24 * 60 * 60 # Exactly one day
end
+ def all_day=(time)
+ if time.class == String
+ time = Time.parse(time)
+ end
+ @start_time = time.strftime("%Y-%m-%d")
+ @end_time = (time + 24*60*60).strftime("%Y-%m-%d")
+ end
+
# Duration in seconds
def duration
Time.parse(end_time) - Time.parse(start_time)
@@ -232,6 +232,24 @@ class TestGoogleCalendar < Test::Unit::TestCase
end
end
end
+
+ context "#all_day=" do
+ context "sets the start and end time to the appropriate values for an all day event on that day" do
+ should "set the start time" do
+ @event = Event.new :all_day => Time.parse("2012-05-02 12:24")
+ assert_equal @event.start_time, "2012-05-02"
+ end
+ should "set the end time" do
+ @event = Event.new :all_day => Time.parse("2012-05-02 12:24")
+ assert_equal @event.end_time, "2012-05-03"
+ end
+ should "be able to handle strings" do
+ @event = Event.new :all_day => "2012-05-02 12:24"
+ assert_equal @event.start_time, "2012-05-02"
+ assert_equal @event.end_time, "2012-05-03"
+ end
+ end
+ end
end
def test_https_extension

0 comments on commit 781368b

Please sign in to comment.