Class: Google::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/google/event.rb

Overview

Represents a google Event.

Attributes

  • id - The google assigned id of the event (nil until saved), read only.
  • title - The title of the event, read/write.
  • content - The content of the event, read/write.
  • start_time - The start time of the event (Time object, defaults to now), read/write.
  • end_time - The end time of the event (Time object, defaults to one hour from now), read/write.
  • calendar - What calendar the event belongs to, read/write.
  • raw_xml - The full google xml representation of the event.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Event

Create a new event, and optionally set it's attributes.

Example

Event.new(:title => 'Swimming', :content => 'Do not forget a towel this time', :where => 'The Ocean', :start_time => Time.now, :end_time => Time.now + (60 * 60), :calendar => calendar_object)



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/google/event.rb', line 32

def initialize(params = {})
  @id = params[:id]
  @title = params[:title]
  @content = params[:content]
  @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]
end

Instance Attribute Details

#calendarObject

Returns the value of attribute calendar.



20
21
22
# File 'lib/google/event.rb', line 20

def calendar
  @calendar
end

#contentObject

Returns the value of attribute content.



20
21
22
# File 'lib/google/event.rb', line 20

def content
  @content
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/google/event.rb', line 19

def id
  @id
end

#quickaddObject

Returns the value of attribute quickadd.



20
21
22
# File 'lib/google/event.rb', line 20

def quickadd
  @quickadd
end

#raw_xmlObject (readonly)

Returns the value of attribute raw_xml.



19
20
21
# File 'lib/google/event.rb', line 19

def raw_xml
  @raw_xml
end

#titleObject

Returns the value of attribute title.



20
21
22
# File 'lib/google/event.rb', line 20

def title
  @title
end

#whereObject

Returns the value of attribute where.



20
21
22
# File 'lib/google/event.rb', line 20

def where
  @where
end

Class Method Details

.build_from_google_feed(xml, calendar) ⇒ Object



97
98
99
# File 'lib/google/event.rb', line 97

def self.build_from_google_feed(xml, calendar)
  Nokogiri::XML(xml).xpath("//xmlns:entry").collect {|e| new_from_xml(e, calendar)}
end

Instance Method Details

#all_day=(time) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/google/event.rb', line 83

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

#all_day?Boolean

Returns whether the Event is an all-day event, based on whether the event starts at the beginning and ends at the end of the day.

Returns:

  • (Boolean)


79
80
81
# File 'lib/google/event.rb', line 79

def all_day?
  duration == 24 * 60 * 60 # Exactly one day
end

#deleteObject

Deletes an event. Note: If using this on an event you created without using a calendar object, make sure to set the calendar before calling this method.



142
143
144
145
# File 'lib/google/event.rb', line 142

def delete
  @calendar.delete_event(self)
  @id = nil
end

#durationObject

Duration in seconds



92
93
94
# File 'lib/google/event.rb', line 92

def duration
  Time.parse(end_time) - Time.parse(start_time)
end

#end_timeObject

Get the end_time of the event.

If no time is set (i.e. new event) it defaults to one hour in the future.



65
66
67
68
# File 'lib/google/event.rb', line 65

def end_time
  @end_time ||= Time.now + (60 * 60) # seconds * min
  (@end_time.is_a? String) ? @end_time : @end_time.utc.xmlschema
end

#end_time=(time) ⇒ Object

Sets the end time of the Event. Must be a Time object or a parsable string representation of a time.

Raises:

  • (ArgumentError)


72
73
74
75
# File 'lib/google/event.rb', line 72

def end_time=(time)
  raise ArgumentError, "End Time must be either Time or String" unless (time.is_a?(String) || time.is_a?(Time))
  @end_time = ((time.is_a? String) ? Time.parse(time) : time)
end

#saveObject

Saves an event. Note: If using this on an event you created without using a calendar object, make sure to set the calendar before calling this method.



134
135
136
# File 'lib/google/event.rb', line 134

def save
  update_after_save(@calendar.save_event(self))
end

#start_timeObject

Get the start_time of the event.

If no time is set (i.e. new event) it defaults to the current time.



56
57
58
59
# File 'lib/google/event.rb', line 56

def start_time
  @start_time ||= Time.now
  (@start_time.is_a? String) ? @start_time : @start_time.utc.xmlschema
end

#start_time=(time) ⇒ Object

Sets the start time of the Event. Must be a Time object or a parsable string representation of a time.

Raises:

  • (ArgumentError)


47
48
49
50
# File 'lib/google/event.rb', line 47

def start_time=(time)
  raise ArgumentError, "Start Time must be either Time or String" unless (time.is_a?(String) || time.is_a?(Time))
  @start_time = (time.is_a? String) ? Time.parse(time) : time
end

#to_sObject

String representation of an event object.



124
125
126
127
128
# File 'lib/google/event.rb', line 124

def to_s
  s = "#{title} (#{self.id})\n\t#{start_time}\n\t#{end_time}\n\t#{where}\n\t#{content}"
  s << "\n\t#{quickadd}" if quickadd
  s
end

#to_xmlObject

Google XMl representation of an evetn object.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/google/event.rb', line 103

def to_xml
  unless quickadd
    "<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}</title>
      <content type='text'>#{content}</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>
      <gd:where valueString=\"#{where}\"></gd:where>
      <gd:when startTime=\"#{start_time}\" endTime=\"#{end_time}\"></gd:when>
     </entry>"
  else
    %Q{<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gCal='http://schemas.google.com/gCal/2005'>
        <content type="html">#{content}</content>
        <gCal:quickadd value="true"/>
      </entry>}
  end
end