Class: GCal4Ruby::Calendar

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

Constant Summary collapse

GOOGLE_PARAM_NAMES =
{
  :mode => "mode",
  :height => "height",
  :width => "width",
  :bg_color => "bgcolor",
  :color => "color",
  :show_title => "showTitle",
  :show_nav => "showNav",
  :show_date => "showDate",
  :show_print => "showPrint",
  :show_tabs => "showTabs",
  :show_calendars => "showCalendars",
  :show_timezone => "showTimezone"    
}
IFRAME_DEFAULTS =
{
  :mode => "WEEK", 
  :height => "600",
  :width => "600",
  :bg_color => "#FFFFFF",
  :color => "#2852A3",
  :show_title => false,
  :show_nav => true,
  :show_date => true,
  :show_print => true,
  :show_tabs => true,
  :show_calendars => true,
  :show_timezone => true
}
CALENDAR_FEED =
"https://www.google.com/calendar/feeds/default/owncalendars/full"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, attributes = {}) ⇒ Calendar

Returns a new instance of Calendar.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gcal4ruby/calendar.rb', line 40

def initialize(service, attributes = {})
  super()
  @service = service
  attributes.each do |key, value|
    self.send("#{key}=", value)
  end  

  @xml ||= CALENDAR_XML
  @exists = false
  @title ||= ""
  @summary ||= ""
  @public ||= false
  @hidden ||= false
  @timezone ||= "America/Los_Angeles"
  @color ||= "#2952A3"
  @where ||= ""
  # return true
  return self
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



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

def color
  @color
end

#edit_feedObject (readonly)

Returns the value of attribute edit_feed.



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

def edit_feed
  @edit_feed
end

#editableObject (readonly)

Returns the value of attribute editable.



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

def editable
  @editable
end

#event_feedObject (readonly)

Returns the value of attribute event_feed.



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

def event_feed
  @event_feed
end

#hiddenObject

Returns the value of attribute hidden.



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

def hidden
  @hidden
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#selectedObject

Returns the value of attribute selected.



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

def selected
  @selected
end

#self_feedObject (readonly)

Returns the value of attribute self_feed.



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

def self_feed
  @self_feed
end

#serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

#summaryObject

Returns the value of attribute summary.



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

def summary
  @summary
end

#timezoneObject

Returns the value of attribute timezone.



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

def timezone
  @timezone
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#whereObject

Returns the value of attribute where.



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

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.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/gcal4ruby/calendar.rb', line 153

def self.find(service, query_term=nil, params = {})
  t = query_term.downcase if query_term
  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, self_feed) ⇒ Object



175
176
177
178
179
180
# File 'lib/gcal4ruby/calendar.rb', line 175

def self.get(service, self_feed)
  ret = service.send_get(self_feed)
  c = Calendar.new(service)
  c.load(ret.body)
  return c
end

.query(service, query_term) ⇒ Object



182
183
184
185
186
187
# File 'lib/gcal4ruby/calendar.rb', line 182

def self.query(service, query_term)
  url = 'https://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



296
297
298
299
300
301
# File 'lib/gcal4ruby/calendar.rb', line 296

def self.to_iframe(id, params = {})
  raise "Calendar ID is required" unless id
  options = build_options_set(params)
  url_options = options.join("&amp;")
  "<iframe src='http://www.google.com/calendar/embed?src=#{id}&amp;#{output}' width='#{options[:width]}' height='#{options[:height]}' frameborder='0' scrolling='no'></iframe>"  
end

Instance Method Details

#build_options_set(params) ⇒ Object



303
304
305
306
307
308
309
# File 'lib/gcal4ruby/calendar.rb', line 303

def build_options_set(params)
  IFRAME_DEFAULTS.merge(params).collect do |key, value|
    if IFRAME_DEFAULTS.keys.include?(key)
      [GOOGLE_PARAM_NAMES[key], raw_value_to_param_value(value)].join("=")
    end
  end
end

#deleteObject

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gcal4ruby/calendar.rb', line 110

def delete
  if @exists    
    if @service.send_delete(@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

#events(query_hash = nil) ⇒ Object

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gcal4ruby/calendar.rb', line 69

def events(query_hash = nil)
  url = @event_feed
  if query_hash
    url += "?#{query_hash.to_query}"
  end
  events = []
  ret = @service.send_get(url)
  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:app"] = "http://www.w3.org/2007/app"
    entry.attributes["xmlns"] = "http://www.w3.org/2005/Atom"
    entry.attributes["xmlns:georss"] = "http://www.georss.org/georss"
    entry.attributes["xmlns:gml"] = "http://www.opengis.net/gml"
    e = Event.new(self)
    if e.load(entry.to_s)
      events << e
    end
  end
  return events
end

#events_since(time) ⇒ Object



91
92
93
# File 'lib/gcal4ruby/calendar.rb', line 91

def events_since(time)
  events({ 'updated-min' => time.xmlschema })
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  @exists
end

#load(string) ⇒ Object

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
# File 'lib/gcal4ruby/calendar.rb', line 226

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 #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"
        href = ele.attributes['href']
        case ele.attributes['rel']
          when "self" then @self_feed = href
          when "edit" then @edit_feed = href
          when "http://schemas.google.com/gCal/2005#eventFeed" then @event_feed = href
          when "http://schemas.google.com/acl/2007#accessControlList" then @acl_feed = href
        end
    end
  end

  if @service.check_public
    puts "Getting ACL Feed" if @service.debug
  
    #rescue error on shared calenar ACL list access
    begin 
      ret = @service.send_get(@acl_feed)
    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
        #added per eruder http://github.com/h13ronim/gcal4ruby/commit/3074ebde33bd3970500f6de992a66c0a4578062a
        if e.name == 'role' and e.previous_element 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=(param) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gcal4ruby/calendar.rb', line 95

def public=(param)
  permissions = param ? 'http://schemas.google.com/gCal/2005#read' : 'none'  

  path = "https://www.google.com/calendar/feeds/#{@id}/acl/full/default"
  request = REXML::Document.new(ACL_XML)  # What/Where is ACL_XML???
  request.root.elements.each() do |ele|
    if ele.name == 'role'
      ele.attributes['value'] = permissions
    end
  end
  @public = @service.send_put(path, request.to_s, {"Content-Type" => "application/atom+xml", "Content-Length" => request.length.to_s})
end

#public?Boolean

Returns:

  • (Boolean)


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

def public?
  @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.



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/gcal4ruby/calendar.rb', line 191

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.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/gcal4ruby/calendar.rb', line 133

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



291
292
293
294
# File 'lib/gcal4ruby/calendar.rb', line 291

def to_iframe(params = {})
  raise "The calendar must exist and be saved before you can use this method." unless self.id
  GCal4Ruby::Calendar.to_iframe(self.id, params)
end

#to_xmlObject

Returns the xml representation of the Calenar.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/gcal4ruby/calendar.rb', line 204

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