Completed
Push — master ( f107d5...0e20f4 )
by John
11:59 queued 06:46
created

ExtensionPresence.enable_dnd_department_calls()   A

Complexity

Conditions 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 14
rs 9.4285
1
module RingCentralSdk::Helpers
2
  class ExtensionPresence
3
4
    attr_accessor :rc_api
5
    attr_accessor :account_id
6
    attr_accessor :exension_id
7
    attr_accessor :presence_info
8
9
    def initialize(rc_api, extension_id=nil)
10
      @rc_api = rc_api
11
      @account_id = '~'
12
      @extension_id = extension_id.to_s
13
      @presence_info = {}
14
    end
15
16
    def retrieve()
17
      if @extension_id !~ /^[0-9]+$/
18
        raise "extension_id is not an integer"
19
      end
20
21
      res = @rc_api.client.get do |req|
22
        req.url "account/#{@account_id}/extension/#{@extension_id}/presence"
23
      end
24
25
      @presence_info = res.body
26
27
      return @presence_info
28
    end
29
30
    def enable_department_calls()
31
      retrieve()
32
33
      if !@presence_info.has_key?('dndStatus')
34
        raise 'invalid presence info'
35
      end
36
37
      current_status = @presence_info['dndStatus']
38
      new_status = status_enable_dnd_department_calls(current_status)
39
40
      if current_status != new_status
41
        update({:dndStatus => new_status})
42
      end
43
    end
44
45
    def disable_department_calls()
46
      retrieve()
47
48
      if !@presence_info.has_key?('dndStatus')
49
        raise 'invalid presence info'
50
      end
51
52
      current_status = @presence_info['dndStatus']
53
      new_status = status_disable_dnd_department_calls(current_status)
54
55
      if current_status != new_status
56
        update({:dndStatus => new_status})
57
      end
58
    end
59
60
    def update(body=nil)
61
      if body.nil?
62
        raise 'HTTP request body is required to update presence'
63
      end
64
65
      res = @rc_api.client.put do |req|
66
        req.url "account/#{@account_id}/extension/#{@extension_id}/presence"
67
        req.headers['Content-Type'] = 'application/json'
68
        req.body = body
69
      end
70
      
71
      @presence_info = res.body
72
73
      return @presence_info
74
    end
75
76
    def status_enable_dnd_department_calls(current_status)
77
      new_status = current_status
78
79
      new_statuses = {
80
        'DoNotAcceptAnyCalls' => 'TakeDepartmentCallsOnly',
81
        'DoNotAcceptDepartmentCalls' => 'TakeAllCalls'
82
      }
83
84
      if new_statuses.has_key?(current_status.to_s)
85
        new_status = new_statuses[current_status.to_s]
86
      end
87
88
      return new_status
89
    end
90
91
    def status_disable_dnd_department_calls(current_status)
92
      new_status = current_status
93
94
      new_statuses = {
95
        'TakeAllCalls' => 'DoNotAcceptDepartmentCalls',
96
        'TakeDepartmentCallsOnly' => 'DoNotAcceptAnyCalls'
97
      }
98
99
      if new_statuses.has_key?(current_status.to_s)
100
        new_status = new_statuses[current_status.to_s]
101
      end
102
103
      return new_status
104
    end
105
106
  end
107
end