Class: Zimbra::AppointmentService

Inherits:
HandsoapAccountService show all
Defined in:
lib/zimbra/appointment.rb

Defined Under Namespace

Classes: Builder, Parser

Instance Method Summary collapse

Methods inherited from HandsoapAccountService

#on_create_document, #on_response_document

Methods included from HandsoapAccountUriOverrides

#envelope_namespace, #request_content_type, #uri

Methods included from HandsoapAccountNamespaces

#request_namespaces, #response_namespaces

Methods included from HandsoapErrors

#http_error?, #http_not_found?, #on_after_create_http_request, #on_http_error, #report_error, #soap_fault_not_found?

Instance Method Details

#cancel(appointment, invite_id) ⇒ Object



260
261
262
263
264
# File 'lib/zimbra/appointment.rb', line 260

def cancel(appointment, invite_id)
  xml = invoke("n2:CancelAppointmentRequest") do |message|
    Builder.cancel(message, appointment.id, invite_id)
  end
end

#create(appointment) ⇒ Object



244
245
246
247
248
249
250
251
252
# File 'lib/zimbra/appointment.rb', line 244

def create(appointment)
  xml = invoke("n2:CreateAppointmentRequest") do |message|
    Builder.create(message, appointment)
  end
  response_hash = Zimbra::Hash.from_xml(xml.document.to_s)
  id = response_hash[:Envelope][:Body][:CreateAppointmentResponse][:attributes][:apptId] rescue nil
  invite_id = response_hash[:Envelope][:Body][:CreateAppointmentResponse][:attributes][:invId].gsub(/#{id}\-/, '').to_i rescue nil
  { :id => id, :invite_id => invite_id }
end

#find(appointment_id) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/zimbra/appointment.rb', line 236

def find(appointment_id)
  xml = invoke("n2:GetAppointmentRequest") do |message|
    Builder.find_by_id(message, appointment_id)
  end
  return nil unless xml
  Parser.appointment_response(xml/"//n2:appt")
end

#find_all_by_calendar_id(calendar_id) ⇒ Object



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

def find_all_by_calendar_id(calendar_id)
  appointment_attributes = []
  cursor = nil
  while(true) do
    xml = invoke("n2:SearchRequest") do |message|
      Builder.find_all_with_query(message, "inid:#{calendar_id}", cursor)
    end
    
    new_results = Parser.get_search_response(xml)
    
    return appointment_attributes if new_results.empty?
    
    appointment_attributes += new_results
    
    cursor = new_results.last[:appt][:attributes][:id]
  end
end

#find_all_by_calendar_id_since(calendar_id, since_date) ⇒ Object



229
230
231
232
233
234
# File 'lib/zimbra/appointment.rb', line 229

def find_all_by_calendar_id_since(calendar_id, since_date)
   xml = invoke("n2:SearchRequest") do |message|
     Builder.find_all_with_query(message, "inid:#{calendar_id} AND date:>#{since_date.to_i}")
   end
   Parser.get_search_response(xml)
end

#find_all_instance_times_of_an_appointment(appointment) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/zimbra/appointment.rb', line 191

def find_all_instance_times_of_an_appointment(appointment)
  instance_times = []
  
  cursor = nil
  while(true) do
    xml = invoke("n2:SearchRequest") do |message|
      message.set_attr 'query', "date:#{appointment.date.to_i * 1000}"
      message.set_attr 'types', 'appointment'
      message.set_attr 'calExpandInstStart', '1'
      message.set_attr 'calExpandInstEnd', (Time.now + (86400 * 365 * 10)).to_i * 1000

      if cursor
        message.add 'cursor' do |cursor_element|
          cursor_element.set_attr 'id', cursor
        end
      end
    end
    response_hash = Zimbra::Hash.from_xml(xml.document.to_s)
    response_hash = response_hash[:Envelope][:Body][:SearchResponse]

    appointments = if response_hash[:appt].nil?
      []
    elsif response_hash[:appt].is_a?(Array)
      response_hash[:appt]
    else
      [response_hash[:appt]]
    end
    
    return instance_times if appointments.empty?
    
    cursor = appointments.last[:attributes][:id]
    
    appt_hash = appointments.find { |appt| appt[:attributes][:id] == appointment.id }
    instances = appt_hash[:inst].is_a?(Array) ? appt_hash[:inst] : [appt_hash[:inst]]
    instance_times += instances.collect { |inst| Time.at(inst[:attributes][:s] / 1000) }
  end
end

#update(appointment, invite_id) ⇒ Object



254
255
256
257
258
# File 'lib/zimbra/appointment.rb', line 254

def update(appointment, invite_id)
  xml = invoke("n2:ModifyAppointmentRequest") do |message|
    Builder.update(message, appointment, invite_id)
  end
end