ringcentral_sdk 1.3.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/Gemfile.lock +70 -33
- data/{LICENSE.txt → LICENSE.md} +0 -0
- data/README.md +40 -76
- data/lib/ringcentral_sdk.rb +4 -3
- data/lib/ringcentral_sdk/rest.rb +18 -17
- data/lib/ringcentral_sdk/rest/cache.rb +9 -3
- data/lib/ringcentral_sdk/rest/cache/extensions.rb +91 -94
- data/lib/ringcentral_sdk/rest/client.rb +196 -202
- data/lib/ringcentral_sdk/rest/configuration.rb +91 -0
- data/lib/ringcentral_sdk/rest/event.rb +44 -43
- data/lib/ringcentral_sdk/rest/extension.rb +12 -9
- data/lib/ringcentral_sdk/rest/extension_presence.rb +73 -78
- data/lib/ringcentral_sdk/rest/messages.rb +40 -31
- data/lib/ringcentral_sdk/rest/messages_retriever.rb +30 -33
- data/lib/ringcentral_sdk/rest/request.rb +10 -5
- data/lib/ringcentral_sdk/rest/request/base.rb +24 -19
- data/lib/ringcentral_sdk/rest/request/fax.rb +88 -91
- data/lib/ringcentral_sdk/rest/request/inflator.rb +9 -2
- data/lib/ringcentral_sdk/rest/request/inflator/contact_info.rb +19 -12
- data/lib/ringcentral_sdk/rest/request/simple.rb +24 -34
- data/lib/ringcentral_sdk/rest/simple_client.rb +63 -78
- data/lib/ringcentral_sdk/rest/subscription.rb +223 -228
- data/test/test_base.rb +5 -5
- data/test/test_client.rb +87 -88
- data/test/test_event.rb +28 -11
- data/test/test_extension_presence.rb +64 -62
- data/test/test_helper_fax.rb +46 -47
- data/test/test_helper_inflator_contact_info.rb +8 -10
- data/test/test_helper_request.rb +1 -1
- data/test/test_setup.rb +24 -21
- data/test/test_subscription.rb +46 -48
- metadata +72 -33
- data/lib/ringcentral_sdk/rest/config.rb +0 -102
- data/test/test_config.rb +0 -29
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'dotenv'
|
2
|
+
require 'logger'
|
3
|
+
require 'multi_json'
|
4
|
+
|
5
|
+
module RingCentralSdk
|
6
|
+
module REST
|
7
|
+
# Configuration class populated by Client constructor block
|
8
|
+
class Configuration
|
9
|
+
attr_accessor :server_url
|
10
|
+
attr_accessor :app_key
|
11
|
+
attr_accessor :app_secret
|
12
|
+
attr_accessor :redirect_url
|
13
|
+
|
14
|
+
attr_accessor :username
|
15
|
+
attr_accessor :extension
|
16
|
+
attr_accessor :password
|
17
|
+
attr_accessor :token
|
18
|
+
attr_accessor :token_file
|
19
|
+
|
20
|
+
attr_accessor :load_env
|
21
|
+
attr_accessor :headers
|
22
|
+
attr_accessor :retry
|
23
|
+
attr_accessor :retry_options
|
24
|
+
attr_accessor :logger
|
25
|
+
|
26
|
+
def inflate
|
27
|
+
@logger = default_logger if !defined?(@logger) || @logger.nil?
|
28
|
+
load_environment if load_env
|
29
|
+
inflate_headers
|
30
|
+
inflate_retry
|
31
|
+
inflate_token
|
32
|
+
end
|
33
|
+
|
34
|
+
def default_logger
|
35
|
+
logger = Logger.new STDOUT
|
36
|
+
logger.level = Logger::WARN
|
37
|
+
logger
|
38
|
+
end
|
39
|
+
|
40
|
+
def load_environment
|
41
|
+
Dotenv.load
|
42
|
+
@server_url = ENV['RC_SERVER_URL'] if ENV.key? 'RC_SERVER_URL'
|
43
|
+
@app_key = ENV['RC_APP_KEY'] if ENV.key? 'RC_APP_KEY'
|
44
|
+
@app_secret = ENV['RC_APP_SECRET'] if ENV.key? 'RC_APP_SECRET'
|
45
|
+
@redirect_url = ENV['RC_APP_REDIRECT_URL'] if ENV.key? 'RC_APP_REDIRECT_URL'
|
46
|
+
@username = ENV['RC_USER_USERNAME'] if ENV.key? 'RC_USER_USERNAME'
|
47
|
+
@extension = ENV['RC_USER_EXTENSION'] if ENV.key? 'RC_USER_EXTENSION'
|
48
|
+
@password = ENV['RC_USER_PASSWORD'] if ENV.key? 'RC_USER_PASSWORD'
|
49
|
+
@token = ENV['RC_TOKEN'] if ENV.key? 'RC_TOKEN'
|
50
|
+
@token_file = ENV['RC_TOKEN_FILE'] if ENV.key? 'RC_TOKEN_FILE'
|
51
|
+
@retry = ENV['RC_RETRY'] if ENV.key? 'RC_RETRY'
|
52
|
+
@retry_options = ENV['RC_RETRY_OPTIONS'] if ENV.key? 'RC_RETRY_OPTIONS'
|
53
|
+
@headers = ENV['RC_HEADERS'] if ENV.key? 'RC_HEADERS'
|
54
|
+
end
|
55
|
+
|
56
|
+
def inflate_retry
|
57
|
+
if !defined?(@retry) || @retry.nil? || @retry.empty? || @retry.to_s.downcase == 'false'
|
58
|
+
@retry = false
|
59
|
+
@retry_options = {}
|
60
|
+
return
|
61
|
+
end
|
62
|
+
@retry = true
|
63
|
+
if !@retry_options.nil? && @retry_options.to_s =~ /^\s*{/
|
64
|
+
@retry_options = MultiJson.decode @retry_options.to_s, symbolize_keys: true
|
65
|
+
else
|
66
|
+
@retry_options = {}
|
67
|
+
end
|
68
|
+
@retry_options[:logger] = @logger
|
69
|
+
end
|
70
|
+
|
71
|
+
def inflate_headers
|
72
|
+
@headers = {} unless defined? @headers
|
73
|
+
if !@headers.nil? && @headers.is_a?(String) && @headers =~ /^\s*{/
|
74
|
+
@headers = MultiJson.decode @headers, symbolize_keys: true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def inflate_token
|
79
|
+
@token = nil unless defined? @token
|
80
|
+
|
81
|
+
if (@token.nil? || @token.empty?) && !token_file.nil? && !@token_file.empty?
|
82
|
+
@token = IO.read @token_file if File.exist? @token_file
|
83
|
+
end
|
84
|
+
|
85
|
+
if !defined?(@token) && !@token.nil? && @token.is_a?(String) && @token =~ /^\s*{/
|
86
|
+
@token = MultiJson.decode @token
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -1,61 +1,62 @@
|
|
1
1
|
require 'jsondoc'
|
2
2
|
require 'multi_json'
|
3
3
|
|
4
|
-
module RingCentralSdk
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
4
|
+
module RingCentralSdk
|
5
|
+
module REST
|
6
|
+
# Event represents a Subscription API event
|
7
|
+
class Event
|
8
|
+
attr_accessor :doc
|
9
|
+
def initialize(data = nil)
|
10
|
+
if data.is_a? JsonDoc::Document
|
11
|
+
@doc = data
|
12
|
+
elsif data.is_a? Hash
|
13
|
+
data = _symbolize_keys data
|
14
|
+
@doc = JsonDoc::Document.new(data, false, false, false)
|
15
|
+
elsif data.nil?
|
16
|
+
@doc = JsonDoc::Document.new({}, false, false, false)
|
17
|
+
else
|
18
|
+
raise 'initialize needs JsonDoc::Document or Hash argument'
|
19
|
+
end
|
17
20
|
end
|
18
|
-
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
def _hash_has_string_keys(hash = {})
|
23
|
+
hash.each do |k, _|
|
24
|
+
return true if k.is_a? String
|
25
|
+
end
|
26
|
+
false
|
23
27
|
end
|
24
|
-
return false
|
25
|
-
end
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
def _symbolize_keys(hash = {})
|
30
|
+
if _hash_has_string_keys hash
|
31
|
+
return MultiJson.decode(MultiJson.encode(hash), symbolize_keys: true)
|
32
|
+
end
|
33
|
+
hash
|
30
34
|
end
|
31
|
-
return hash
|
32
|
-
end
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
-
|
36
|
+
def new_fax_count
|
37
|
+
new_type_count('fax')
|
38
|
+
end
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
40
|
+
def new_sms_count
|
41
|
+
new_type_count('sms')
|
42
|
+
end
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
count += change[:newCount]
|
44
|
+
def new_type_count(type)
|
45
|
+
count = 0
|
46
|
+
have_type = false
|
47
|
+
changes = @doc.getAttr('body.changes')
|
48
|
+
if changes.is_a?(Array) && !changes.empty?
|
49
|
+
changes.each do |change|
|
50
|
+
if change.key?(:type) && change[:type].to_s.downcase == type
|
51
|
+
have_type = true
|
52
|
+
count += change[:newCount] if change.key? :newCount
|
52
53
|
end
|
53
54
|
end
|
54
55
|
end
|
56
|
+
have_type ? count : -1
|
55
57
|
end
|
56
|
-
return have_type ? count : -1
|
57
|
-
end
|
58
58
|
|
59
|
-
|
59
|
+
private :_hash_has_string_keys, :_symbolize_keys
|
60
|
+
end
|
60
61
|
end
|
61
62
|
end
|
@@ -1,12 +1,15 @@
|
|
1
|
-
module RingCentralSdk
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module RingCentralSdk
|
2
|
+
module REST
|
3
|
+
# Extension represents a RingCentral user extension object
|
4
|
+
class Extension
|
5
|
+
attr_reader :extension_id
|
6
|
+
attr_reader :presence
|
7
|
+
attr_accessor :client
|
8
|
+
def initialize(extension_id, opts = {})
|
9
|
+
@extension_id = extension_id
|
10
|
+
@client = opts.key?(:client) ? opts[:client] : nil
|
11
|
+
@presence = RingCentralSdk::REST::ExtensionPresence.new(extension_id, opts)
|
12
|
+
end
|
10
13
|
end
|
11
14
|
end
|
12
15
|
end
|
@@ -1,104 +1,99 @@
|
|
1
|
-
module RingCentralSdk
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
def retrieve()
|
16
|
-
if @extension_id !~ /^[0-9]+$/
|
17
|
-
raise "extension_id is not an integer"
|
18
|
-
end
|
19
|
-
|
20
|
-
res = @client.http.get do |req|
|
21
|
-
req.url "account/#{@account_id}/extension/#{@extension_id}/presence"
|
1
|
+
module RingCentralSdk
|
2
|
+
module REST
|
3
|
+
# ExtensionPresence is a helper class to manage presence info
|
4
|
+
class ExtensionPresence
|
5
|
+
attr_accessor :client
|
6
|
+
attr_accessor :account_id
|
7
|
+
attr_accessor :extension_id
|
8
|
+
attr_accessor :presence_data
|
9
|
+
|
10
|
+
def initialize(extension_id, opts = {})
|
11
|
+
@client = opts.key?(:client) ? opts[:client] : nil
|
12
|
+
@account_id = '~'
|
13
|
+
@extension_id = extension_id.to_s
|
14
|
+
@presence_data = {}
|
22
15
|
end
|
23
16
|
|
24
|
-
|
17
|
+
def retrieve
|
18
|
+
raise 'extension_id is not an integer' if @extension_id !~ /^[0-9]+$/
|
25
19
|
|
26
|
-
|
27
|
-
|
20
|
+
res = @client.http.get do |req|
|
21
|
+
req.url "account/#{@account_id}/extension/#{@extension_id}/presence"
|
22
|
+
end
|
28
23
|
|
29
|
-
|
30
|
-
retrieve()
|
24
|
+
@presence_data = res.body
|
31
25
|
|
32
|
-
|
33
|
-
raise 'invalid presence info'
|
26
|
+
@presence_data
|
34
27
|
end
|
35
28
|
|
36
|
-
|
37
|
-
|
29
|
+
def department_calls_enable(enable)
|
30
|
+
retrieve
|
38
31
|
|
39
|
-
|
40
|
-
|
41
|
-
|
32
|
+
unless @presence.is_a?(Hash) && @presence_data.key?('dndStatus')
|
33
|
+
raise 'invalid presence info'
|
34
|
+
end
|
42
35
|
|
43
|
-
|
44
|
-
|
36
|
+
current_status = @presence_data['dndStatus']
|
37
|
+
new_status = new_status_dnd_department_calls(current_status, enable)
|
45
38
|
|
46
|
-
|
47
|
-
|
48
|
-
retrieve()
|
49
|
-
elsif !@presence_data.key?('dndStatus')
|
50
|
-
retrieve()
|
39
|
+
update(dndStatus: new_status) if current_status != new_status
|
40
|
+
new_status
|
51
41
|
end
|
52
42
|
|
53
|
-
|
43
|
+
def department_calls_enabled?(reload = false)
|
44
|
+
if reload
|
45
|
+
retrieve
|
46
|
+
elsif !@presence_data.key?('dndStatus')
|
47
|
+
retrieve
|
48
|
+
end
|
54
49
|
|
55
|
-
|
56
|
-
'DoNotAcceptAnyCalls' => false,
|
57
|
-
'DoNotAcceptDepartmentCalls' => false,
|
58
|
-
'TakeAllCalls' => true,
|
59
|
-
'TakeDepartmentCallsOnly' => true
|
60
|
-
}
|
50
|
+
current_status = @presence_data['dndStatus']
|
61
51
|
|
62
|
-
|
63
|
-
|
64
|
-
|
52
|
+
status_enabled = {
|
53
|
+
'DoNotAcceptAnyCalls' => false,
|
54
|
+
'DoNotAcceptDepartmentCalls' => false,
|
55
|
+
'TakeAllCalls' => true,
|
56
|
+
'TakeDepartmentCallsOnly' => true
|
57
|
+
}
|
65
58
|
|
66
|
-
|
67
|
-
if body.nil?
|
68
|
-
raise 'HTTP request body is required to update presence'
|
59
|
+
status_enabled.key?(current_status) ? status_enabled[current_status] : nil
|
69
60
|
end
|
70
61
|
|
71
|
-
|
72
|
-
|
73
|
-
req.headers['Content-Type'] = 'application/json'
|
74
|
-
req.body = body
|
75
|
-
end
|
76
|
-
|
77
|
-
@presence_data = res.body
|
62
|
+
def update(body = nil)
|
63
|
+
raise 'HTTP request body is required to update presence' if body.nil?
|
78
64
|
|
79
|
-
|
80
|
-
|
65
|
+
res = @client.http.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_data = res.body
|
81
72
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
73
|
+
@presence_data
|
74
|
+
end
|
75
|
+
|
76
|
+
def new_status_dnd_department_calls(current_status, enable)
|
77
|
+
new_statuses = {
|
78
|
+
enable: {
|
79
|
+
'DoNotAcceptAnyCalls' => 'TakeDepartmentCallsOnly',
|
80
|
+
'DoNotAcceptDepartmentCalls' => 'TakeAllCalls'
|
81
|
+
},
|
82
|
+
disable: {
|
83
|
+
'TakeAllCalls' => 'DoNotAcceptDepartmentCalls',
|
84
|
+
'TakeDepartmentCallsOnly' => 'DoNotAcceptAnyCalls'
|
85
|
+
}
|
91
86
|
}
|
92
|
-
}
|
93
87
|
|
94
|
-
|
88
|
+
action = enable ? :enable : :disable
|
95
89
|
|
96
|
-
|
90
|
+
new_status = current_status
|
97
91
|
|
98
|
-
|
99
|
-
|
92
|
+
new_status = new_statuses[action][current_status.to_s] \
|
93
|
+
if new_statuses[action].key?(current_status.to_s)
|
100
94
|
|
101
|
-
|
95
|
+
new_status
|
96
|
+
end
|
102
97
|
end
|
103
98
|
end
|
104
|
-
end
|
99
|
+
end
|
@@ -1,46 +1,55 @@
|
|
1
|
-
module RingCentralSdk
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module RingCentralSdk
|
2
|
+
module REST
|
3
|
+
# Messages is a wrapper helper class
|
4
|
+
class Messages
|
5
|
+
attr_reader :sms
|
6
|
+
attr_reader :fax
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def initialize(client)
|
9
|
+
@client = client
|
10
|
+
@sms = RingCentralSdk::REST::MessagesSMS.new(client)
|
11
|
+
@fax = RingCentralSdk::REST::MessagesFax.new(client)
|
12
|
+
end
|
10
13
|
end
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
14
|
-
module RingCentralSdk
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
module RingCentralSdk
|
18
|
+
module REST
|
19
|
+
# MessagesSMS provides a helper for SMS messages
|
20
|
+
class MessagesSMS
|
21
|
+
def initialize(client)
|
22
|
+
@client = client
|
23
|
+
end
|
19
24
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
def create(opts)
|
26
|
+
response = @client.http.post do |req|
|
27
|
+
req.url 'account/~/extension/~/sms'
|
28
|
+
req.headers['Content-Type'] = 'application/json'
|
29
|
+
req.body = {
|
30
|
+
from: { phoneNumber: opts[:from].to_s },
|
31
|
+
to: [{ phoneNumber: opts[:to].to_s }],
|
32
|
+
text: opts[:text].to_s
|
33
|
+
}
|
34
|
+
end
|
35
|
+
response
|
29
36
|
end
|
30
|
-
return response
|
31
37
|
end
|
32
38
|
end
|
33
39
|
end
|
34
40
|
|
35
|
-
module RingCentralSdk
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
41
|
+
module RingCentralSdk
|
42
|
+
module REST
|
43
|
+
# MessagesFax provides a helper for fax requests
|
44
|
+
class MessagesFax
|
45
|
+
def initialize(client)
|
46
|
+
@client = client
|
47
|
+
end
|
40
48
|
|
41
|
-
|
42
|
-
|
43
|
-
|
49
|
+
def create(opts)
|
50
|
+
fax = RingCentralSdk::REST::Request::Fax.new opts
|
51
|
+
@client.send_request fax
|
52
|
+
end
|
44
53
|
end
|
45
54
|
end
|
46
55
|
end
|