Class: GoogleCalendar::Event

Inherits:
Object
  • Object
show all
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.

    1. your default calendar’s feed url is defined in Calendar::DEFAULT_CALENDAR_FEED. To get other calendar’s feed url, read below.

    2. click “Manage Calendars” in Google Calendar.

    3. select a calendar you want to edit.

    4. copy private address of XML.

    5. 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".
      
    6. 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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvent

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

#alldayObject

Returns the value of attribute allday.



105
106
107
# File 'lib/googlecalendar/event.rb', line 105

def allday
  @allday
end

#descObject

Returns the value of attribute desc.



105
106
107
# File 'lib/googlecalendar/event.rb', line 105

def desc
  @desc
end

#enObject

Returns the value of attribute en.



105
106
107
# File 'lib/googlecalendar/event.rb', line 105

def en
  @en
end

#feedObject

Returns the value of attribute feed.



105
106
107
# File 'lib/googlecalendar/event.rb', line 105

def feed
  @feed
end

#srvObject

Returns the value of attribute srv.



105
106
107
# File 'lib/googlecalendar/event.rb', line 105

def srv
  @srv
end

#stObject

Returns the value of attribute st.



105
106
107
# File 'lib/googlecalendar/event.rb', line 105

def st
  @st
end

#statusObject

Returns the value of attribute status.



105
106
107
# File 'lib/googlecalendar/event.rb', line 105

def status
  @status
end

#titleObject

Returns the value of attribute title.



105
106
107
# File 'lib/googlecalendar/event.rb', line 105

def title
  @title
end

#whereObject

Returns the value of attribute where.



105
106
107
# File 'lib/googlecalendar/event.rb', line 105

def where
  @where
end

#xmlObject

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

Raises:



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

#destroyObject

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

#saveObject

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_sObject

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