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.



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

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

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



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

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.



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

def extensions_hash
  @extensions_hash
end

#extensions_num2idObject (readonly)

Returns the value of attribute extensions_num2id.



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

def extensions_num2id
  @extensions_num2id
end

#last_retrievedObject (readonly)

Returns the value of attribute last_retrieved.



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

def last_retrieved
  @last_retrieved
end

Instance Method Details

#flushObject



19
20
21
22
23
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 19

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

#get_department_members(department_id) ⇒ Object



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

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



72
73
74
75
76
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 72

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



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 78

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



61
62
63
64
65
66
67
68
69
70
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 61

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



49
50
51
52
53
54
55
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 49

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



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

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



57
58
59
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 57

def retrieve_all
  retrieve({}, true)
end