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



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



48
49
50
51
52
53
54
55
56
57
# File 'lib/google_apps_api/calendar.rb', line 48

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



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

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



149
150
151
152
# File 'lib/google_apps_api/calendar.rb', line 149

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



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

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



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

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/google_apps_api/calendar.rb', line 106

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



100
101
102
103
# File 'lib/google_apps_api/calendar.rb', line 100

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



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

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
# File 'lib/google_apps_api/calendar.rb', line 13

def set_calendar_for_user(calendar, user, *args)
  options = args.extract_options!
  
  existing_acl = "none"
  need_to_create = false
  
  cal = retrieve_calendar_for_user(calendar, user)
  if cal
    existing_acl = cal.details[:accesslevel]
  else
    need_to_create = true
  end
  
  acl = options.delete(:accesslevel) || existing_acl

  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

    if acl != existing_acl
      set_calendar_acl(CalendarAcl.new(:calendar => calendar, :scope => user, :role => acl))
    end
    update_calendar_for_user(calendar, user, options) unless options.empty?
    
  end
end

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/google_apps_api/calendar.rb', line 66

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