Class: GoogleCalendar::Event
- Inherits:
-
Object
- Object
- GoogleCalendar::Event
- Defined in:
- lib/googlecalendar/event.rb
Overview
Summary
this class represents an event of a calendar.
How to use this class
-
MAIL: your gmail account.
-
PASS: password for MAIL.
-
FEED: a calendar’s editable feed url.
-
your default calendar’s feed url is defined in Calendar::DEFAULT_CALENDAR_FEED. To get other calendar’s feed url, read below.
-
click “Manage Calendars” in Google Calendar.
-
select a calendar you want to edit.
-
copy private address of XML.
-
change the address’s end into “/private/full”. If your calendar’s private XML address is
"http://www.google.com/calendar/feeds/[email protected]/private-aaaaaa/basic",
the editable feed url is
"http://www.google.com/calendar/feeds/[email protected]/private/full".
-
for detail, See code.google.com/apis/gdata/calendar.html#Visibility.
-
create new events
cal = Calendar.new(Service.new(MAIL, PASS), FEED)
event = cal.create_event
event.title = "event title"
event.desc = "event description"
event.where = "event location"
event.st = Time.mktime(2006, 9, 21, 01, 0, 0)
event.en = Time.mktime(2006, 9, 21, 03, 0, 0)
event.save!
udpate existing events
cal = Calendar.new(Service.new(MAIL, PASS), FEED)
event = cal.events[0]
event.title = "event title"
event.desc = "event description"
event.where = "event location"
event.st = Time.mktime(2006, 9, 21, 01, 0, 0)
event.en = Time.mktime(2006, 9, 21, 03, 0, 0)
event.save!
delete events
cal = Calendar.new(Service.new(MAIL, PASS), FEED)
event = cal.events[0]
event.destroy!
create all day events.
event = cal.create_event
event.title = "1 days event"
event.st = Time.mktime(2006, 9, 22)
event.en = Time.mktime(2006, 9, 23)
event.allday = true
event.save!
event = cal.create_event
event.title = "2 days event"
event.st = Time.mktime(2006, 9, 22)
event.en = Time.mktime(2006, 9, 24)
event.allday = true
event.save!
get existint event
event = Event.get(FEED, Service.new(MAIL, PASS))
TODO
-
this class doesn’t support recurring event.
Constant Summary collapse
- ATTRIBUTES_MAP =
{ "title" => { "element" => "title"}, "desc" => { "element" => "content"}, "where" => { "element" => "gd:where", "attribute" => "valueString" }, "st" => { "element" => "gd:when", "attribute" => "startTime", "to_xml" => "time_to_str", "from_xml" => "str_to_time" }, "en" => { "element" => "gd:when", "attribute" => "endTime", "to_xml" => "time_to_str", "from_xml" => "str_to_time" } }
- SKELTON =
<<XML <?xml version='1.0' encoding='UTF-8'?> <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'> <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'></category> <title type='text'></title> <content type='text'></content> <gd:transparency value='http://schemas.google.com/g/2005#event.opaque'></gd:transparency> <gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'></gd:eventStatus> </entry> XML
Instance Attribute Summary collapse
-
#allday ⇒ Object
Returns the value of attribute allday.
-
#desc ⇒ Object
Returns the value of attribute desc.
-
#en ⇒ Object
Returns the value of attribute en.
-
#feed ⇒ Object
Returns the value of attribute feed.
-
#srv ⇒ Object
Returns the value of attribute srv.
-
#st ⇒ Object
Returns the value of attribute st.
-
#status ⇒ Object
Returns the value of attribute status.
-
#title ⇒ Object
Returns the value of attribute title.
-
#where ⇒ Object
Returns the value of attribute where.
-
#xml ⇒ Object
Returns the value of attribute xml.
Class Method Summary collapse
-
.get(feed, srv) ⇒ Object
get event from event feed.
Instance Method Summary collapse
-
#destroy ⇒ Object
same as destroy! If failed, this method returns false.
-
#destroy! ⇒ Object
delete this event from google calendar server.
-
#initialize ⇒ Event
constructor
A new instance of Event.
-
#load_xml(str) ⇒ Object
load xml into this instance.
-
#save ⇒ Object
same as save! If failed, this method returns false.
-
#save! ⇒ Object
save this event into google calendar server.
-
#to_s ⇒ Object
retuns this event’s xml.
Constructor Details
#initialize ⇒ Event
Returns a new instance of Event.
107 108 109 110 |
# File 'lib/googlecalendar/event.rb', line 107 def initialize() @xml = nil self.status = :new end |
Instance Attribute Details
#allday ⇒ Object
Returns the value of attribute allday.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def allday @allday end |
#desc ⇒ Object
Returns the value of attribute desc.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def desc @desc end |
#en ⇒ Object
Returns the value of attribute en.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def en @en end |
#feed ⇒ Object
Returns the value of attribute feed.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def feed @feed end |
#srv ⇒ Object
Returns the value of attribute srv.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def srv @srv end |
#st ⇒ Object
Returns the value of attribute st.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def st @st end |
#status ⇒ Object
Returns the value of attribute status.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def status @status end |
#title ⇒ Object
Returns the value of attribute title.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def title @title end |
#where ⇒ Object
Returns the value of attribute where.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def where @where end |
#xml ⇒ Object
Returns the value of attribute xml.
105 106 107 |
# File 'lib/googlecalendar/event.rb', line 105 def xml @xml end |
Class Method Details
.get(feed, srv) ⇒ Object
get event from event feed
167 168 169 170 171 172 173 174 |
# File 'lib/googlecalendar/event.rb', line 167 def self.get(feed, srv) ret = srv.query(feed) raise EventGetFailed, ret.body unless ret.code == "200" evt = Event.new evt.srv = srv evt.load_xml(ret.body) evt end |
Instance Method Details
#destroy ⇒ Object
same as destroy! If failed, this method returns false.
143 144 145 |
# File 'lib/googlecalendar/event.rb', line 143 def destroy do_without_exception(:destroy!) end |
#destroy! ⇒ Object
delete this event from google calendar server. If failed, this method throws an Exception.
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/googlecalendar/event.rb', line 148 def destroy! ret = nil if self.status == :old ret = @srv.delete(self.feed, self.to_s) raise EventDeleteFailed, "Not Deleted" unless ret.code == "200" else raise EventDeleteFailed, "Not Saved" end status = :deleted end |
#load_xml(str) ⇒ Object
load xml into this instance
113 114 115 116 117 |
# File 'lib/googlecalendar/event.rb', line 113 def load_xml(str) @xml = REXML::Document.new(str.to_s) xml_to_instance self end |
#save ⇒ Object
same as save! If failed, this method returns false.
120 121 122 |
# File 'lib/googlecalendar/event.rb', line 120 def save do_without_exception(:save!) end |
#save! ⇒ Object
save this event into google calendar server. If failed, this method throws an Exception.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/googlecalendar/event.rb', line 125 def save! ret = nil case self.status when :new ret = @srv.insert(self.feed, self.to_s) raise EventInsertFailed, ret.body unless ret.code == "201" when :old ret = @srv.update(self.feed, self.to_s) raise EventUpdateFailed, ret.body unless ret.code == "200" when :deleted raise EventDeleteFailed, "already deleted" else raise StandardError, "invalid inner status" end load_xml(ret.body) end |
#to_s ⇒ Object
retuns this event’s xml.
160 161 162 163 164 |
# File 'lib/googlecalendar/event.rb', line 160 def to_s @xml = REXML::Document.new(SKELTON) if self.status == :new instance_to_xml @xml.to_s end |