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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/Gemfile.lock +70 -33
  4. data/{LICENSE.txt → LICENSE.md} +0 -0
  5. data/README.md +40 -76
  6. data/lib/ringcentral_sdk.rb +4 -3
  7. data/lib/ringcentral_sdk/rest.rb +18 -17
  8. data/lib/ringcentral_sdk/rest/cache.rb +9 -3
  9. data/lib/ringcentral_sdk/rest/cache/extensions.rb +91 -94
  10. data/lib/ringcentral_sdk/rest/client.rb +196 -202
  11. data/lib/ringcentral_sdk/rest/configuration.rb +91 -0
  12. data/lib/ringcentral_sdk/rest/event.rb +44 -43
  13. data/lib/ringcentral_sdk/rest/extension.rb +12 -9
  14. data/lib/ringcentral_sdk/rest/extension_presence.rb +73 -78
  15. data/lib/ringcentral_sdk/rest/messages.rb +40 -31
  16. data/lib/ringcentral_sdk/rest/messages_retriever.rb +30 -33
  17. data/lib/ringcentral_sdk/rest/request.rb +10 -5
  18. data/lib/ringcentral_sdk/rest/request/base.rb +24 -19
  19. data/lib/ringcentral_sdk/rest/request/fax.rb +88 -91
  20. data/lib/ringcentral_sdk/rest/request/inflator.rb +9 -2
  21. data/lib/ringcentral_sdk/rest/request/inflator/contact_info.rb +19 -12
  22. data/lib/ringcentral_sdk/rest/request/simple.rb +24 -34
  23. data/lib/ringcentral_sdk/rest/simple_client.rb +63 -78
  24. data/lib/ringcentral_sdk/rest/subscription.rb +223 -228
  25. data/test/test_base.rb +5 -5
  26. data/test/test_client.rb +87 -88
  27. data/test/test_event.rb +28 -11
  28. data/test/test_extension_presence.rb +64 -62
  29. data/test/test_helper_fax.rb +46 -47
  30. data/test/test_helper_inflator_contact_info.rb +8 -10
  31. data/test/test_helper_request.rb +1 -1
  32. data/test/test_setup.rb +24 -21
  33. data/test/test_subscription.rb +46 -48
  34. metadata +72 -33
  35. data/lib/ringcentral_sdk/rest/config.rb +0 -102
  36. 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::REST
5
- class Event
6
- attr_accessor :doc
7
- def initialize(data = nil)
8
- if data.is_a? JsonDoc::Document
9
- @doc = data
10
- elsif data.is_a? Hash
11
- data = _symbolize_keys data
12
- @doc = JsonDoc::Document.new(data, false, false, false)
13
- elsif data.nil?
14
- @doc = JsonDoc::Document.new({}, false, false, false)
15
- else
16
- raise 'initialize needs JsonDoc::Document or Hash argument'
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
- def _hash_has_string_keys(hash={})
21
- hash.each do |k,v|
22
- return true if k.is_a? String
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
- def _symbolize_keys(hash={})
28
- if _hash_has_string_keys hash
29
- return MultiJson.decode(MultiJson.encode(hash), :symbolize_keys=>true)
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
- def new_fax_count
35
- new_type_count('fax')
36
- end
36
+ def new_fax_count
37
+ new_type_count('fax')
38
+ end
37
39
 
38
- def new_sms_count
39
- new_type_count('sms')
40
- end
40
+ def new_sms_count
41
+ new_type_count('sms')
42
+ end
41
43
 
42
- def new_type_count(type)
43
- count = 0
44
- have_type = false
45
- changes = @doc.getAttr('body.changes')
46
- if changes.is_a?(Array) && changes.length > 0
47
- changes.each do |change|
48
- if change.key?(:type) && change[:type].to_s.downcase == type
49
- have_type = true
50
- if change.key?(:newCount)
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
- private :_hash_has_string_keys, :_symbolize_keys
59
+ private :_hash_has_string_keys, :_symbolize_keys
60
+ end
60
61
  end
61
62
  end
@@ -1,12 +1,15 @@
1
- module RingCentralSdk::REST
2
- class Extension
3
- attr_reader :extension_id
4
- attr_reader :presence
5
- attr_accessor :client
6
- def initialize(extension_id, opts={})
7
- @extension_id = extension_id
8
- @client = opts.has_key?(:client) ? opts[:client] : nil
9
- @presence = RingCentralSdk::REST::ExtensionPresence.new(extension_id, opts)
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::REST
2
- class ExtensionPresence
3
- attr_accessor :client
4
- attr_accessor :account_id
5
- attr_accessor :extension_id
6
- attr_accessor :presence_data
7
-
8
- def initialize(extension_id, opts={})
9
- @client = opts.key?(:client) ? opts[:client] : nil
10
- @account_id = '~'
11
- @extension_id = extension_id.to_s
12
- @presence_data = {}
13
- end
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
- @presence_data = res.body
17
+ def retrieve
18
+ raise 'extension_id is not an integer' if @extension_id !~ /^[0-9]+$/
25
19
 
26
- return @presence_data
27
- end
20
+ res = @client.http.get do |req|
21
+ req.url "account/#{@account_id}/extension/#{@extension_id}/presence"
22
+ end
28
23
 
29
- def department_calls_enable(enable)
30
- retrieve()
24
+ @presence_data = res.body
31
25
 
32
- unless @presence.is_a?(Hash) && @presence_data.key?('dndStatus')
33
- raise 'invalid presence info'
26
+ @presence_data
34
27
  end
35
28
 
36
- current_status = @presence_data['dndStatus']
37
- new_status = new_status_dnd_department_calls(current_status, enable)
29
+ def department_calls_enable(enable)
30
+ retrieve
38
31
 
39
- if current_status != new_status
40
- update({dndStatus: new_status})
41
- end
32
+ unless @presence.is_a?(Hash) && @presence_data.key?('dndStatus')
33
+ raise 'invalid presence info'
34
+ end
42
35
 
43
- return new_status
44
- end
36
+ current_status = @presence_data['dndStatus']
37
+ new_status = new_status_dnd_department_calls(current_status, enable)
45
38
 
46
- def department_calls_enabled?(reload=false)
47
- if reload
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
- current_status = @presence_data['dndStatus']
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
- status_enabled = {
56
- 'DoNotAcceptAnyCalls' => false,
57
- 'DoNotAcceptDepartmentCalls' => false,
58
- 'TakeAllCalls' => true,
59
- 'TakeDepartmentCallsOnly' => true
60
- }
50
+ current_status = @presence_data['dndStatus']
61
51
 
62
- return status_enabled.key?(current_status) ?
63
- status_enabled[current_status] : nil
64
- end
52
+ status_enabled = {
53
+ 'DoNotAcceptAnyCalls' => false,
54
+ 'DoNotAcceptDepartmentCalls' => false,
55
+ 'TakeAllCalls' => true,
56
+ 'TakeDepartmentCallsOnly' => true
57
+ }
65
58
 
66
- def update(body=nil)
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
- res = @client.http.put do |req|
72
- req.url "account/#{@account_id}/extension/#{@extension_id}/presence"
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
- return @presence_data
80
- end
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
- def new_status_dnd_department_calls(current_status, enable)
83
- new_statuses = {
84
- enable: {
85
- 'DoNotAcceptAnyCalls' => 'TakeDepartmentCallsOnly',
86
- 'DoNotAcceptDepartmentCalls' => 'TakeAllCalls'
87
- },
88
- disable: {
89
- 'TakeAllCalls' => 'DoNotAcceptDepartmentCalls',
90
- 'TakeDepartmentCallsOnly' => 'DoNotAcceptAnyCalls'
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
- action = enable ? :enable : :disable
88
+ action = enable ? :enable : :disable
95
89
 
96
- new_status = current_status
90
+ new_status = current_status
97
91
 
98
- new_status = new_statuses[action][current_status.to_s] \
99
- if new_statuses[action].key?(current_status.to_s)
92
+ new_status = new_statuses[action][current_status.to_s] \
93
+ if new_statuses[action].key?(current_status.to_s)
100
94
 
101
- return new_status
95
+ new_status
96
+ end
102
97
  end
103
98
  end
104
- end
99
+ end
@@ -1,46 +1,55 @@
1
- module RingCentralSdk::REST
2
- class Messages
3
- attr_reader :sms
4
- attr_reader :fax
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
- def initialize(client)
7
- @client = client
8
- @sms = RingCentralSdk::REST::MessagesSMS.new(client)
9
- @fax = RingCentralSdk::REST::MessagesFax.new(client)
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::REST
15
- class MessagesSMS
16
- def initialize(client)
17
- @client = client
18
- end
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
- def create(opts)
21
- response = @client.http.post do |req|
22
- req.url 'account/~/extension/~/sms'
23
- req.headers['Content-Type'] = 'application/json'
24
- req.body = {
25
- from: { phoneNumber: opts[:from].to_s },
26
- to: [ { phoneNumber: opts[:to].to_s } ],
27
- text: opts[:text].to_s
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::REST
36
- class MessagesFax
37
- def initialize(client)
38
- @client = client
39
- end
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
- def create(opts)
42
- fax = RingCentralSdk::REST::Request::Fax.new(opts)
43
- return @client.send_request(fax)
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