Class: Google::Event
- Inherits:
-
Object
- Object
- Google::Event
- Defined in:
- lib/google/event.rb
Overview
Represents a Google Event.
Attributes
-
id
- The google assigned id of the event (nil until saved). Read Write. -
status
- The status of the event (confirmed, tentative or cancelled). Read only. -
title
- The title of the event. Read Write. -
description
- The content of the event. Read Write. -
location
- The location 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. -
recurrence
- A hash containing recurrence info for repeating events. Read write. -
calendar
- What calendar the event belongs to. Read Write. -
all_day
- Does the event run all day. Read Write. -
quickadd
- A string that Google parses when setting up a new event. If set and then saved it will take priority over any attributes you have set. Read Write. -
reminders
- A hash containing reminders. Read Write. -
attendees
- An array of hashes containing information about attendees. Read Write -
transparency
- Does the event ‘block out space’ on the calendar. Valid values are true, false or ‘transparent’, ‘opaque’. Read Write. -
duration
- The duration of the event in seconds. Read only. -
html_link
- An absolute link to this event in the Google Calendar Web UI. Read only. -
raw
- The full google json representation of the event. Read only. -
visibility
- The visibility of the event (*‘default’*, ‘public’, ‘private’, ‘confidential’). Read Write. -
extended_properties
- Custom properties which may be shared or private. Read Write -
guests_can_invite_others
- Whether attendees other than the organizer can invite others to the event (true, false). Read Write. -
guests_can_see_other_guests
- Whether attendees other than the organizer can see who the event’s attendees are (true, false). Read Write. -
send_notifications
- Whether to send notifications about the event update (true, false). Write only.
Instance Attribute Summary collapse
-
#attendees ⇒ Object
Returns the value of attribute attendees.
-
#calendar ⇒ Object
Returns the value of attribute calendar.
-
#color_id ⇒ Object
Returns the value of attribute color_id.
-
#creator_name ⇒ Object
Returns the value of attribute creator_name.
-
#description ⇒ Object
Returns the value of attribute description.
-
#extended_properties ⇒ Object
Stores custom data within extended properties which can be shared or private.
-
#guests_can_invite_others ⇒ Object
Returns the value of attribute guests_can_invite_others.
-
#guests_can_see_other_guests ⇒ Object
Returns the value of attribute guests_can_see_other_guests.
-
#html_link ⇒ Object
readonly
Returns the value of attribute html_link.
-
#id ⇒ Object
Returns the value of attribute id.
-
#location ⇒ Object
Returns the value of attribute location.
-
#new_event_with_id_specified ⇒ Object
Returns the value of attribute new_event_with_id_specified.
-
#quickadd ⇒ Object
Returns the value of attribute quickadd.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#recurrence ⇒ Object
Stores recurrence rules for repeating events.
-
#reminders ⇒ Object
Stores reminders for this event.
-
#send_notifications ⇒ Object
Returns the value of attribute send_notifications.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#title ⇒ Object
Returns the value of attribute title.
-
#transparency ⇒ Object
Returns the value of attribute transparency.
-
#visibility ⇒ Object
Returns the value of attribute visibility.
Class Method Summary collapse
-
.build_from_google_feed(response, calendar) ⇒ Object
Convenience method used to build an array of events from a Google feed.
Instance Method Summary collapse
-
#all_day=(time) ⇒ Object
Makes an event all day, by setting it’s start time to the passed in time and it’s end time 24 hours later.
-
#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.
-
#attendees_attributes ⇒ Object
Hash representation of attendees.
-
#attendees_json ⇒ Object
JSON representation of attendees.
-
#color_attributes ⇒ Object
Hash representation of colors.
-
#color_json ⇒ Object
JSON representation of colors.
-
#delete ⇒ Object
Deletes an event.
-
#duration ⇒ Object
Duration of the event in seconds.
-
#end_time ⇒ Object
Get the end_time of the event.
-
#end_time=(time) ⇒ Object
Sets the end time of the Event.
-
#extended_properties_attributes ⇒ Object
Hash representation of extended properties shared : whether this should handle shared or public properties.
-
#extended_properties_json ⇒ Object
JSON representation of extended properties shared : whether this should handle shared or public properties.
-
#initialize(params = {}) ⇒ Event
constructor
Create a new event, and optionally set it’s attributes.
-
#local_timezone_attributes ⇒ Object
Hash representation of local timezone.
-
#local_timezone_json ⇒ Object
JSON representation of local timezone.
-
#new_event? ⇒ Boolean
Returns true if this a new event.
-
#opaque? ⇒ Boolean
Returns true if the event is opaque otherwise returns false.
-
#recurrence_attributes ⇒ Object
Hash representation of recurrence rules for repeating events.
-
#recurrence_json ⇒ Object
JSON representation of recurrence rules for repeating events.
-
#reminders_attributes ⇒ Object
Hash representation of a reminder.
-
#reminders_json ⇒ Object
JSON representation of a reminder.
-
#save ⇒ Object
Saves an event.
-
#send_notifications? ⇒ Boolean
Returns true if notifications were requested to be sent.
-
#start_time ⇒ Object
Get the start_time of the event.
-
#start_time=(time) ⇒ Object
Sets the start time of the Event.
-
#timezone_needed? ⇒ Boolean
Timezone info is needed only at recurring events.
-
#to_json ⇒ Object
Google JSON representation of an event object.
-
#to_s ⇒ Object
String representation of an event object.
-
#transparent? ⇒ Boolean
Returns true if the event is transparent otherwise returns false.
-
#use_quickadd? ⇒ Boolean
Returns true if the event will use quickadd when it is saved.
Constructor Details
#initialize(params = {}) ⇒ Event
Create a new event, and optionally set it’s attributes.
Example
event = Google::Event.new event.calendar = AnInstanceOfGoogleCalendaer event.id = “0123456789abcdefghijklmopqrstuv” event.start_time = Time.now event.end_time = Time.now + (60 * 60) event.recurrence = => ‘monthly’ event.title = “Go Swimming” event.description = “The polar bear plunge” event.location = “In the arctic ocean” event.transparency = “opaque” event.visibility = “public” event.reminders = => false, ‘overrides’ => [‘minutes’ => 10, ‘method’ => “popup”] event.attendees = [
{'email' => '[email protected]', 'displayName' => 'Some A One', 'responseStatus' => 'tentative'},
{'email' => '[email protected]', 'displayName' => 'Some B One', 'responseStatus' => 'tentative'}
]
event.extendedProperties = => {‘custom_str’ => ‘some custom string’} event.guests_can_invite_others = false event.guests_can_see_other_guests = false event.send_notifications = true
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/google/event.rb', line 66 def initialize(params = {}) [:id, :status, :raw, :html_link, :title, :location, :calendar, :quickadd, :attendees, :description, :reminders, :recurrence, :start_time, :end_time, :color_id, :extended_properties, :guests_can_invite_others, :guests_can_see_other_guests, :send_notifications].each do |attribute| instance_variable_set("@#{attribute}", params[attribute]) end self.visibility = params[:visibility] self.transparency = params[:transparency] self.all_day = params[:all_day] if params[:all_day] self.creator_name = params[:creator]['displayName'] if params[:creator] self.new_event_with_id_specified = !!params[:new_event_with_id_specified] end |
Instance Attribute Details
#attendees ⇒ Object
Returns the value of attribute attendees.
38 39 40 |
# File 'lib/google/event.rb', line 38 def attendees @attendees end |
#calendar ⇒ Object
Returns the value of attribute calendar.
38 39 40 |
# File 'lib/google/event.rb', line 38 def calendar @calendar end |
#color_id ⇒ Object
Returns the value of attribute color_id.
38 39 40 |
# File 'lib/google/event.rb', line 38 def color_id @color_id end |
#creator_name ⇒ Object
Returns the value of attribute creator_name.
38 39 40 |
# File 'lib/google/event.rb', line 38 def creator_name @creator_name end |
#description ⇒ Object
Returns the value of attribute description.
38 39 40 |
# File 'lib/google/event.rb', line 38 def description @description end |
#extended_properties ⇒ Object
Stores custom data within extended properties which can be shared or private.
Allowed contents: :private => a hash containing custom key/values (strings) private to the event OPTIONAL :shared => a hash containing custom key/values (strings) shared with others OPTIONAL
Note: Both private and shared can be specified at once
Example
event = cal.create_event do |e|
e.title = 'Work-day Event'
e.start_time = Time.now
e.end_time = Time.now + (60 * 60) # seconds * min
e.extended_properties = {'shared' => {'prop1' => 'value 1'}}
end
210 211 212 |
# File 'lib/google/event.rb', line 210 def extended_properties @extended_properties ||= {} end |
#guests_can_invite_others ⇒ Object
Returns the value of attribute guests_can_invite_others.
38 39 40 |
# File 'lib/google/event.rb', line 38 def guests_can_invite_others @guests_can_invite_others end |
#guests_can_see_other_guests ⇒ Object
Returns the value of attribute guests_can_see_other_guests.
38 39 40 |
# File 'lib/google/event.rb', line 38 def guests_can_see_other_guests @guests_can_see_other_guests end |
#html_link ⇒ Object (readonly)
Returns the value of attribute html_link.
36 37 38 |
# File 'lib/google/event.rb', line 36 def html_link @html_link end |
#id ⇒ Object
Returns the value of attribute id.
36 37 38 |
# File 'lib/google/event.rb', line 36 def id @id end |
#location ⇒ Object
Returns the value of attribute location.
38 39 40 |
# File 'lib/google/event.rb', line 38 def location @location end |
#new_event_with_id_specified ⇒ Object
Returns the value of attribute new_event_with_id_specified.
38 39 40 |
# File 'lib/google/event.rb', line 38 def new_event_with_id_specified @new_event_with_id_specified end |
#quickadd ⇒ Object
Returns the value of attribute quickadd.
38 39 40 |
# File 'lib/google/event.rb', line 38 def quickadd @quickadd end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
36 37 38 |
# File 'lib/google/event.rb', line 36 def raw @raw end |
#recurrence ⇒ Object
Stores recurrence rules for repeating events.
Allowed contents: :freq => frequence information (“daily”, “weekly”, “monthly”, “yearly”) REQUIRED :count => how many times the repeating event should occur OPTIONAL :until => Time class, until when the event should occur OPTIONAL :interval => how often should the event occur (every “2” weeks, …) OPTIONAL :byday => if frequence is “weekly”, contains ordered (starting with OPTIONAL
Sunday)comma separated abbreviations of days the event
should occur on ("su,mo,th")
if frequence is "monthly", can specify which day of month
the event should occur on ("2mo" - second Monday, "-1th" - last Thursday,
allowed indices are 1,2,3,4,-1)
Note: The hash should not contain :count and :until keys simultaneously.
Example
event = cal.create_event do |e|
e.title = 'Work-day Event'
e.start_time = Time.now
e.end_time = Time.now + (60 * 60) # seconds * min
e.recurrence = {freq: "weekly", byday: "mo,tu,we,th,fr"}
end
189 190 191 |
# File 'lib/google/event.rb', line 189 def recurrence @recurrence ||= {} end |
#reminders ⇒ Object
Stores reminders for this event. Multiple reminders are allowed.
Examples
event = cal.create_event do |e|
e.title = 'Some Event'
e.start_time = Time.now + (60 * 10)
e.end_time = Time.now + (60 * 60) # seconds * min
e.reminders = { 'useDefault' => false, 'overrides' => [{method: 'email', minutes: 4}, {method: 'popup', minutes: 60}, {method: 'sms', minutes: 30}]}
end
event = Event.new :start_time => “2012-03-31”, :end_time => “2012-04-03”, :reminders => { ‘useDefault’ => false, ‘overrides’ => [=> 10, ‘method’ => “popup”]}
160 161 162 |
# File 'lib/google/event.rb', line 160 def reminders @reminders ||= {} end |
#send_notifications ⇒ Object
Returns the value of attribute send_notifications.
38 39 40 |
# File 'lib/google/event.rb', line 38 def send_notifications @send_notifications end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
36 37 38 |
# File 'lib/google/event.rb', line 36 def status @status end |
#title ⇒ Object
Returns the value of attribute title.
38 39 40 |
# File 'lib/google/event.rb', line 38 def title @title end |
#transparency ⇒ Object
Returns the value of attribute transparency.
36 37 38 |
# File 'lib/google/event.rb', line 36 def transparency @transparency end |
#visibility ⇒ Object
Returns the value of attribute visibility.
36 37 38 |
# File 'lib/google/event.rb', line 36 def visibility @visibility end |
Class Method Details
.build_from_google_feed(response, calendar) ⇒ Object
Convenience method used to build an array of events from a Google feed.
256 257 258 259 |
# File 'lib/google/event.rb', line 256 def self.build_from_google_feed(response, calendar) events = response['items'] ? response['items'] : [response] events.collect {|e| new_from_feed(e, calendar)}.flatten end |
Instance Method Details
#all_day=(time) ⇒ Object
Makes an event all day, by setting it’s start time to the passed in time and it’s end time 24 hours later. Note: this will clobber both the start and end times currently set.
131 132 133 134 135 136 137 |
# File 'lib/google/event.rb', line 131 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.
122 123 124 125 |
# File 'lib/google/event.rb', line 122 def all_day? time = (@start_time.is_a? String) ? Time.parse(@start_time) : @start_time.dup.utc duration % (24 * 60 * 60) == 0 && time == Time.local(time.year,time.month,time.day) end |
#attendees_attributes ⇒ Object
Hash representation of attendees
313 314 315 316 317 318 319 320 321 |
# File 'lib/google/event.rb', line 313 def attendees_attributes return {} unless @attendees attendees = @attendees.map do |attendee| attendee.select { |k,_v| ['displayName', 'email', 'responseStatus'].include?(k) } end { "attendees" => attendees } end |
#attendees_json ⇒ Object
JSON representation of attendees
326 327 328 |
# File 'lib/google/event.rb', line 326 def attendees_json attendees_attributes.to_json end |
#color_attributes ⇒ Object
Hash representation of colors
298 299 300 301 |
# File 'lib/google/event.rb', line 298 def color_attributes return {} unless color_id { "colorId" => "#{color_id}" } end |
#color_json ⇒ Object
JSON representation of colors
306 307 308 |
# File 'lib/google/event.rb', line 306 def color_json color_attributes.to_json end |
#delete ⇒ Object
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.
430 431 432 433 |
# File 'lib/google/event.rb', line 430 def delete @calendar.delete_event(self) @id = nil end |
#duration ⇒ Object
Duration of the event in seconds
142 143 144 |
# File 'lib/google/event.rb', line 142 def duration Time.parse(end_time) - Time.parse(start_time) end |
#end_time ⇒ Object
Get the end_time of the event.
If no time is set (i.e. new event) it defaults to one hour in the future.
107 108 109 110 |
# File 'lib/google/event.rb', line 107 def end_time @end_time ||= Time.now.utc + (60 * 60) # seconds * min (@end_time.is_a? String) ? @end_time : @end_time.xmlschema end |
#end_time=(time) ⇒ Object
Sets the end time of the Event. Must be a Time object or a parse-able string representation of a time.
115 116 117 |
# File 'lib/google/event.rb', line 115 def end_time=(time) @end_time = Event.parse_time(time) end |
#extended_properties_attributes ⇒ Object
Hash representation of extended properties shared : whether this should handle shared or public properties
396 397 398 399 400 |
# File 'lib/google/event.rb', line 396 def extended_properties_attributes return {} unless @extended_properties && (@extended_properties['shared'] || @extended_properties['private']) { "extendedProperties" => @extended_properties.select {|k,_v| ['shared', 'private'].include?(k) } } end |
#extended_properties_json ⇒ Object
JSON representation of extended properties shared : whether this should handle shared or public properties
406 407 408 |
# File 'lib/google/event.rb', line 406 def extended_properties_json extended_properties_attributes.to_json end |
#local_timezone_attributes ⇒ Object
Hash representation of local timezone
359 360 361 362 363 |
# File 'lib/google/event.rb', line 359 def local_timezone_attributes tz = Time.now.getlocal.zone tz_name = TimezoneParser::getTimezones(tz).last { "timeZone" => tz_name } end |
#local_timezone_json ⇒ Object
JSON representation of local timezone
368 369 370 |
# File 'lib/google/event.rb', line 368 def local_timezone_json local_timezone_attributes.to_json end |
#new_event? ⇒ Boolean
Returns true if this a new event.
445 446 447 |
# File 'lib/google/event.rb', line 445 def new_event? new_event_with_id_specified? || id == nil || id == '' end |
#opaque? ⇒ Boolean
Returns true if the event is opaque otherwise returns false. Opaque events block time on a calendar.
238 239 240 |
# File 'lib/google/event.rb', line 238 def opaque? @transparency == "opaque" end |
#recurrence_attributes ⇒ Object
Hash representation of recurrence rules for repeating events
375 376 377 378 379 380 381 382 383 |
# File 'lib/google/event.rb', line 375 def recurrence_attributes return {} unless is_recurring_event? @recurrence[:until] = @recurrence[:until].strftime('%Y%m%dT%H%M%SZ') if @recurrence[:until] rrule = "RRULE:" + @recurrence.collect { |k,v| "#{k}=#{v}" }.join(';').upcase @recurrence[:until] = Time.parse(@recurrence[:until]) if @recurrence[:until] { "recurrence" => [rrule] } end |
#recurrence_json ⇒ Object
JSON representation of recurrence rules for repeating events
388 389 390 |
# File 'lib/google/event.rb', line 388 def recurrence_json recurrence_attributes.to_json end |
#reminders_attributes ⇒ Object
Hash representation of a reminder
333 334 335 336 337 338 339 340 |
# File 'lib/google/event.rb', line 333 def reminders_attributes if reminders && reminders.is_a?(Hash) && reminders['overrides'] { "useDefault" => false, "overrides" => reminders['overrides'] } else { "useDefault" => true} end end |
#reminders_json ⇒ Object
JSON representation of a reminder
345 346 347 |
# File 'lib/google/event.rb', line 345 def reminders_json reminders_attributes.to_json end |
#save ⇒ Object
Saves an event.
Note: make sure to set the calendar before calling this method.
421 422 423 |
# File 'lib/google/event.rb', line 421 def save update_after_save(@calendar.save_event(self)) end |
#send_notifications? ⇒ Boolean
Returns true if notifications were requested to be sent
452 453 454 |
# File 'lib/google/event.rb', line 452 def send_notifications? !!send_notifications end |
#start_time ⇒ Object
Get the start_time of the event.
If no time is set (i.e. new event) it defaults to the current time.
97 98 99 100 |
# File 'lib/google/event.rb', line 97 def start_time @start_time ||= Time.now.utc (@start_time.is_a? String) ? @start_time : @start_time.xmlschema end |
#start_time=(time) ⇒ Object
Sets the start time of the Event. Must be a Time object or a parse-able string representation of a time.
88 89 90 |
# File 'lib/google/event.rb', line 88 def start_time=(time) @start_time = Event.parse_time(time) end |
#timezone_needed? ⇒ Boolean
Timezone info is needed only at recurring events
352 353 354 |
# File 'lib/google/event.rb', line 352 def timezone_needed? is_recurring_event? end |
#to_json ⇒ Object
Google JSON representation of an event object.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/google/event.rb', line 264 def to_json attributes = { "summary" => title, "visibility" => visibility, "transparency" => transparency, "description" => description, "location" => location, "start" => time_or_all_day(start_time), "end" => time_or_all_day(end_time), "reminders" => reminders_attributes, "guestsCanInviteOthers" => guests_can_invite_others, "guestsCanSeeOtherGuests" => guests_can_see_other_guests } if id attributes["id"] = id end if timezone_needed? attributes['start'].merge!(local_timezone_attributes) attributes['end'].merge!(local_timezone_attributes) end attributes.merge!(recurrence_attributes) attributes.merge!(color_attributes) attributes.merge!(attendees_attributes) attributes.merge!(extended_properties_attributes) JSON.generate attributes end |
#to_s ⇒ Object
String representation of an event object.
413 414 415 |
# File 'lib/google/event.rb', line 413 def to_s "Event Id '#{self.id}'\n\tStatus: #{status}\n\tTitle: #{title}\n\tStarts: #{start_time}\n\tEnds: #{end_time}\n\tLocation: #{location}\n\tDescription: #{description}\n\tColor: #{color_id}\n\n" end |
#transparent? ⇒ Boolean
Returns true if the event is transparent otherwise returns false. Transparent events do not block time on a calendar.
230 231 232 |
# File 'lib/google/event.rb', line 230 def transparent? @transparency == "transparent" end |
#use_quickadd? ⇒ Boolean
Returns true if the event will use quickadd when it is saved.
438 439 440 |
# File 'lib/google/event.rb', line 438 def use_quickadd? quickadd && id == nil end |