Class: Google::Calendar

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

Overview

Calendar is the main object you use to interact with events. use it to find, create, update and delete them.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Calendar) initialize(params)

A new instance of Calendar



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/google/calendar.rb', line 34

def initialize(params)
  self.username = params[:username]
  self.password = params[:password]
  self.calendar = params[:calendar]
  self.app_name = params[:app_name]
  self.auth_url = params[:auth_url]

  self.connection = Connection.new(:username => username,
                               :password => password,
                               :app_name => app_name,
                               :auth_url => auth_url)
end

Instance Attribute Details

- (Object) app_name

Calendar attributes



32
33
34
# File 'lib/google/calendar.rb', line 32

def app_name
  @app_name
end

- (Object) auth_url

Calendar attributes



32
33
34
# File 'lib/google/calendar.rb', line 32

def auth_url
  @auth_url
end

- (Object) calendar

Calendar attributes



32
33
34
# File 'lib/google/calendar.rb', line 32

def calendar
  @calendar
end

- (Object) connection

Calendar attributes



32
33
34
# File 'lib/google/calendar.rb', line 32

def connection
  @connection
end

- (Object) password

Calendar attributes



32
33
34
# File 'lib/google/calendar.rb', line 32

def password
  @password
end

- (Object) username

Calendar attributes



32
33
34
# File 'lib/google/calendar.rb', line 32

def username
  @username
end

Instance Method Details

- (Object) create_event(&blk)

Creates a new event and immediatly saves it. returns the event

Examples

# Use a block
cal.create_event do |e|
  e.title = "A New Event"
  e.where = "Room 101"
end

# Don't use a block (need to call save maunally)
event  = cal.create_event
event.title = "A New Event"
event.where = "Room 101"
event.save


115
116
117
# File 'lib/google/calendar.rb', line 115

def create_event(&blk)
  setup_event(Event.new, &blk)
end

- (Object) delete_event(event)

Deletes the specified event. This is a callback used by the Event class.



140
141
142
# File 'lib/google/calendar.rb', line 140

def delete_event(event)
  @connection.send(Addressable::URI.parse(events_url + "/#{event.id}"), :delete)
end

- (Object) display_color



165
166
167
# File 'lib/google/calendar.rb', line 165

def display_color
  calendar_data.xpath("//entry[title='#{@calendar}']/color/@value").first.value
end

- (Object) events

Find all of the events associated with this calendar.

Returns:
 nil if nothing found.
 a single event if only one found.
 an array of events if many found.


53
54
55
# File 'lib/google/calendar.rb', line 53

def events
  event_lookup()
end

- (Object) find_event_by_id(id)

Attempts to find the event specified by the id

Returns:
 nil if nothing found.
 a single event if only one found.
 an array of events if many found.


94
95
96
97
# File 'lib/google/calendar.rb', line 94

def find_event_by_id(id)
  return nil unless id && id.strip != ''
  event_lookup("/#{id}")
end

- (Object) find_events(query)

This is equivalnt to running a search in the google calendar web application. Google does not provide a way to easily specify what attributes you would like to search (i.e. title), by default it searches everything. If you would like to find specific attribute value (i.e. title=Picnic), run a query and parse the results.

Returns:
 nil if nothing found.
 a single event if only one found.
 an array of events if many found.


67
68
69
# File 'lib/google/calendar.rb', line 67

def find_events(query)
  event_lookup("?q=#{query}")
end

- (Object) find_events_in_range(start_min, start_max, options = {})

Find all of the events associated with this calendar that start in the given time frame. The lower bound is inclusive, whereas the upper bound is exclusive. Events that overlap the range are included.

Returns:
 nil if nothing found.
 a single event if only one found.
 an array of events if many found.


79
80
81
82
83
84
85
86
# File 'lib/google/calendar.rb', line 79

def find_events_in_range(start_min, start_max,options = {})
	options[:max_results] ||=  25
  formatted_start_min = start_min.strftime("%Y-%m-%dT%H:%M:%S")
  formatted_start_max = start_max.strftime("%Y-%m-%dT%H:%M:%S")
  query = "?start-min=#{formatted_start_min}&start-max=#{formatted_start_max}&recurrence-expansion-start=#{formatted_start_min}&recurrence-expansion-end=#{formatted_start_max}"
	query = "#{query}&max-results=#{options[:max_results]}"
  event_lookup(query)
end

- (Object) find_or_create_event_by_id(id, &blk)

looks for the spedified event id. If it is found it, updates it's vales and returns it. If the event is no longer on the server it creates a new one with the specified values. Works like the create_event method.



124
125
126
# File 'lib/google/calendar.rb', line 124

def find_or_create_event_by_id(id, &blk)
  setup_event(find_event_by_id(id) || Event.new, &blk)
end

- (Object) reload

Explicitly reload the connection to google calendar

Examples class User

def calendar
  @calendar ||= Google::Calendar.new :username => "foo@gmail.com", :password => "bar"
end

end user = User.new 2.times { user.calendar } #only one HTTP authentication request to google user.calendar.reload #new HTTP authentication request to google

Returns Google::Calendar instance



157
158
159
160
161
162
163
# File 'lib/google/calendar.rb', line 157

def reload
  self.connection = Connection.new(:username => username,
                               :password => password,
                               :app_name => app_name,
                               :auth_url => auth_url)
  self
end

- (Object) save_event(event)

Saves the specified event. This is a callback used by the Event class.



131
132
133
134
135
# File 'lib/google/calendar.rb', line 131

def save_event(event)
  method = (event.id == nil || event.id == '') ? :post : :put
  query_string = (method == :put) ? "/#{event.id}" : ''
  @connection.send(Addressable::URI.parse(events_url + query_string), method, event.to_xml)
end