Class: GCal4Ruby::Calendar

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

Overview

to the Google Calendar service.

Constant Summary collapse

CALENDAR_FEED =
"http://www.google.com/calendar/feeds/default/owncalendars/full"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ Calendar

Accepts a Service object. Returns the new Calendar if successful, otherwise raises the InvalidService error.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/gcal4ruby/calendar.rb', line 118

def initialize(service)
  super()
  if !service.is_a?(Service)
    raise InvalidService
  end
  @xml = CALENDAR_XML
  @service = service
  @exists = false
  @title = ""
  @summary = ""
  @public = false
  @id = nil
  @hidden = false
  @timezone = "America/Los_Angeles"
  @color = "#2952A3"
  @where = ""
  return true
end

Instance Attribute Details

#colorObject

The calendar color. Must be one of these values.



47
48
49
# File 'lib/gcal4ruby/calendar.rb', line 47

def color
  @color
end

#editableObject (readonly)

A flag indicating whether the calendar is editable by this account



59
60
61
# File 'lib/gcal4ruby/calendar.rb', line 59

def editable
  @editable
end

#event_feedObject (readonly)

The event feed for the calendar



56
57
58
# File 'lib/gcal4ruby/calendar.rb', line 56

def event_feed
  @event_feed
end

#hiddenObject

Boolean value indicating the calendar visibility



41
42
43
# File 'lib/gcal4ruby/calendar.rb', line 41

def hidden
  @hidden
end

#idObject (readonly)

The unique calendar id



38
39
40
# File 'lib/gcal4ruby/calendar.rb', line 38

def id
  @id
end

#selectedObject

A boolean value indicating whether the calendar appears by default when viewed online



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

def selected
  @selected
end

#serviceObject (readonly)

The parent Service object passed on initialization



35
36
37
# File 'lib/gcal4ruby/calendar.rb', line 35

def service
  @service
end

#summaryObject

A short description of the calendar



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

def summary
  @summary
end

#timezoneObject

The calendar timezone



44
45
46
# File 'lib/gcal4ruby/calendar.rb', line 44

def timezone
  @timezone
end

#titleObject

The calendar title



29
30
31
# File 'lib/gcal4ruby/calendar.rb', line 29

def title
  @title
end

#whereObject

The calendar geo location, if any



50
51
52
# File 'lib/gcal4ruby/calendar.rb', line 50

def where
  @where
end

Class Method Details

.find(service, query_term = nil, params = {}) ⇒ Object

Class method for querying the google service for specific calendars. The service parameter should be an appropriately authenticated Service. The term parameter can be any string. The scope parameter may be either :all to return an array of matches, or :first to return the first match as a Calendar object.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/gcal4ruby/calendar.rb', line 182

def self.find(service, query_term=nil, params = {})
  t = query_term.downcase
  cals = service.calendars
  ret = []
  cals.each do |cal|
    title = cal.title || ""
    summary = cal.summary || ""
    id = cal.id || ""
    if id == query_term
      return cal
    end
    if title.downcase.match(t) or summary.downcase.match(t)
      if params[:scope] == :first
        return cal
      else
        ret << cal
      end
    end
  end
  ret
end

.get(service, id) ⇒ Object



204
205
206
207
208
209
# File 'lib/gcal4ruby/calendar.rb', line 204

def self.get(service, id)
  url = 'http://www.google.com/calendar/feeds/default/allcalendars/full/'+id
  ret = service.send_get(url)
  puts "==return=="
  puts ret.body
end

.query(service, query_term) ⇒ Object



211
212
213
214
215
216
# File 'lib/gcal4ruby/calendar.rb', line 211

def self.query(service, query_term)
  url = 'http://www.google.com/calendar/feeds/default/allcalendars/full'+"?q="+CGI.escape(query_term)
  ret = service.send_get(url)
  puts "==return=="
  puts ret.body
end

.to_iframe(id, params = {}) ⇒ Object

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 service, but does require you know the google calendar id.

  1. id: the unique google assigned id for the calendar to display.

  2. params: a hash of parameters that affect the display of the embedded calendar:

height:: the height of the embedded calendar in pixels
width:: the width of the embedded calendar in pixels
title:: the title to display
bgcolor:: the background color.  Limited choices, see google docs for allowable values.
color:: the color of the calendar elements.  Limited choices, see google docs for allowable values.
showTitle:: set to 'false' to hide the title
showDate:: set to 'false' to hide the current date
showNav:: set to 'false to hide the navigation tools
showPrint:: set to 'false' to hide the print icon
showTabs:: set to 'false' to hide the tabs
showCalendars:: set to 'false' to hide the calendars selection drop down
showTimezone:: set to 'false' to hide the timezone selection
border:: the border width in pixels
dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'.  Example: 20090820/20091001
privateKey:: use to display a private calendar.  You can find this key under the calendar settings pane of the Google Calendar website.


396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/gcal4ruby/calendar.rb', line 396

def self.to_iframe(id, params = {})
  params[:id] = id
  params[:height] ||= "600"
  params[:width] ||= "600"
  params[:bgcolor] ||= "#FFFFFF"
  params[:color] ||= "#2952A3"
  params[:showTitle] = params[:showTitle] == false ? "showTitle=0" : ''
  params[:showNav] = params[:showNav] == false ? "showNav=0" : ''
  params[:showDate] = params[:showDate] == false ? "showDate=0" : ''
  params[:showPrint] = params[:showPrint] == false ? "showPrint=0" : ''
  params[:showTabs] = params[:showTabs] == false ? "showTabs=0" : ''
  params[:showCalendars] = params[:showCalendars] == false ? "showCalendars=0" : ''
  params[:showTimezone] = params[:showTimezone] == false ? 'showTz=0' : ''
  params[:border] ||= "0"
  output = ''
  params.each do |key, value|
    case key
      when :height then output += "height=#{value}"
      when :width then output += "width=#{value}"
      when :title then output += "title=#{CGI.escape(value)}"
      when :bgcolor then output += "bgcolor=#{CGI.escape(value)}"
      when :showTitle then output += value
      when :showDate then output += value
      when :showNav then output += value
      when :showPrint then output += value
      when :showTabs then output += value
      when :showCalendars then output += value
      when :showTimezone then output += value
      when :viewMode then output += "mode=#{value}"
      when :dates then output += "dates=#{CGI.escape(value)}"
      when :privateKey then output += "pvttk=#{value}"
    end
    output += "&amp;"
  end

  output += "src=#{params[:id]}&amp;color=#{CGI.escape(params[:color])}"
      
  "<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>"  
end

Instance Method Details

#deleteObject

Deletes a calendar. If successful, returns true, otherwise false. If successful, the calendar object is cleared.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/gcal4ruby/calendar.rb', line 139

def delete
  if @exists    
    if @service.send_delete(CALENDAR_FEED+"/"+@id)
      @exists = false
      @title = nil
      @summary = nil
      @public = false
      @id = nil
      @hidden = false
      @timezone = nil
      @color = nil
      @where = nil
      return true
    else
      return false
    end
  else
    return false
  end
end

#eventsObject

Returns an array of Event objects corresponding to each event in the calendar.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gcal4ruby/calendar.rb', line 73

def events
  events = []
  ret = @service.send_get(@event_feed)
  REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
    entry.attributes["xmlns:gCal"] = "http://schemas.google.com/gCal/2005"
    entry.attributes["xmlns:gd"] = "http://schemas.google.com/g/2005"
    entry.attributes["xmlns"] = "http://www.w3.org/2005/Atom"
    e = Event.new(self)
    if e.load(entry.to_s)
      events << e
    end
  end
  return events
end

#exists?Boolean

Returns true if the calendar exists on the Google Calendar system (i.e. was loaded or has been saved). Otherwise returns false.

Returns:

  • (Boolean)


63
64
65
# File 'lib/gcal4ruby/calendar.rb', line 63

def exists?
  return @exists
end

#load(string) ⇒ Object

Loads the Calendar with returned data from Google Calendar feed. Returns true if successful.



255
256
257
258
259
260
261
262
263
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/gcal4ruby/calendar.rb', line 255

def load(string)
  @exists = true
  @xml = string
  xml = REXML::Document.new(string)
  xml.root.elements.each(){}.map do |ele|
    case ele.name
      when "id"
        @id = ele.text.gsub("http://www.google.com/calendar/feeds/default/calendars/", "")
      when 'title'
        @title = ele.text
      when 'summary'
        @summary = ele.text
      when "color"
        @color = ele.attributes['value']
      when 'hidden'
        @hidden = ele.attributes["value"] == "true" ? true : false
      when 'timezone'
        @timezone = ele.attributes["value"]
      when "selected"
        @selected = ele.attributes["value"] == "true" ? true : false
      when "link"
        if ele.attributes['rel'] == 'edit'
          @edit_feed = ele.attributes['href']
        end
    end
  end
  
  @event_feed = "http://www.google.com/calendar/feeds/#{@id}/private/full"
  
  if @service.check_public
    puts "Getting ACL Feed" if @service.debug
    
    #rescue error on shared calenar ACL list access
    begin 
      ret = @service.send_get("http://www.google.com/calendar/feeds/#{@id}/acl/full/")
    rescue Exception => e
      @public = false
      @editable = false
      return true
    end
    @editable = true
    r = REXML::Document.new(ret.read_body)
    r.root.elements.each("entry") do |ele|
      ele.elements.each do |e|
        #puts "e = "+e.to_s if @service.debug
        #puts "previous element = "+e.previous_element.to_s if @service.debug
        if e.name == 'role' and e.previous_element.name == 'scope' and e.previous_element.attributes['type'] == 'default'
          if e.attributes['value'].match('#read')
            @public = true
          else
            @public = false
          end
        end
      end
    end
  else
    @public = false
    @editable = true
  end
  return true
end

#public=(p) ⇒ Object

Set the calendar to public (p = true) or private (p = false). Publically viewable calendars can be accessed by anyone without having to log in to google calendar. See Calendar#to_iframe for options to display a public calendar in a webpage.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gcal4ruby/calendar.rb', line 91

def public=(p)
  if p
    permissions = 'http://schemas.google.com/gCal/2005#read' 
  else
    permissions = 'none'
  end
  
  #if p != @public
    path = "http://www.google.com/calendar/feeds/#{@id}/acl/full/default"
    request = REXML::Document.new(ACL_XML)
    request.root.elements.each() do |ele|
      if ele.name == 'role'
        ele.attributes['value'] = permissions
      end
      
    end
    if @service.send_put(path, request.to_s, {"Content-Type" => "application/atom+xml", "Content-Length" => request.length.to_s})
      @public = p
      return true
    else
      return false
    end
  #end
end

#public?Boolean

Returns true if the calendar is publically accessable, otherwise returns false.

Returns:

  • (Boolean)


68
69
70
# File 'lib/gcal4ruby/calendar.rb', line 68

def public?
  return @public
end

#reloadObject

Reloads the calendar objects information from the stored server version. Returns true if successful, otherwise returns false. Any information not saved will be overwritten.



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/gcal4ruby/calendar.rb', line 220

def reload
  if not @exists
    return false
  end  
  t = Calendar.find(service, @id, :first)
  if t
    load(t.to_xml)
  else
    return false
  end
end

#saveObject

If the calendar does not exist, creates it, otherwise updates the calendar info. Returns true if the save is successful, otherwise false.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/gcal4ruby/calendar.rb', line 162

def save
  if @exists
    ret = service.send_put(@edit_feed, to_xml(), {'Content-Type' => 'application/atom+xml'})
  else
    ret = service.send_post(CALENDAR_FEED, to_xml(), {'Content-Type' => 'application/atom+xml'})
  end
  if !@exists
    if load(ret.read_body)
      return true
    else
      raise CalendarSaveFailed
    end
  end
  return true
end

#to_iframe(params = {}) ⇒ Object

Helper function to return the currently loaded calendar formatted iframe embedded google calendar.

  1. params: a hash of parameters that affect the display of the embedded calendar:

height:: the height of the embedded calendar in pixels
width:: the width of the embedded calendar in pixels
title:: the title to display
bgcolor:: the background color.  Limited choices, see google docs for allowable values.
color:: the color of the calendar elements.  Limited choices, see google docs for allowable values.
showTitle:: set to 'false' to hide the title
showDate:: set to 'false' to hide the current date
showNav:: set to 'false to hide the navigation tools
showPrint:: set to 'false' to hide the print icon
showTabs:: set to 'false' to hide the tabs
showCalendars:: set to 'false' to hide the calendars selection drop down
showTimezone:: set to 'false' to hide the timezone selection
border:: the border width in pixels
dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'.  Example: 20090820/20091001
privateKey:: use to display a private calendar.  You can find this key under the calendar settings pane of the Google Calendar website.


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/gcal4ruby/calendar.rb', line 334

def to_iframe(params = {})
  if not self.id
    raise "The calendar must exist and be saved before you can use this method."
  end
  params[:id] = self.id
  params[:height] ||= "600"
  params[:width] ||= "600"
  params[:bgcolor] ||= "#FFFFFF"
  params[:color] ||= "#2952A3"
  params[:showTitle] = params[:showTitle] == false ? "showTitle=0" : ''
  params[:showNav] = params[:showNav] == false ? "showNav=0" : ''
  params[:showDate] = params[:showDate] == false ? "showDate=0" : ''
  params[:showPrint] = params[:showPrint] == false ? "showPrint=0" : ''
  params[:showTabs] = params[:showTabs] == false ? "showTabs=0" : ''
  params[:showCalendars] = params[:showCalendars] == false ? "showCalendars=0" : ''
  params[:showTimezone] = params[:showTimezone] == false ? 'showTz=0' : ''
  params[:border] ||= "0"
  output = ''
  params.each do |key, value|
    case key
      when :height then output += "height=#{value}"
      when :width then output += "width=#{value}"
      when :title then output += "title=#{CGI.escape(value)}"
      when :bgcolor then output += "bgcolor=#{CGI.escape(value)}"
      when :showTitle then output += value
      when :showDate then output += value
      when :showNav then output += value
      when :showPrint then output += value
      when :showTabs then output += value
      when :showCalendars then output += value
      when :showTimezone then output += value
      when :viewMode then output += "mode=#{value}"
      when :dates then output += "dates=#{CGI.escape(value)}"
      when :privateKey then output += "pvttk=#{value}"
    end
    output += "&amp;"
  end

  output += "src=#{params[:id]}&amp;color=#{CGI.escape(params[:color])}"
      
  "<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>"  
end

#to_xmlObject

Returns the xml representation of the Calenar.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/gcal4ruby/calendar.rb', line 233

def to_xml
  xml = REXML::Document.new(@xml)
  xml.root.elements.each(){}.map do |ele|
    case ele.name
    when "title"
      ele.text = @title
    when "summary"
      ele.text = @summary
    when "timezone"
      ele.attributes["value"] = @timezone
    when "hidden"
      ele.attributes["value"] = @hidden.to_s
    when "color"
      ele.attributes["value"] = @color
    when "selected"
      ele.attributes["value"] = @selected.to_s
    end
  end
  xml.to_s
end