Completed
Push — master ( 0e20f4...381566 )
by John
01:13 queued 17s
created

ExtensionPresence.department_calls_enable()   A

Complexity

Conditions 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 16
rs 9.2
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 department_calls_enable(enable)
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 = enable ?
39
        status_enable_dnd_department_calls(current_status) :
40
        status_disable_dnd_department_calls(current_status)
41
42
      if current_status != new_status
43
        update({:dndStatus => new_status})
44
      end
45
    end
46
47
    def department_calls_enabled?(reload=false)
48
      if reload
49
        retrieve()
50
      elsif !@presence_info.has_key?('dndStatus')
51
        retrieve()
52
      end
53
54
      current_status = @presence_info['dndStatus']
55
56
      status_enabled = {
57
        'DoNotAcceptAnyCalls' => false,
58
        'DoNotAcceptDepartmentCalls' => false,
59
        'TakeAllCalls' => true,
60
        'TakeDepartmentCallsOnly' => true
61
      }
62
63
      return status_enabled.has_key?(current_status) ?
64
        status_enabled[current_status] : nil
65
    end
66
67
    def disable_department_calls()
68
      retrieve()
69
70
      if !@presence_info.has_key?('dndStatus')
71
        raise 'invalid presence info'
72
      end
73
74
      current_status = @presence_info['dndStatus']
75
      new_status = status_disable_dnd_department_calls(current_status)
76
77
      if current_status != new_status
78
        update({:dndStatus => new_status})
79
      end
80
    end
81
82
    def update(body=nil)
83
      if body.nil?
84
        raise 'HTTP request body is required to update presence'
85
      end
86
87
      res = @rc_api.client.put do |req|
88
        req.url "account/#{@account_id}/extension/#{@extension_id}/presence"
89
        req.headers['Content-Type'] = 'application/json'
90
        req.body = body
91
      end
92
      
93
      @presence_info = res.body
94
95
      return @presence_info
96
    end
97
98
    def status_enable_dnd_department_calls(current_status)
99
      new_status = current_status
100
101
      new_statuses = {
102
        'DoNotAcceptAnyCalls' => 'TakeDepartmentCallsOnly',
103
        'DoNotAcceptDepartmentCalls' => 'TakeAllCalls'
104
      }
105
106
      if new_statuses.has_key?(current_status.to_s)
107
        new_status = new_statuses[current_status.to_s]
108
      end
109
110
      return new_status
111
    end
112
113
    def status_disable_dnd_department_calls(current_status)
114
      new_status = current_status
115
116
      new_statuses = {
117
        'TakeAllCalls' => 'DoNotAcceptDepartmentCalls',
118
        'TakeDepartmentCallsOnly' => 'DoNotAcceptAnyCalls'
119
      }
120
121
      if new_statuses.has_key?(current_status.to_s)
122
        new_status = new_statuses[current_status.to_s]
123
      end
124
125
      return new_status
126
    end
127
128
  end
129
end