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

#initialize(params) ⇒ Calendar

Returns 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

#app_nameObject

Calendar attributes



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

def app_name
  @app_name
end

#auth_urlObject

Calendar attributes



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

def auth_url
  @auth_url
end

#calendarObject

Calendar attributes



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

def calendar
  @calendar
end

#connectionObject

Calendar attributes



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

def connection
  @connection
end

#passwordObject

Calendar attributes



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

def password
  @password
end

#usernameObject

Calendar attributes



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

def username
  @username
end

Instance Method Details

#create_event(&blk) ⇒ Object

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


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

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

#delete_event(event) ⇒ Object

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



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

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

#display_colorObject



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

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

#eventsObject

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

#find_event_by_id(id) ⇒ Object

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.


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

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

#find_events(query) ⇒ Object

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

#find_events_in_range(start_min, start_max, query = nil) ⇒ Object

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
87
# File 'lib/google/calendar.rb', line 79

def find_events_in_range(start_min, start_max,query = nil)
  formatted_start_min = start_min.strftime("%Y-%m-%dT%H:%M:%S")
  formatted_start_max = start_max.strftime("%Y-%m-%dT%H:%M:%S")
  if query
    event_lookup("?q=#{query}&start-min=#{formatted_start_min}&start-max=#{formatted_start_max}&recurrence-expansion-start=#{formatted_start_min}&recurrence-expansion-end=#{formatted_start_max}")
  else
    event_lookup("?start-min=#{formatted_start_min}&start-max=#{formatted_start_max}&recurrence-expansion-start=#{formatted_start_min}&recurrence-expansion-end=#{formatted_start_max}")
  end
end

#find_or_create_event_by_id(id, &blk) ⇒ Object

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.



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

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

#reloadObject

Explicitly reload the connection to google calendar

Examples class User

def calendar
  @calendar ||= Google::Calendar.new :username => "[email protected]", :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



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

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

#save_event(event) ⇒ Object

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



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

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