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. click “Manage Calendars” in Google Calendar.

    2. select a calendar you want to edit.

    3. copy private address of XML.

    4. 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".
      
    5. 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!

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

Instance Method Summary collapse

Constructor Details

#initializeEvent

Returns a new instance of Event.



100
101
102
103
# File 'lib/googlecalendar/event.rb', line 100

def initialize()
  @xml = nil
  self.status = :new
end

Instance Attribute Details

#alldayObject

Returns the value of attribute allday.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def allday
  @allday
end

#descObject

Returns the value of attribute desc.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def desc
  @desc
end

#enObject

Returns the value of attribute en.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def en
  @en
end

#feedObject

Returns the value of attribute feed.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def feed
  @feed
end

#srvObject

Returns the value of attribute srv.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def srv
  @srv
end

#stObject

Returns the value of attribute st.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def st
  @st
end

#statusObject

Returns the value of attribute status.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def status
  @status
end

#titleObject

Returns the value of attribute title.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def title
  @title
end

#whereObject

Returns the value of attribute where.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def where
  @where
end

#xmlObject

Returns the value of attribute xml.



98
99
100
# File 'lib/googlecalendar/event.rb', line 98

def xml
  @xml
end

Instance Method Details

#destroyObject

same as destroy! If failed, this method returns false.



136
137
138
# File 'lib/googlecalendar/event.rb', line 136

def destroy
  do_without_exception(:destroy!)
end

#destroy!Object

delete this event from google calendar server. If failed, this method throws an Exception.



141
142
143
144
145
146
147
148
149
150
# File 'lib/googlecalendar/event.rb', line 141

def destroy!
  ret = nil
  if self.status == :old
    ret = @srv.delete(self.feed) 
    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



106
107
108
109
110
# File 'lib/googlecalendar/event.rb', line 106

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.



113
114
115
# File 'lib/googlecalendar/event.rb', line 113

def save
  do_without_exception(:save!)
end

#save!Object

save this event into google calendar server. If failed, this method throws an Exception.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/googlecalendar/event.rb', line 118

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.



153
154
155
156
157
# File 'lib/googlecalendar/event.rb', line 153

def to_s
  @xml = REXML::Document.new(SKELTON) if self.status == :new
  instance_to_xml
  @xml.to_s
end