Class: GoogleAppsApi::Calendar::Api

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

Instance Attribute Summary collapse

Attributes inherited from BaseApi

#domain

Instance Method Summary collapse

Methods inherited from BaseApi

#entity

Constructor Details

#initialize(*args) ⇒ Api

Returns a new instance of Api.



9
10
11
# File 'lib/google_apps_api/calendar.rb', line 9

def initialize(*args)
  super(:calendar, *args)
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/google_apps_api/calendar.rb', line 7

def token
  @token
end

Instance Method Details

#add_calendar_to_user(calendar, user, *args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/google_apps_api/calendar.rb', line 65

def add_calendar_to_user(calendar, user, *args)
  req = <<-DESCXML
    <entry xmlns='http://www.w3.org/2005/Atom'> 
      <id>#{calendar.full_id_escaped}</id>
    </entry>
  DESCXML
  
  options = args.extract_options!.merge(:username => user.full_id_escaped, :body => req.strip)
  request(:add_calendar_to_user, options)
end

#create_calendar_acl(acl, *args) ⇒ Object

generally, use set_calendar_acl, it works for both creates and updates this will throw an exception “Version Conflict” if it already exists



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/google_apps_api/calendar.rb', line 149

def create_calendar_acl(acl, *args)
  req = <<-DESCXML
  <?xml version="1.0" encoding="UTF-8"?>
  <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>
    <category scheme='http://schemas.google.com/g/2005#kind'
      term='http://schemas.google.com/acl/2007#accessRule'/>
    <gAcl:scope type='#{acl.scope.kind}' value='#{acl.scope.full_id}'></gAcl:scope>
    <gAcl:role
      value='http://schemas.google.com/gCal/2005##{acl.role}'>
    </gAcl:role>
  </entry>
  DESCXML
  options = args.extract_options!.merge(:calendar => acl.calendar.full_id_escaped, :body => req.strip) 
  request(:create_calendar_acl, options)
end

#remove_calendar_acl(acl, *args) ⇒ Object

you can substitute set_calendar_acl with role set to none



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

def remove_calendar_acl(acl, *args)
  options = args.extract_options!.merge(:calendar => acl.calendar.full_id_escaped, :scope => acl.scope.qualified_id_escaped)
  request(:remove_calendar_acl, options)
end

#remove_calendar_from_user(calendar, user, *args) ⇒ Object



77
78
79
80
# File 'lib/google_apps_api/calendar.rb', line 77

def remove_calendar_from_user(calendar, user, *args)
  options = args.extract_options!.merge(:username => user.full_id_escaped, :calendar => calendar.full_id_escaped)
  request(:remove_calendar_from_user, options)
end

#retrieve_acls_for_calendar(calendar, *args) ⇒ Object

returns array of acls for a given calendar



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

def retrieve_acls_for_calendar(calendar, *args)
  options = args.extract_options!.merge(:calendar => calendar.full_id_escaped)
  request(:retrieve_acls_for_calendar, options)
end

#retrieve_calendar_for_user(calendar, user, *args) ⇒ Object

lists all calendards for a user



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/google_apps_api/calendar.rb', line 123

def retrieve_calendar_for_user(calendar, user, *args)
  options = args.extract_options!.merge(:calendar => calendar.full_id_escaped, :username => user.full_id_escaped)
  retries = options[:retries] || 5

  while (retries > 0)
    begin
      return request(:retrieve_calendar_for_user, options)
    rescue GDataError => g
      retries -= 1
      sleep 0.5
    end
  end
  
  return nil
end

#retrieve_calendars_for_user(user, *args) ⇒ Object

lists all calendards for a user



117
118
119
120
# File 'lib/google_apps_api/calendar.rb', line 117

def retrieve_calendars_for_user(user, *args)
  options = args.extract_options!.merge(:username => user.full_id_escaped)
  request(:retrieve_calendars_for_user, options)
end

#set_calendar_acl(acl, *args) ⇒ Object

updates a given acl for a given scope



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/google_apps_api/calendar.rb', line 173

def set_calendar_acl(acl, *args)
  req = <<-DESCXML
  <?xml version="1.0" encoding="UTF-8"?>
  <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'
  xmlns:gd='http://schemas.google.com/g/2005'
    gd:etag='W/"DU4ERH47eCp7ImA9WxRVEkQ."'>
    <category scheme='http://schemas.google.com/g/2005#kind'
      term='http://schemas.google.com/acl/2007#accessRule'/>
    <gAcl:scope type='#{acl.scope.kind}' value='#{acl.scope.full_id}'></gAcl:scope>
    <gAcl:role
      value='#{acl.role_schema}'>
    </gAcl:role>
  </entry>
  DESCXML
  
  options = args.extract_options!.merge(:calendar => acl.calendar.full_id_escaped, :scope => acl.scope.qualified_id_escaped, :body => req.strip) 
  request(:set_calendar_acl, options)
end

#set_calendar_for_user(calendar, user, *args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/google_apps_api/calendar.rb', line 13

def set_calendar_for_user(calendar, user, *args)
  options = args.extract_options!
  
  acl_ranking = ["none", "freebusy", "read", "editor", "owner", "root"]
  acl_comparison = options.delete(:acl_comparison) || :set
  
  do_not_subscribe = options.delete(:do_not_subscribe) || false
  
  existing_acl = "none"
  need_to_create = false
  
  existing_cal = retrieve_calendar_for_user(calendar, user)
  if existing_cal
    existing_acl = existing_cal.details[:accesslevel]
  else
    need_to_create = true unless do_not_subscribe
  end
  
  existing_acl_rank = acl_ranking.index(existing_acl) || raise("invalid existing acl")        
  
  acl = options.delete(:accesslevel) || existing_acl
  acl_rank = acl_ranking.index(acl) || raise("new acl invalid")
  
  if need_to_create 
    raise "Must set accesslevel for a newly added calendar" unless acl
    owner_acl = CalendarAcl.new(:calendar => calendar, :scope => user, :role => "owner")
    set_calendar_acl(owner_acl)
    add_calendar_to_user(calendar, user)
  end
  
  if acl == "none" 
    remove_calendar_from_user(calendar, user) unless existing_acl == "none"
  else

    set_acl = case acl_comparison
    when :set
      true
    when :set_if_higher
      acl_rank > existing_acl_rank
    when :set_if_lower
      acl_rank < existing_acl_rank
    end


    set_calendar_acl(CalendarAcl.new(:calendar => calendar, :scope => user, :role => acl)) if set_acl
    if !options.empty? && (existing_cal || need_to_create)
      
      update_calendar_for_user(calendar, user, options) 
    end
  end
end

#update_calendar_for_user(calendar, user, *args) ⇒ Object



83
84
85
86
87
88
89
90
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/google_apps_api/calendar.rb', line 83

def update_calendar_for_user(calendar, user, *args)

  username = user.full_id_escaped

  cal = nil
  cal = retrieve_calendar_for_user(calendar, user) 
  
  cal_id = cal.full_id_escaped

  options = args.extract_options!.merge(:username => username, :calendar => cal_id)
  
  details = cal.details.merge(options)


  req = <<-DESCXML
  <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gCal='http://schemas.google.com/gCal/2005' xmlns:gd='http://schemas.google.com/g/2005'>
    <id>http://www.google.com/calendar/feeds/#{username}/allcalendars/full/#{cal_id}</id>

    <title type='text'>#{details[:title]}</title>
    <summary type='text'>#{details[:summary]}</summary>
    <gCal:timezone value='#{details[:timezone]}'/>
    <gCal:hidden value='#{details[:hidden].to_s}'/>
    <gCal:color value='#{details[:color]}'/>
    <gCal:selected value='#{details[:selected].to_s}'/>
    <gd:where valueString='#{details[:where]}'/>
  </entry>

  DESCXML
  
  options[:body] = req.strip
  request(:update_calendar_for_user, options)
end