class RingCentralSdk::REST::ExtensionPresence

ExtensionPresence is a helper class to manage presence info

Attributes

account_id[RW]
client[RW]
extension_id[RW]
presence_data[RW]

Public Class Methods

new(extension_id, opts = {}) click to toggle source
# File lib/ringcentral_sdk/rest/extension_presence.rb, line 10
def initialize(extension_id, opts = {})
  @client = opts.key?(:client) ? opts[:client] : nil
  @account_id = '~'
  @extension_id = extension_id.to_s
  @presence_data = {}
end

Public Instance Methods

department_calls_enable(enable) click to toggle source
# File lib/ringcentral_sdk/rest/extension_presence.rb, line 29
def department_calls_enable(enable)
  retrieve

  unless @presence.is_a?(Hash) && @presence_data.key?('dndStatus')
    raise 'invalid presence info'
  end

  current_status = @presence_data['dndStatus']
  new_status = new_status_dnd_department_calls(current_status, enable)

  update(dndStatus: new_status) if current_status != new_status
  new_status
end
department_calls_enabled?(reload = false) click to toggle source
# File lib/ringcentral_sdk/rest/extension_presence.rb, line 43
def department_calls_enabled?(reload = false)
  if reload
    retrieve
  elsif !@presence_data.key?('dndStatus')
    retrieve
  end

  current_status = @presence_data['dndStatus']

  status_enabled = {
    'DoNotAcceptAnyCalls' => false,
    'DoNotAcceptDepartmentCalls' => false,
    'TakeAllCalls' => true,
    'TakeDepartmentCallsOnly' => true
  }

  status_enabled.key?(current_status) ? status_enabled[current_status] : nil
end
new_status_dnd_department_calls(current_status, enable) click to toggle source
# File lib/ringcentral_sdk/rest/extension_presence.rb, line 76
def new_status_dnd_department_calls(current_status, enable)
  new_statuses = {
    enable: {
      'DoNotAcceptAnyCalls' => 'TakeDepartmentCallsOnly',
      'DoNotAcceptDepartmentCalls' => 'TakeAllCalls'
    },
    disable: {
      'TakeAllCalls' => 'DoNotAcceptDepartmentCalls',
      'TakeDepartmentCallsOnly' => 'DoNotAcceptAnyCalls'
    }
  }

  action = enable ? :enable : :disable

  new_status = current_status

  new_status = new_statuses[action][current_status.to_s] \
    if new_statuses[action].key?(current_status.to_s)

  new_status
end
retrieve() click to toggle source
# File lib/ringcentral_sdk/rest/extension_presence.rb, line 17
def retrieve
  raise 'extension_id is not an integer' if @extension_id !~ /^[0-9]+$/

  res = @client.http.get do |req|
    req.url "account/#{@account_id}/extension/#{@extension_id}/presence"
  end

  @presence_data = res.body

  @presence_data
end
update(body = nil) click to toggle source
# File lib/ringcentral_sdk/rest/extension_presence.rb, line 62
def update(body = nil)
  raise 'HTTP request body is required to update presence' if body.nil?

  res = @client.http.put do |req|
    req.url "account/#{@account_id}/extension/#{@extension_id}/presence"
    req.headers['Content-Type'] = 'application/json'
    req.body = body
  end

  @presence_data = res.body

  @presence_data
end