Class: RingCentralSdk::REST::Cache::Extensions

Inherits:
Object
  • Object
show all
Defined in:
lib/ringcentral_sdk/rest/cache/extensions.rb

Overview

Extensions cache is a local store that can retrieve all extensions for use locally

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Extensions

Returns a new instance of Extensions.



16
17
18
19
20
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 16

def initialize(client)
  @client = client
  @account_id = '~'
  flush
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



11
12
13
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 11

def 
  @account_id
end

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 10

def client
  @client
end

#extensions_hashObject (readonly)

Returns the value of attribute extensions_hash.



12
13
14
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 12

def extensions_hash
  @extensions_hash
end

#extensions_num2idObject (readonly)

Returns the value of attribute extensions_num2id.



13
14
15
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 13

def extensions_num2id
  @extensions_num2id
end

#last_retrievedObject (readonly)

Returns the value of attribute last_retrieved.



14
15
16
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 14

def last_retrieved
  @last_retrieved
end

Instance Method Details

#flushObject



22
23
24
25
26
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 22

def flush
  @extensions_hash = {}
  @extensions_num2id = {}
  @last_retrieved = -1
end

#get_department_members(department_id) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 94

def get_department_members(department_id)
  department_id = department_id.to_s unless department_id.is_a? String
  if department_id !~ /^[0-9]+$/
    raise 'department_id parameter must be a positive integer'
  end

  members = []

  res = @client.http.get do |req|
    req.url "account/#{}/department/#{department_id}/members"
  end

  if res.body.key? 'records'
    res.body['records'].each do |extension|
      if extension.key? 'id'
        member = get_extension_by_id extension['id']
        members.push member unless member.nil?
      end
    end
  end

  members
end

#get_extension_by_id(extension_id) ⇒ Object



75
76
77
78
79
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 75

def get_extension_by_id(extension_id)
  extension_id = extension_id.to_s unless extension_id.is_a? String
  return @extensions_hash[extension_id] if @extensions_hash.key? extension_id
  nil
end

#get_extension_by_number(extension_number) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 81

def get_extension_by_number(extension_number)
  unless extension_number.is_a? String
    extension_number = extension_number.to_s
  end
  if @extensions_num2id.key?(extension_number)
    extension_id = @extensions_num2id[extension_number]
    if @extensions_hash.key?(extension_id)
      return @extensions_hash[extension_id]
    end
  end
  nil
end

#inflate_num2idObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 64

def inflate_num2id
  num2id = {}
  @extensions_hash.each do |_, v|
    if v.key?('id') && v['id'] > 0 && v.key?('extensionNumber') && !v['extensionNumber'].empty?
      num2id[v['extensionNumber']] = v['id'].to_s
    end
  end
  @extensions_num2id = num2id
  num2id
end

#load_extensions(api_extensions) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 52

def load_extensions(api_extensions)
  api_extensions.each do |extension|
    if extension.key?('id') && extension['id'] > 0
      @extensions_hash[extension['id'].to_s] = extension
    end
  end
end

#retrieve(params = {}, retrieve_all = true) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 28

def retrieve(params = {}, retrieve_all = true)
  @last_retrieved = Time.now.to_i
  uri = URI.parse "account/#{@account_id}/extension"
  uri.query = URI.encode_www_form(params) unless params.empty?
  res = @client.http.get do |req|
    req.url uri.to_s
    if retrieve_all
      req.params['page'] = 1
      req.params['perPage'] = 1000
    end
  end
  load_extensions(res.body['records'])
  if retrieve_all
    while res.body.key?('navigation') && res.body['navigation'].key?('nextPage')
      res = @client.http.get do |req|
        req.url res.body['navigation']['nextPage']['uri']
      end
      load_extensions(res.body['records'])
    end
  end
  inflate_num2id
  @extensions_hash
end

#retrieve_allObject



60
61
62
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 60

def retrieve_all
  retrieve({}, true)
end