gcal4ruby 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,4 +1,21 @@
1
1
  #=CHANGELOG
2
+ #==version 0.5.0 - MAJOR CHANGES
3
+ #* Now using GData4Ruby library as base class for service, calendar, and event objects. This removed the base.rb class, which is now inherited from
4
+ # GData4ruby
5
+ #* Added 'events' method to service to get all events for an account.
6
+ #* Major interface changes to find methods - these are not backwards compatible changes.
7
+ #* Changed Event#find default behavior to search all calendars.
8
+ #* Major interface changes to to_iframe methods - these are not backwards compatible changes.
9
+ #* Added support for multiple reminders.
10
+ #* Improved object relationship for events. Events are now service-centric, i.e. events are instantiated with a service object.
11
+ #* Event calendar is now set through a 'calendar' attribute.
12
+ #* Fixes to event recurrence handling and display.
13
+ #* Added support for reminders with recurring events.
14
+ #* Numerous improvements to performance.
15
+ #* Calendar objects no longer have to query ACL feed to determine editability.
16
+ #* Added support for event statuses: cancelled, tentative or confirmed.
17
+ #==version 0.3.2
18
+ #* Updated the Event.find method to support finding all events by passing :all as the query term.
2
19
  #==version 0.3.1
3
20
  #* Fixed Event.find method to work with secondary calendars and google apps accounts. Fix provided by groesser3.
4
21
  #* Added max results to Calendar.find.
data/README CHANGED
@@ -10,7 +10,7 @@
10
10
  #and is licenses under the GPL v2. Feel free to use and update, but be sure to contribute your
11
11
  #code back to the project and attribute as required by the license.
12
12
  #===Website
13
- #http://rubyforge.org/projects/gcal4ruby/
13
+ #http://cookingandcoding.com/gcal4ruby/
14
14
  #
15
15
  #==Description
16
16
  #GCal4Ruby has three major components: the service, calendar and event objects. Each service
@@ -33,53 +33,65 @@
33
33
  #1. Create a new Calendar
34
34
  # cal = Calendar.new(service)
35
35
  #
36
- #2. Find an existing Calendar
37
- # cal = Calendar.find(service, "New Calendar", :first)
36
+ #2. Find a calendar by ID
37
+ # cal = Calendar.find(service, {:id => cal_id})
38
38
  #
39
- #3. Find all calendars containing the search term
40
- # cal = Calendar.find(service, "Soccer Team")
39
+ #3. Get all calendar events
40
+ # cal = Calendar.find(service, {:id => cal_id})
41
+ # events = cal.events
42
+ #
43
+ #4. Find an existing calendar by title
44
+ # cal = Calendar.find(service, {:title => "New Calendar"})
41
45
  #
42
- #4. Find a calendar by ID
43
- # cal = Calendar.find(service, id, :first)
46
+ #5. Find all calendars containing a search term
47
+ # cal = Calendar.find(service, "Soccer Team")
44
48
  #===Event
45
49
  #All usages assume a successfully authenticated Service and valid Calendar.
46
50
  #1. Create a new Event
47
- # event = Event.new(calendar)
48
- # event.title = "Soccer Game"
49
- # event.start = Time.parse("12-06-2009 at 12:30 PM")
50
- # event.end = Time.parse("12-06-2009 at 1:30 PM")
51
- # event.where = "Merry Playfields"
51
+ # event = Event.new(service, {:calendar => cal, :title => "Soccer Game", :start => Time.parse("12-06-2009 at 12:30 PM"), :end => Time.parse("12-06-2009 at 1:30 PM"), :where => "Merry Playfields"})
52
52
  # event.save
53
53
  #
54
- #2. Find an existing Event
55
- # event = Event.find(cal, "Soccer Game", {:scope => :first})
54
+ #2. Find an existing Event by title
55
+ # event = Event.find(service, {:title => "Soccer Game"})
56
+ #
57
+ #3. Find an existing Event by ID
58
+ # event = Event.find(service, {:id => event.id})
56
59
  #
57
- #3. Find all events containing the search term
58
- # event = Event.find(cal, "Soccer Game")
60
+ #4. Find all events containing the search term
61
+ # event = Event.find(service, "Soccer Game")
59
62
  #
60
- #4. Create a recurring event for every saturday
61
- # event = Event.new(calendar)
63
+ #5. Find all events on a calendar containing the search term
64
+ # event = Event.find(service, "Soccer Game", {:calendar => cal.id})
65
+ #
66
+ #6. Find events within a date range
67
+ # event = Event.find(service, "Soccer Game", {'start-min' => Time.parse("01/01/2010").utc.xmlschema, 'start-max' => Time.parse("06/01/2010").utc.xmlschema})
68
+ #
69
+ #7. Create a recurring event for every saturday
70
+ # event = Event.new(service)
62
71
  # event.title = "Baseball Game"
72
+ # event.calendar = cal
63
73
  # event.where = "Municipal Stadium"
64
74
  # event.recurrence = Recurrence.new
65
- # event.recurrence.start = Time.parse("13-06-2009 at 4:30 PM")
66
- # event.recurrence.end = Time.parse("13-06-2009 at 6:30 PM")
75
+ # event.recurrence.start_time = Time.parse("06/20/2009 at 4:30 PM")
76
+ # event.recurrence.end_time = Time.parse("06/20/2009 at 6:30 PM")
67
77
  # event.recurrence.frequency = {"weekly" => ["SA"]}
68
78
  # event.save
69
79
  #
70
- #5. Create an event with a 15 minute email reminder
71
- # event = Event.new(calendar)
80
+ #8. Create an event with a 15 minute email reminder
81
+ # event = Event.new(service)
82
+ # event.calendar = cal
72
83
  # event.title = "Dinner with Kate"
73
- # event.start = Time.parse("20-06-2009 at 5 pm")
74
- # event.end = Time.parse("20-06-209 at 8 pm")
84
+ # event.start_time = Time.parse("06/20/2009 at 5 pm")
85
+ # event.end_time = Time.parse("06/20/2009 at 8 pm")
75
86
  # event.where = "Luigi's"
76
87
  # event.reminder = {:minutes => 15, :method => 'email'}
77
88
  # event.save
78
89
  #
79
- #6. Create an event with attendees
80
- # event = Event.new(calendar)
90
+ #9. Create an event with attendees
91
+ # event = Event.new(service)
92
+ # event.calendar = cal
81
93
  # event.title = "Dinner with Kate"
82
- # event.start = Time.parse("20-06-2009 at 5 pm")
83
- # event.end = Time.parse("20-06-209 at 8 pm")
94
+ # event.start_time = Time.parse("06/20/2009 at 5 pm")
95
+ # event.end_time = Time.parse("06/20/2009 at 8 pm")
84
96
  # event.attendees => {:name => "Kate", :email => "kate@gmail.com"}
85
- # event.save
97
+ # event.save
@@ -1,451 +1,356 @@
1
- require 'gcal4ruby/event'
2
-
3
- module GCal4Ruby
4
- #The Calendar Class is the representation of a Google Calendar. Each user account
5
- #can have multiple calendars. You must have an authenticated Service object before
6
- #using the Calendar object.
7
- #=Usage
8
- #All usages assume a successfully authenticated Service.
9
- #1. Create a new Calendar
10
- # cal = Calendar.new(service)
11
- #
12
- #2. Find an existing Calendar
13
- # cal = Calendar.find(service, "New Calendar", :first)
1
+ # Author:: Mike Reich (mike@seabourneconsulting.com)
2
+ # Copyright:: Copyright (C) 2010 Mike Reich
3
+ # License:: GPL v2
4
+ #--
5
+ # Licensed under the General Public License (GPL), Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
14
8
  #
15
- #3. Find all calendars containing the search term
16
- # cal = Calendar.find(service, "Soccer Team")
9
+ # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
17
10
  #
18
- #4. Find a calendar by ID
19
- # cal = Calendar.find(service, id, :first)
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
14
  #
21
- #After a calendar object has been created or loaded, you can change any of the
22
- #attributes like you would any other object. Be sure to save the calendar to write changes
23
- #to the Google Calendar service.
24
-
25
- class Calendar
26
- CALENDAR_FEED = "http://www.google.com/calendar/feeds/default/owncalendars/full"
27
-
28
- #The calendar title
29
- attr_accessor :title
30
-
31
- #A short description of the calendar
32
- attr_accessor :summary
33
-
34
- #The parent Service object passed on initialization
35
- attr_reader :service
36
-
37
- #The unique calendar id
38
- attr_reader :id
39
-
40
- #Boolean value indicating the calendar visibility
41
- attr_accessor :hidden
42
-
43
- #The calendar timezone[http://code.google.com/apis/calendar/docs/2.0/reference.html#gCaltimezone]
44
- attr_accessor :timezone
45
-
46
- #The calendar color. Must be one of these[http://code.google.com/apis/calendar/docs/2.0/reference.html#gCalcolor] values.
47
- attr_accessor :color
48
-
49
- #The calendar geo location, if any
50
- attr_accessor :where
51
-
52
- #A boolean value indicating whether the calendar appears by default when viewed online
53
- attr_accessor :selected
54
-
55
- #The event feed for the calendar
56
- attr_reader :event_feed
57
-
58
- #A flag indicating whether the calendar is editable by this account
59
- attr_reader :editable
60
-
61
- #Returns true if the calendar exists on the Google Calendar system (i.e. was
62
- #loaded or has been saved). Otherwise returns false.
63
- def exists?
64
- return @exists
65
- end
66
-
67
- #Returns true if the calendar is publically accessable, otherwise returns false.
68
- def public?
69
- return @public
70
- end
71
-
72
- #Returns an array of Event objects corresponding to each event in the calendar.
73
- def events
74
- events = []
75
- ret = @service.send_get(@event_feed)
76
- REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
77
- entry.attributes["xmlns:gCal"] = "http://schemas.google.com/gCal/2005"
78
- entry.attributes["xmlns:gd"] = "http://schemas.google.com/g/2005"
79
- entry.attributes["xmlns:app"] = "http://www.w3.org/2007/app"
80
- entry.attributes["xmlns"] = "http://www.w3.org/2005/Atom"
81
- entry.attributes["xmlns:georss"] = "http://www.georss.org/georss"
82
- entry.attributes["xmlns:gml"] = "http://www.opengis.net/gml"
83
- e = Event.new(self)
84
- if e.load(entry.to_s)
85
- events << e
15
+ # Feel free to use and update, but be sure to contribute your
16
+ # code back to the project and attribute as required by the license.
17
+ #++
18
+ require 'gdata4ruby/acl/access_rule'
19
+ module GCal4Ruby
20
+ #The Calendar Class is the representation of a Google Calendar. Each user account
21
+ #can have multiple calendars. You must have an authenticated Service object before
22
+ #using the Calendar object.
23
+ #=Usage
24
+ #All usages assume a successfully authenticated Service.
25
+ #1. Create a new Calendar
26
+ # cal = Calendar.new(service)
27
+ #
28
+ #2. Find a calendar by ID
29
+ # cal = Calendar.find(service, {:id => cal_id})
30
+ #
31
+ #3. Get all calendar events
32
+ # cal = Calendar.find(service, {:id => cal_id})
33
+ # events = cal.events
34
+ #
35
+ #4. Find an existing calendar by title
36
+ # cal = Calendar.find(service, {:title => "New Calendar"})
37
+ #
38
+ #5. Find all calendars containing a search term
39
+ # cal = Calendar.find(service, "Soccer Team")
40
+ #
41
+ #After a calendar object has been created or loaded, you can change any of the
42
+ #attributes like you would any other object. Be sure to save the calendar to write changes
43
+ #to the Google Calendar service.
44
+ class Calendar < GData4Ruby::GDataObject
45
+ CALENDAR_FEED = "http://www.google.com/calendar/feeds/default/owncalendars/full/"
46
+ CALENDAR_QUERY_FEED = "http://www.google.com/calendar/feeds/default/calendars/"
47
+ CALENDAR_XML = "<entry xmlns='http://www.w3.org/2005/Atom'
48
+ xmlns:gd='http://schemas.google.com/g/2005'
49
+ xmlns:gCal='http://schemas.google.com/gCal/2005'>
50
+ <title type='text'></title>
51
+ <summary type='text'></summary>
52
+ <gCal:timezone value='America/Los_Angeles'></gCal:timezone>
53
+ <gCal:hidden value='false'></gCal:hidden>
54
+ <gCal:color value='#2952A3'></gCal:color>
55
+ <gd:where rel='' label='' valueString='Oakland'></gd:where>
56
+ </entry>"
57
+
58
+ #A short description of the calendar
59
+ attr_accessor :summary
60
+
61
+ #Boolean value indicating the calendar visibility
62
+ attr_accessor :hidden
63
+
64
+ #The calendar timezone[http://code.google.com/apis/calendar/docs/2.0/reference.html#gCaltimezone]
65
+ attr_accessor :timezone
66
+
67
+ #The calendar color. Must be one of these[http://code.google.com/apis/calendar/docs/2.0/reference.html#gCalcolor] values.
68
+ attr_accessor :color
69
+
70
+ #The calendar geo location, if any
71
+ attr_accessor :where
72
+
73
+ #A boolean value indicating whether the calendar appears by default when viewed online
74
+ attr_accessor :selected
75
+
76
+ #A flag indicating whether the calendar is editable by this account
77
+ attr_reader :editable
78
+
79
+ #Accepts a Service object and an optional attributes hash for initialization. Returns the new Calendar
80
+ #if successful, otherwise raises the InvalidService error.
81
+ def initialize(service, attributes = {})
82
+ super(service, attributes)
83
+ if !service.is_a?(Service)
84
+ raise InvalidService
86
85
  end
86
+ @xml = CALENDAR_XML
87
+ @service ||= service
88
+ @exists = false
89
+ @title ||= ""
90
+ @summary ||= ""
91
+ @public ||= false
92
+ @hidden ||= false
93
+ @timezone ||= "America/Los_Angeles"
94
+ @color ||= "#2952A3"
95
+ @where ||= ""
96
+ @permissions = ''
97
+ attributes.each do |key, value|
98
+ self.send("#{key}=", value)
99
+ end
100
+ return true
87
101
  end
88
- return events
89
- end
90
-
91
- #Set the calendar to public (p = true) or private (p = false). Publically viewable
92
- #calendars can be accessed by anyone without having to log in to google calendar. See
93
- #Calendar#to_iframe for options to display a public calendar in a webpage.
94
- def public=(p)
95
- if p
96
- permissions = 'http://schemas.google.com/gCal/2005#read'
97
- else
98
- permissions = 'none'
102
+
103
+ #Returns true if the calendar exists on the Google Calendar system (i.e. was
104
+ #loaded or has been saved). Otherwise returns false.
105
+ def exists?
106
+ return @exists
99
107
  end
100
108
 
101
- #if p != @public
102
- path = "http://www.google.com/calendar/feeds/#{@id}/acl/full/default"
103
- request = REXML::Document.new(ACL_XML)
104
- request.root.elements.each() do |ele|
105
- if ele.name == 'role'
106
- ele.attributes['value'] = permissions
109
+ #Returns true if the calendar is publically accessable, otherwise returns false.
110
+ def public?
111
+ return @public
112
+ end
113
+
114
+ #Returns an array of Event objects corresponding to each event in the calendar.
115
+ def events
116
+ events = []
117
+ ret = @service.send_request(GData4Ruby::Request.new(:get, @content_uri))
118
+ REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
119
+ entry = GData4Ruby::Utils.add_namespaces(entry)
120
+ e = Event.new(service)
121
+ if e.load(entry.to_s)
122
+ events << e
107
123
  end
108
-
109
- end
110
- if @service.send_put(path, request.to_s, {"Content-Type" => "application/atom+xml", "Content-Length" => request.length.to_s})
111
- @public = p
112
- return true
113
- else
114
- return false
115
124
  end
116
- #end
117
- end
118
-
119
- #Accepts a Service object and an optional attributes hash for initialization. Returns the new Calendar
120
- #if successful, otherwise raises the InvalidService error.
121
- def initialize(service, attributes = {})
122
- super()
123
- if !service.is_a?(Service)
124
- raise InvalidService
125
- end
126
- attributes.each do |key, value|
127
- self.send("#{key}=", value)
125
+ return events
128
126
  end
129
- @xml ||= CALENDAR_XML
130
- @service ||= service
131
- @exists = false
132
- @title ||= ""
133
- @summary ||= ""
134
- @public ||= false
135
- @hidden ||= false
136
- @timezone ||= "America/Los_Angeles"
137
- @color ||= "#2952A3"
138
- @where ||= ""
139
- return true
140
- end
141
-
142
- #Deletes a calendar. If successful, returns true, otherwise false. If successful, the
143
- #calendar object is cleared.
144
- def delete
145
- if @exists
146
- if @service.send_delete(CALENDAR_FEED+"/"+@id)
147
- @exists = false
148
- @title = nil
149
- @summary = nil
150
- @public = false
151
- @id = nil
152
- @hidden = false
153
- @timezone = nil
154
- @color = nil
155
- @where = nil
156
- return true
127
+
128
+ #Set the calendar to public (p = true) or private (p = false). Publically viewable
129
+ #calendars can be accessed by anyone without having to log in to google calendar. See
130
+ #Calendar#to_iframe on how to display a public calendar in a webpage.
131
+ def public=(p)
132
+ if p
133
+ @permissions = 'http://schemas.google.com/gCal/2005#read'
157
134
  else
158
- return false
135
+ @permissions = 'none'
159
136
  end
160
- else
161
- return false
162
137
  end
163
- end
164
-
165
- #If the calendar does not exist, creates it, otherwise updates the calendar info. Returns
166
- #true if the save is successful, otherwise false.
167
- def save
168
- if @exists
169
- ret = service.send_put(@edit_feed, to_xml(), {'Content-Type' => 'application/atom+xml'})
170
- else
171
- ret = service.send_post(CALENDAR_FEED, to_xml(), {'Content-Type' => 'application/atom+xml'})
138
+
139
+ #Creates a new instance of the object
140
+ def create
141
+ return service.send_request(GData4Ruby::Request.new(:post, CALENDAR_FEED, to_xml()))
172
142
  end
173
- if !@exists
174
- if load(ret.read_body)
175
- return true
143
+
144
+ #Finds a Calendar based on a text query or by an id. Parameters are:
145
+ #*service*:: A valid Service object to search.
146
+ #*query*:: either a string containing a text query to search by, or a hash containing an +id+ key with an associated id to find, or a +query+ key containint a text query to search for, or a +title+ key containing a title to search.
147
+ #*args*:: a hash containing optional additional query paramters to use. See http://code.google.com/apis/gdata/docs/2.0/reference.html#Queries for a full list of possible values. Example:
148
+ # {'max-results' => '100'}
149
+ #If an ID is specified, a single instance of the calendar is returned if found, otherwise false.
150
+ #If a query term or title text is specified, and array of matching results is returned, or an empty array if nothing
151
+ #was found.
152
+ def self.find(service, query, args = {})
153
+ raise ArgumentError, 'query must be a hash or string' if not query.is_a? Hash and not query.is_a? String
154
+ if query.is_a? Hash and query[:id]
155
+ id = query[:id]
156
+ puts "id passed, finding calendar by id" if service.debug
157
+ puts "id = "+id if service.debug
158
+ d = service.send_request(GData4Ruby::Request.new(:get, CALENDAR_FEED+id, {"If-Not-Match" => "*"}))
159
+ puts d.inspect if service.debug
160
+ if d
161
+ return get_instance(service, d)
162
+ end
176
163
  else
177
- raise CalendarSaveFailed
178
- end
179
- end
180
- return true
181
- end
182
-
183
- #Class method for querying the google service for specific calendars. The service parameter
184
- #should be an appropriately authenticated Service. The term parameter can be any string. The
185
- #scope parameter may be either :all to return an array of matches, or :first to return
186
- #the first match as a Calendar object.
187
- def self.find(service, query_term=nil, params = {})
188
- t = query_term.downcase if query_term
189
- cals = service.calendars
190
- ret = []
191
- cals.each do |cal|
192
- title = cal.title || ""
193
- summary = cal.summary || ""
194
- id = cal.id || ""
195
- if id == query_term
196
- return cal
197
- end
198
- if title.downcase.match(t) or summary.downcase.match(t)
199
- if params[:scope] == :first
200
- return cal
201
- else
202
- ret << cal
164
+ #fugly, but Google doesn't provide a way to query the calendar feed directly
165
+ old_public = service.check_public
166
+ service.check_public = false
167
+ results = []
168
+ cals = service.calendars
169
+ cals.each do |cal|
170
+ if query.is_a?(Hash)
171
+ results << cal if query[:query] and cal.title.downcase.include? query[:query].downcase
172
+ results << cal if query[:title] and cal.title == query[:query]
173
+ else
174
+ results << cal if cal.title.downcase.include? query.downcase
175
+ end
203
176
  end
177
+ service.check_public = old_public
178
+ return results
204
179
  end
205
- end
206
- ret
207
- end
208
-
209
- def self.get(service, id)
210
- url = 'http://www.google.com/calendar/feeds/default/allcalendars/full/'+id
211
- ret = service.send_get(url)
212
- puts "==return=="
213
- puts ret.body
214
- end
215
-
216
- def self.query(service, query_term)
217
- url = 'http://www.google.com/calendar/feeds/default/allcalendars/full'+"?q="+CGI.escape(query_term)
218
- ret = service.send_get(url)
219
- puts "==return=="
220
- puts ret.body
221
- end
222
-
223
- #Reloads the calendar objects information from the stored server version. Returns true
224
- #if successful, otherwise returns false. Any information not saved will be overwritten.
225
- def reload
226
- if not @exists
227
- return false
228
- end
229
- t = Calendar.find(service, @id, :first)
230
- if t
231
- load(t.to_xml)
232
- else
233
180
  return false
234
181
  end
235
- end
236
-
237
- #Returns the xml representation of the Calenar.
238
- def to_xml
239
- xml = REXML::Document.new(@xml)
240
- xml.root.elements.each(){}.map do |ele|
241
- case ele.name
242
- when "title"
243
- ele.text = @title
244
- when "summary"
245
- ele.text = @summary
246
- when "timezone"
247
- ele.attributes["value"] = @timezone
248
- when "hidden"
249
- ele.attributes["value"] = @hidden.to_s
250
- when "color"
251
- ele.attributes["value"] = @color
252
- when "selected"
253
- ele.attributes["value"] = @selected.to_s
182
+
183
+ #Reloads the calendar objects information from the stored server version. Returns true
184
+ #if successful, otherwise returns false. Any information not saved will be overwritten.
185
+ def reload
186
+ return false if not @exists
187
+ t = self.find(service, @id)
188
+ if t
189
+ load(t.to_xml)
190
+ else
191
+ return false
254
192
  end
255
193
  end
256
- xml.to_s
257
- end
258
-
259
- #Loads the Calendar with returned data from Google Calendar feed. Returns true if successful.
260
- def load(string)
261
- @exists = true
262
- @xml = string
263
- xml = REXML::Document.new(string)
264
- xml.root.elements.each(){}.map do |ele|
265
- case ele.name
266
- when "id"
267
- @id = ele.text.gsub("http://www.google.com/calendar/feeds/default/calendars/", "")
268
- when 'title'
269
- @title = ele.text
270
- when 'summary'
271
- @summary = ele.text
194
+
195
+ #Returns the xml representation of the Calenar.
196
+ def to_xml
197
+ xml = REXML::Document.new(super)
198
+ xml.root.elements.each(){}.map do |ele|
199
+ case ele.name
200
+ when "summary"
201
+ ele.text = @summary
202
+ when "timezone"
203
+ ele.attributes["value"] = @timezone
204
+ when "hidden"
205
+ ele.attributes["value"] = @hidden.to_s
272
206
  when "color"
273
- @color = ele.attributes['value']
274
- when 'hidden'
275
- @hidden = ele.attributes["value"] == "true" ? true : false
276
- when 'timezone'
277
- @timezone = ele.attributes["value"]
207
+ ele.attributes["value"] = @color
278
208
  when "selected"
279
- @selected = ele.attributes["value"] == "true" ? true : false
280
- when "link"
281
- if ele.attributes['rel'] == 'edit'
282
- @edit_feed = ele.attributes['href']
283
- end
209
+ ele.attributes["value"] = @selected.to_s
210
+ when 'role'
211
+ ele.attributes['value'] = @permissions
212
+ end
284
213
  end
214
+ xml.to_s
285
215
  end
286
-
287
- @event_feed = "http://www.google.com/calendar/feeds/#{@id}/private/full"
288
-
289
- if @service.check_public
290
- puts "Getting ACL Feed" if @service.debug
291
-
292
- #rescue error on shared calenar ACL list access
293
- begin
294
- ret = @service.send_get("http://www.google.com/calendar/feeds/#{@id}/acl/full/")
295
- rescue Exception => e
296
- @public = false
297
- @editable = false
298
- return true
299
- end
300
- @editable = true
301
- r = REXML::Document.new(ret.read_body)
302
- r.root.elements.each("entry") do |ele|
303
- ele.elements.each do |e|
304
- #puts "e = "+e.to_s if @service.debug
305
- #puts "previous element = "+e.previous_element.to_s if @service.debug
306
- #added per eruder http://github.com/h13ronim/gcal4ruby/commit/3074ebde33bd3970500f6de992a66c0a4578062a
307
- if e.name == 'role' and e.previous_element and e.previous_element.name == 'scope' and e.previous_element.attributes['type'] == 'default'
308
- if e.attributes['value'].match('#read')
309
- @public = true
310
- else
311
- @public = false
216
+
217
+ #Loads the Calendar with returned data from Google Calendar feed. Returns true if successful.
218
+ def load(string)
219
+ super(string)
220
+ @exists = true
221
+ @xml = string
222
+ xml = REXML::Document.new(string)
223
+ xml.root.elements.each(){}.map do |ele|
224
+ case ele.name
225
+ when "id"
226
+ @id = ele.text.gsub("http://www.google.com/calendar/feeds/default/calendars/", "")
227
+ when 'summary'
228
+ @summary = ele.text
229
+ when "color"
230
+ @color = ele.attributes['value']
231
+ when 'hidden'
232
+ @hidden = ele.attributes["value"] == "true" ? true : false
233
+ when 'timezone'
234
+ @timezone = ele.attributes["value"]
235
+ when "selected"
236
+ @selected = ele.attributes["value"] == "true" ? true : false
237
+ when "link"
238
+ if ele.attributes['rel'] == 'edit'
239
+ @edit_feed = ele.attributes['href']
312
240
  end
313
- end
241
+ when 'accesslevel'
242
+ @editable = (ele.attributes["value"] == 'editor' or ele.attributes["value"] == 'owner' or ele.attributes["value"] == 'root')
314
243
  end
315
244
  end
316
- else
317
- @public = false
318
- @editable = true
245
+
246
+ if @service.check_public
247
+ puts "Getting ACL Feed" if @service.debug
248
+
249
+ #rescue error on shared calenar ACL list access
250
+ begin
251
+ ret = @service.send_request(GData4Ruby::Request.new(:get, @acl_uri))
252
+ rescue Exception => e
253
+ puts "ACL Feed Get Failed: #{e.inspect}" if @service.debug
254
+ @public = false
255
+ return true
256
+ end
257
+ r = REXML::Document.new(ret.read_body)
258
+ r.root.elements.each("entry") do |ele|
259
+ e = GData4Ruby::ACL::AccessRule.new(service, self)
260
+ ele = GData4Ruby::Utils.add_namespaces(ele)
261
+ e.load(ele.to_s)
262
+ @public == (e.role.include? 'read' and e.user == 'default')
263
+ break if @public
264
+ end
265
+ else
266
+ @public = false
267
+ end
268
+ return true
319
269
  end
320
- return true
321
- end
322
-
323
- #Helper function to return the currently loaded calendar formatted iframe embedded google calendar.
324
- #1. *params*: a hash of parameters that affect the display of the embedded calendar:
325
- # height:: the height of the embedded calendar in pixels
326
- # width:: the width of the embedded calendar in pixels
327
- # title:: the title to display
328
- # bgcolor:: the background color. Limited choices, see google docs for allowable values.
329
- # color:: the color of the calendar elements. Limited choices, see google docs for allowable values.
330
- # showTitle:: set to 'false' to hide the title
331
- # showDate:: set to 'false' to hide the current date
332
- # showNav:: set to 'false to hide the navigation tools
333
- # showPrint:: set to 'false' to hide the print icon
334
- # showTabs:: set to 'false' to hide the tabs
335
- # showCalendars:: set to 'false' to hide the calendars selection drop down
336
- # showTimezone:: set to 'false' to hide the timezone selection
337
- # border:: the border width in pixels
338
- # dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'. Example: 20090820/20091001
339
- # privateKey:: use to display a private calendar. You can find this key under the calendar settings pane of the Google Calendar website.
340
- def to_iframe(params = {})
341
- if not self.id
342
- raise "The calendar must exist and be saved before you can use this method."
270
+
271
+ #Helper function to return a formatted iframe embedded google calendar. Parameters are:
272
+ #1. *params*: a hash of parameters that affect the display of the embedded calendar. Accepts any parameter that the google iframe recognizes. Here are the most common:
273
+ # height:: the height of the embedded calendar in pixels
274
+ # width:: the width of the embedded calendar in pixels
275
+ # title:: the title to display
276
+ # bgcolor:: the background color. Limited choices, see google docs for allowable values.
277
+ # color:: the color of the calendar elements. Limited choices, see google docs for allowable values.
278
+ # showTitle:: set to '0' to hide the title
279
+ # showDate:: set to '0' to hide the current date
280
+ # showNav:: set to '0 to hide the navigation tools
281
+ # showPrint:: set to '0' to hide the print icon
282
+ # showTabs:: set to '0' to hide the tabs
283
+ # showCalendars:: set to '0' to hide the calendars selection drop down
284
+ # showTz:: set to '0' to hide the timezone selection
285
+ # border:: the border width in pixels
286
+ # dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'. Example: 20090820/20091001
287
+ # privateKey:: use to display a private calendar. You can find this key under the calendar settings pane of the Google Calendar website.
288
+ # ctz:: The timezone to convert event times to
289
+ def to_iframe(params = {})
290
+ params[:height] ||= "600"
291
+ params[:width] ||= "600"
292
+ params[:title] ||= (self.account ? self.account : '')
293
+ params[:bgcolor] ||= "#FFFFFF"
294
+ params[:color] ||= "#2952A3"
295
+ params[:border] ||= "0"
296
+ puts "params = #{params.inspect}" if self.debug
297
+ output = "?#{params.to_a.collect{|a| a.join("=")}.join("&")}&"
298
+ puts "param_string = #{output}" if self.debug
299
+
300
+ output += "src=#{@id}&color=#{params[:color]}"
301
+
302
+ "<iframe src='http://www.google.com/calendar/embed?#{CGI.escape(output)}' style='#{params[:border]} px solid;' width='#{params[:width]}' height='#{params[:height]}' frameborder='#{params[:border]}' scrolling='no'></iframe>"
343
303
  end
344
- params[:id] = self.id
345
- params[:height] ||= "600"
346
- params[:width] ||= "600"
347
- params[:bgcolor] ||= "#FFFFFF"
348
- params[:color] ||= "#2952A3"
349
- params[:showTitle] = params[:showTitle] == false ? "showTitle=0" : ''
350
- params[:showNav] = params[:showNav] == false ? "showNav=0" : ''
351
- params[:showDate] = params[:showDate] == false ? "showDate=0" : ''
352
- params[:showPrint] = params[:showPrint] == false ? "showPrint=0" : ''
353
- params[:showTabs] = params[:showTabs] == false ? "showTabs=0" : ''
354
- params[:showCalendars] = params[:showCalendars] == false ? "showCalendars=0" : ''
355
- params[:showTimezone] = params[:showTimezone] == false ? 'showTz=0' : ''
356
- params[:border] ||= "0"
357
- output = ''
358
- params.each do |key, value|
359
- case key
360
- when :height then output += "height=#{value}"
361
- when :width then output += "width=#{value}"
362
- when :title then output += "title=#{CGI.escape(value)}"
363
- when :bgcolor then output += "bgcolor=#{CGI.escape(value)}"
364
- when :showTitle then output += value
365
- when :showDate then output += value
366
- when :showNav then output += value
367
- when :showPrint then output += value
368
- when :showTabs then output += value
369
- when :showCalendars then output += value
370
- when :showTimezone then output += value
371
- when :viewMode then output += "mode=#{value}"
372
- when :dates then output += "dates=#{CGI.escape(value)}"
373
- when :privateKey then output += "pvttk=#{value}"
374
- end
375
- output += "&amp;"
304
+
305
+ #Helper function to return a specified calendar id as a formatted iframe embedded google calendar. This function does not require loading the calendar information from the Google calendar
306
+ #service, but does require you know the google calendar id.
307
+ #1. *id*: the unique google assigned id for the calendar to display.
308
+ #2. *params*: a hash of parameters that affect the display of the embedded calendar. Accepts any parameter that the google iframe recognizes. Here are the most common:
309
+ # height:: the height of the embedded calendar in pixels
310
+ # width:: the width of the embedded calendar in pixels
311
+ # title:: the title to display
312
+ # bgcolor:: the background color. Limited choices, see google docs for allowable values.
313
+ # color:: the color of the calendar elements. Limited choices, see google docs for allowable values.
314
+ # showTitle:: set to '0' to hide the title
315
+ # showDate:: set to '0' to hide the current date
316
+ # showNav:: set to '0 to hide the navigation tools
317
+ # showPrint:: set to '0' to hide the print icon
318
+ # showTabs:: set to '0' to hide the tabs
319
+ # showCalendars:: set to '0' to hide the calendars selection drop down
320
+ # showTz:: set to '0' to hide the timezone selection
321
+ # border:: the border width in pixels
322
+ # dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'. Example: 20090820/20091001
323
+ # privateKey:: use to display a private calendar. You can find this key under the calendar settings pane of the Google Calendar website.
324
+ def self.to_iframe(id, params = {})
325
+ params[:height] ||= "600"
326
+ params[:width] ||= "600"
327
+ params[:title] ||= (self.account ? self.account : '')
328
+ params[:bgcolor] ||= "#FFFFFF"
329
+ params[:color] ||= "#2952A3"
330
+ params[:border] ||= "0"
331
+ puts "params = #{params.inspect}" if self.debug
332
+ output = "?#{params.to_a.collect{|a| a.join("=")}.join("&")}&"
333
+ puts "param_string = #{output}" if self.debug
334
+
335
+ output += "src=#{id}&color=#{params[:color]}"
336
+
337
+ "<iframe src='http://www.google.com/calendar/embed?#{CGI.escape(output)}' style='#{params[:border]} px solid;' width='#{params[:width]}' height='#{params[:height]}' frameborder='#{params[:border]}' scrolling='no'></iframe>"
376
338
  end
377
339
 
378
- output += "src=#{params[:id]}&amp;color=#{CGI.escape(params[:color])}"
379
-
380
- "<iframe src='http://www.google.com/calendar/embed?#{output}' style='#{params[:border]} px solid;' width='#{params[:width]}' height='#{params[:height]}' frameborder='#{params[:border]}' scrolling='no'></iframe>"
381
- end
382
-
383
- #Helper function to return a specified calendar id as a formatted iframe embedded google calendar. This function does not require loading the calendar information from the Google calendar
384
- #service, but does require you know the google calendar id.
385
- #1. *id*: the unique google assigned id for the calendar to display.
386
- #2. *params*: a hash of parameters that affect the display of the embedded calendar:
387
- # height:: the height of the embedded calendar in pixels
388
- # width:: the width of the embedded calendar in pixels
389
- # title:: the title to display
390
- # bgcolor:: the background color. Limited choices, see google docs for allowable values.
391
- # color:: the color of the calendar elements. Limited choices, see google docs for allowable values.
392
- # showTitle:: set to 'false' to hide the title
393
- # showDate:: set to 'false' to hide the current date
394
- # showNav:: set to 'false to hide the navigation tools
395
- # showPrint:: set to 'false' to hide the print icon
396
- # showTabs:: set to 'false' to hide the tabs
397
- # showCalendars:: set to 'false' to hide the calendars selection drop down
398
- # showTimezone:: set to 'false' to hide the timezone selection
399
- # border:: the border width in pixels
400
- # dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'. Example: 20090820/20091001
401
- # privateKey:: use to display a private calendar. You can find this key under the calendar settings pane of the Google Calendar website.
402
- def self.to_iframe(id, params = {})
403
- params[:id] = id
404
- params[:height] ||= "600"
405
- params[:width] ||= "600"
406
- params[:bgcolor] ||= "#FFFFFF"
407
- params[:color] ||= "#2952A3"
408
- params[:showTitle] = params[:showTitle] == false ? "showTitle=0" : ''
409
- params[:showNav] = params[:showNav] == false ? "showNav=0" : ''
410
- params[:showDate] = params[:showDate] == false ? "showDate=0" : ''
411
- params[:showPrint] = params[:showPrint] == false ? "showPrint=0" : ''
412
- params[:showTabs] = params[:showTabs] == false ? "showTabs=0" : ''
413
- params[:showCalendars] = params[:showCalendars] == false ? "showCalendars=0" : ''
414
- params[:showTimezone] = params[:showTimezone] == false ? 'showTz=0' : ''
415
- params[:border] ||= "0"
416
- output = ''
417
- params.each do |key, value|
418
- case key
419
- when :height then output += "height=#{value}"
420
- when :width then output += "width=#{value}"
421
- when :title then output += "title=#{CGI.escape(value)}"
422
- when :bgcolor then output += "bgcolor=#{CGI.escape(value)}"
423
- when :showTitle then output += value
424
- when :showDate then output += value
425
- when :showNav then output += value
426
- when :showPrint then output += value
427
- when :showTabs then output += value
428
- when :showCalendars then output += value
429
- when :showTimezone then output += value
430
- when :viewMode then output += "mode=#{value}"
431
- when :dates then output += "dates=#{CGI.escape(value)}"
432
- when :privateKey then output += "pvttk=#{value}"
340
+ private
341
+ def self.get_instance(service, d)
342
+ if d.is_a? Net::HTTPOK
343
+ xml = REXML::Document.new(d.read_body).root
344
+ if xml.name == 'feed'
345
+ xml = xml.elements.each("entry"){}[0]
346
+ end
347
+ else
348
+ xml = d
433
349
  end
434
- output += "&amp;"
350
+ ele = GData4Ruby::Utils::add_namespaces(xml)
351
+ cal = Calendar.new(service)
352
+ cal.load(ele.to_s)
353
+ cal
435
354
  end
436
-
437
- output += "src=#{params[:id]}&amp;color=#{CGI.escape(params[:color])}"
438
-
439
- "<iframe src='http://www.google.com/calendar/embed?#{output}' style='#{params[:border]} px solid;' width='#{params[:width]}' height='#{params[:height]}' frameborder='#{params[:border]}' scrolling='no'></iframe>"
440
- end
441
-
442
- private
443
- @xml
444
- @exists = false
445
- @public = false
446
- @event_feed = ''
447
- @edit_feed = ''
448
-
449
- end
450
-
355
+ end
451
356
  end