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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Extensions

Returns a new instance of Extensions.



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

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

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



7
8
9
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 7

def 
  @account_id
end

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 6

def client
  @client
end

#extensions_hashObject (readonly)

Returns the value of attribute extensions_hash.



8
9
10
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 8

def extensions_hash
  @extensions_hash
end

#extensions_num2idObject (readonly)

Returns the value of attribute extensions_num2id.



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

def extensions_num2id
  @extensions_num2id
end

#last_retrievedObject (readonly)

Returns the value of attribute last_retrieved.



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

def last_retrieved
  @last_retrieved
end

Instance Method Details

#flushObject



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

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

#get_department_members(department_id) ⇒ Object



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

def get_department_members(department_id)
  if !department_id.is_a?(String)
    department_id = department_id.to_s
  end
  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

  return members
end

#get_extension_by_id(extension_id) ⇒ Object



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

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

#get_extension_by_number(extension_number) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 84

def get_extension_by_number(extension_number)
  if !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
  return nil
end

#inflate_num2idObject



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

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

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ringcentral_sdk/rest/cache/extensions.rb', line 24

def retrieve(params={}, retrieve_all=true)
  @last_retrieved = Time.now.to_i
  uri = URI.parse "account/#{@account_id}/extension"
  if params.length > 0 
    uri.query = URI.encode_www_form params
  end
  res = @client.http.get do |req|
    req.url uri.to_s
    if retrieve_all
      req.params['page'] = 1
      req.params['perPage'] = 1000
    end
  end
  res.body['records'].each do |extension|
    if extension.key?('id') && extension['id'] > 0
      @extensions_hash[extension['id'].to_s] = extension
    end
  end
  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
      res.body['records'].each do |extension|
        if extension.key?('id') && extension['id'] > 0
          @extensions_hash[extension['id'].to_s] = extension
        end
      end
    end
  end
  inflate_num2id()
  return @extensions_hash
end

#retrieve_allObject



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

def retrieve_all()
  retrieve({}, true)
end