ringcentral_sdk 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,9 @@
1
1
  require 'base64'
2
+ require 'logger'
2
3
  require 'multi_json'
3
4
  require 'observer'
5
+ require 'openssl'
6
+ require 'pubnub'
4
7
  require 'timers'
5
8
 
6
9
  module RingCentralSdk
@@ -11,9 +14,8 @@ module RingCentralSdk
11
14
 
12
15
  attr_reader :event_filters
13
16
 
14
- def initialize(platform, pubnub_factory)
17
+ def initialize(platform)
15
18
  @_platform = platform
16
- @_pubnub_factory = pubnub_factory
17
19
  @event_filters = []
18
20
  @_timeout = nil
19
21
  @_subscription = nil_subscription()
@@ -73,7 +75,7 @@ module RingCentralSdk
73
75
  response = @_platform.client.post do |req|
74
76
  req.url 'subscription'
75
77
  req.body = {
76
- :eventFilters => _get_full_events_filter(),
78
+ :eventFilters => @_platform.create_urls(@event_filters),
77
79
  :deliveryMode => {
78
80
  :transportType => 'PubNub'
79
81
  }
@@ -91,7 +93,6 @@ module RingCentralSdk
91
93
  raise 'Subscribe HTTP Request Error'
92
94
  end
93
95
 
94
- return nil
95
96
  end
96
97
 
97
98
  def renew(events=nil)
@@ -109,9 +110,9 @@ module RingCentralSdk
109
110
 
110
111
  begin
111
112
  response = @_platform.client.put do |req|
112
- req.url 'subscription' + @_subscription['id']
113
+ req.url 'subscription/' + @_subscription['id'].to_s
113
114
  req.body = {
114
- :eventFilters => _get_full_events_filter()
115
+ :eventFilters => @_platform.create_urls(@event_filters),
115
116
  }
116
117
  end
117
118
 
@@ -133,6 +134,7 @@ module RingCentralSdk
133
134
  end
134
135
 
135
136
  begin
137
+ url = 'subscription/' + @_subscription['id'].to_s
136
138
  response = @_platform.client.delete do |req|
137
139
  req.url = 'subscription' + @_subscription['id']
138
140
  end
@@ -183,7 +185,8 @@ module RingCentralSdk
183
185
  end
184
186
 
185
187
  s_key = @_subscription['deliveryMode']['subscriberKey']
186
- @_pubnub = @_pubnub_factory.pubnub(s_key, false, '')
188
+
189
+ @_pubnub = new_pubnub(s_key, false, '')
187
190
 
188
191
  callback = lambda { |envelope|
189
192
  _notify(envelope.msg)
@@ -214,14 +217,13 @@ module RingCentralSdk
214
217
 
215
218
  if _encrypted?()
216
219
  delivery_mode = @_subscription['deliveryMode']
217
- key = Base64.decode64(delivery_mode['encryptionKey'])
218
- ciphertext = Base64.decode64(message)
219
220
 
220
- decipher = OpenSSL::Cipher::AES.new(128, :ECB)
221
- decipher.decrypt
222
- decipher.key = key
221
+ cipher = OpenSSL::Cipher::AES.new(128, :ECB)
222
+ cipher.decrypt
223
+ cipher.key = Base64.decode64(delivery_mode['encryptionKey'].to_s)
223
224
 
224
- plaintext = decipher.update(ciphertext) + decipher.final
225
+ ciphertext = Base64.decode64(message)
226
+ plaintext = cipher.update(ciphertext) + cipher.final
225
227
 
226
228
  message = MultiJson.decode(plaintext)
227
229
  end
@@ -244,16 +246,6 @@ module RingCentralSdk
244
246
  end
245
247
  end
246
248
 
247
- def _get_full_events_filter()
248
- full_events_filter = []
249
- @event_filters.each do |filter|
250
- if filter.to_s
251
- full_events_filter.push(@_platform.create_url(filter.to_s))
252
- end
253
- end
254
- return full_events_filter
255
- end
256
-
257
249
  def _set_timeout()
258
250
  time_to_expiration = @_subscription['expiresIn'] - RENEW_HANDICAP
259
251
  @_timeout = Timers::Group.new
@@ -268,5 +260,21 @@ module RingCentralSdk
268
260
  end
269
261
  end
270
262
 
263
+ def new_pubnub(subscribe_key='', ssl_on=false, publish_key='', my_logger=nil)
264
+ my_logger = Logger.new(STDOUT) if my_logger.nil?
265
+
266
+ return Pubnub.new(
267
+ :subscribe_key => subscribe_key.to_s,
268
+ :publish_key => publish_key.to_s,
269
+ :error_callback => lambda { |msg|
270
+ puts "Error callback says: #{msg.inspect}"
271
+ },
272
+ :connect_callback => lambda { |msg|
273
+ puts "CONNECTED: #{msg.inspect}"
274
+ },
275
+ :logger => my_logger
276
+ )
277
+ end
278
+
271
279
  end
272
280
  end
@@ -0,0 +1,12 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'test/unit'
5
+ require "mocha/test_unit"
6
+ require 'ringcentral_sdk'
7
+
8
+ rcsdk = RingCentralSdk.new(
9
+ 'my_app_key',
10
+ 'my_app_secret',
11
+ RingCentralSdk::RC_SERVER_SANDBOX
12
+ )
@@ -0,0 +1,84 @@
1
+ require './test/test_helper.rb'
2
+
3
+ class RingCentralSdkHelperFaxTest < Test::Unit::TestCase
4
+ def testSetup
5
+
6
+ fax = RingCentralSdk::Helpers::CreateFaxRequest.new(
7
+ nil, # Can be nil or {} for defaults '~'
8
+ {
9
+ # phone numbers are in E.164 format with or without leading '+'
10
+ :to => '+16505551212',
11
+ :faxResolution => 'High',
12
+ :coverPageText => 'RingCentral fax demo using Ruby SDK!'
13
+ },
14
+ :text => 'RingCentral fax demo using Ruby SDK!'
15
+ )
16
+
17
+ assert_equal "RingCentralSdk::Helpers::CreateFaxRequest", fax.class.name
18
+ assert_equal 'account/~/extension/~/fax', fax.url()
19
+
20
+ fax2 = RingCentralSdk::Helpers::CreateFaxRequest.new(
21
+ { :account_id => '111111111', :extension_id => '222222222' }, # Can be nil or {} for defaults '~'
22
+ {
23
+ # phone numbers are in E.164 format with or without leading '+'
24
+ "to" => { :phoneNumber => '+16505551212' },
25
+ :faxResolution => 'High',
26
+ :coverPageText => 'RingCentral fax demo using Ruby SDK!'
27
+ },
28
+ :file_name => './scripts/test_file.pdf'
29
+ )
30
+
31
+ assert_equal 'account/111111111/extension/222222222/fax', fax2.url()
32
+
33
+ assert_raise do
34
+ fax2.add_file('non-existent_file_path')
35
+ end
36
+
37
+ fax3 = RingCentralSdk::Helpers::CreateFaxRequest.new(
38
+ { :account_id => 111111111, :extension_id => 222222222 }, # Can be nil or {} for defaults '~'
39
+ {
40
+ # phone numbers are in E.164 format with or without leading '+'
41
+ :to => [{ :phoneNumber => '+16505551212' }],
42
+ :faxResolution => 'High',
43
+ :coverPageText => 'RingCentral fax demo using Ruby SDK!'
44
+ },
45
+ :text => 'RingCentral fax demo using Ruby SDK!'
46
+ )
47
+
48
+ assert_equal 'account/111111111/extension/222222222/fax', fax3.url()
49
+
50
+ fax4 = RingCentralSdk::Helpers::CreateFaxRequest.new(
51
+ { :account_id => 111111111, :extension_id => 222222222 }, # Can be nil or {} for defaults '~'
52
+ '{"to":"+16505551212","coverPageText":"RingCentral fax demo using Ruby SDK!"}',
53
+ :text => 'RingCentral fax demo using Ruby SDK!'
54
+ )
55
+ assert_equal 'account/111111111/extension/222222222/fax', fax4.url()
56
+
57
+ fax5 = RingCentralSdk::Helpers::CreateFaxRequest.new(
58
+ { :account_id => 111111111, :extension_id => 222222222 }, # Can be nil or {} for defaults '~'
59
+ '{"coverPageText":"RingCentral fax demo using Ruby SDK!"}',
60
+ :text => 'RingCentral fax demo using Ruby SDK!'
61
+ )
62
+ assert_equal 'account/111111111/extension/222222222/fax', fax5.url()
63
+
64
+ assert_equal 'application/pdf', fax.get_file_content_type('example.pdf')
65
+ assert_equal 'attachment', fax.get_attachment_content_disposition()
66
+ assert_equal 'attachment; filename="example.pdf"', fax.get_attachment_content_disposition('example.pdf')
67
+ assert_equal 'attachment; filename="example.pdf"', fax.get_attachment_content_disposition('/path/to/example.pdf')
68
+ assert_equal 'post', fax.method()
69
+
70
+ content_type = fax.content_type()
71
+ content_type_prefix = ''
72
+ boundary = ''
73
+ if content_type =~ /^(multipart\/mixed;\s+boundary=)(.*)$/
74
+ content_type_prefix = $1
75
+ boundary = $2
76
+ end
77
+ assert_equal 'multipart/mixed; boundary=', content_type_prefix
78
+
79
+ lines = fax.body.split(/\r?\n/)
80
+ assert_equal '--' + boundary, lines[0]
81
+ assert_equal '--' + boundary + '--', lines[-1]
82
+
83
+ end
84
+ end
@@ -0,0 +1,21 @@
1
+ require './test/test_helper.rb'
2
+
3
+ class RingCentralSdkHelperInflatorContactInfoTest < Test::Unit::TestCase
4
+ def testSetup
5
+
6
+ inf = RingCentralSdk::Helpers::Inflator::ContactInfo.new
7
+
8
+ arr1 = inf.inflate_to_array(16505551212)
9
+ assert_equal 16505551212, arr1[0][:phoneNumber]
10
+
11
+ arr2 = inf.inflate_to_array('+16505551212')
12
+ assert_equal '+16505551212', arr2[0][:phoneNumber]
13
+
14
+ arr3 = inf.inflate_to_array({:phoneNumber=>16505551212})
15
+ assert_equal 16505551212, arr3[0][:phoneNumber]
16
+
17
+ arr4 = inf.inflate_to_array([{:phoneNumber=>16505551212}])
18
+ assert_equal 16505551212, arr4[0][:phoneNumber]
19
+
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ require './test/test_helper.rb'
2
+
3
+ class RingCentralSdkHelperRequestTest < Test::Unit::TestCase
4
+ def testSetup
5
+
6
+ req = RingCentralSdk::Helpers::Request.new
7
+
8
+ assert_equal 'get', req.method()
9
+ assert_equal '', req.url()
10
+ assert_equal '', req.content_type()
11
+ assert_equal '', req.body()
12
+
13
+ end
14
+ end
@@ -0,0 +1,209 @@
1
+ require './test/test_helper.rb'
2
+
3
+ require 'faraday'
4
+ require 'oauth2'
5
+
6
+ class RingCentralSdkPlatformTest < Test::Unit::TestCase
7
+ def setup
8
+ @rcsdk = RingCentralSdk.new(
9
+ 'my_app_key',
10
+ 'my_app_secret',
11
+ RingCentralSdk::RC_SERVER_SANDBOX
12
+ )
13
+ end
14
+
15
+ def test_main
16
+ assert_equal "bXlfYXBwX2tleTpteV9hcHBfc2VjcmV0", @rcsdk.send(:get_api_key)
17
+ end
18
+
19
+ def test_set_client
20
+ rcsdk = new_rcsdk()
21
+ assert_equal true, rcsdk.oauth2client.is_a?(OAuth2::Client)
22
+
23
+ rcsdk.set_oauth2_client()
24
+ assert_equal true, rcsdk.oauth2client.is_a?(OAuth2::Client)
25
+
26
+ rcsdk = new_rcsdk()
27
+ oauth2client = OAuth2::Client.new(
28
+ 'my_app_key',
29
+ 'my_app_secret',
30
+ :site => RingCentralSdk::RC_SERVER_SANDBOX,
31
+ :token_url => rcsdk.class::TOKEN_ENDPOINT)
32
+ rcsdk.set_oauth2_client(oauth2client)
33
+ assert_equal true, rcsdk.oauth2client.is_a?(OAuth2::Client)
34
+
35
+ assert_raise do
36
+ @rcsdk.set_oauth2_client('test')
37
+ end
38
+ end
39
+
40
+ def test_set_token
41
+ token_data = {:access_token => 'test_token'}
42
+
43
+ @rcsdk.set_token(token_data)
44
+
45
+ assert_equal 'OAuth2::AccessToken', @rcsdk.token.class.name
46
+ assert_equal 'Faraday::Connection', @rcsdk.client.class.name
47
+
48
+ assert_raise do
49
+ @rcsdk.set_token('test')
50
+ end
51
+ end
52
+
53
+ def test_authorize_url_default
54
+ rcsdk = RingCentralSdk.new(
55
+ 'my_app_key',
56
+ 'my_app_secret',
57
+ RingCentralSdk::RC_SERVER_PRODUCTION,
58
+ {:redirect_uri => 'http://localhost:4567/oauth'}
59
+ )
60
+ authorize_url = rcsdk.authorize_url()
61
+
62
+ assert_equal true, authorize_url.is_a?(String)
63
+ assert_equal 0, authorize_url.index(RingCentralSdk::RC_SERVER_PRODUCTION)
64
+ assert_equal true, (authorize_url.index('localhost') > 0) ? true : false
65
+ end
66
+
67
+ def test_authorize_url_explicit
68
+ authorize_url = @rcsdk.authorize_url({:redirect_uri => 'http://localhost:4567/oauth'})
69
+
70
+ assert_equal 0, authorize_url.index(RingCentralSdk::RC_SERVER_SANDBOX)
71
+ assert_equal true, (authorize_url.index('localhost') > 0) ? true : false
72
+ end
73
+
74
+ def test_create_url
75
+ assert_equal '/restapi/v1.0/subscribe', @rcsdk.create_url('subscribe')
76
+ assert_equal '/restapi/v1.0/subscribe', @rcsdk.create_url('/subscribe')
77
+ assert_equal RingCentralSdk::RC_SERVER_SANDBOX + '/restapi/v1.0/subscribe', @rcsdk.create_url('subscribe', true)
78
+ assert_equal RingCentralSdk::RC_SERVER_SANDBOX + '/restapi/v1.0/subscribe', @rcsdk.create_url('/subscribe', true)
79
+ assert_equal RingCentralSdk::RC_SERVER_SANDBOX + '/restapi/v1.0/subscribe', @rcsdk.create_url('subscribe', true)
80
+ assert_equal RingCentralSdk::RC_SERVER_SANDBOX + '/restapi/v1.0/subscribe', @rcsdk.create_url('/subscribe', true)
81
+ end
82
+
83
+ def test_create_urls
84
+ urls = @rcsdk.create_urls(['subscribe'])
85
+ assert_equal '/restapi/v1.0/subscribe', urls[0]
86
+ assert_raise do
87
+ @rcsdk.create_urls(nil)
88
+ end
89
+ end
90
+
91
+ def test_authorize_code
92
+ rcsdk = new_rcsdk()
93
+ rcsdk.set_oauth2_client()
94
+
95
+ stub_token_hash = data_auth_token
96
+ stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash)
97
+
98
+ rcsdk.oauth2client.auth_code.stubs(:get_token).returns(stub_token)
99
+
100
+ token = rcsdk.authorize_code('my_test_auth_code')
101
+ assert_equal 'OAuth2::AccessToken', token.class.name
102
+ assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name
103
+
104
+ rcsdk = new_rcsdk({:redirect_uri => 'http://localhost:4567/oauth'})
105
+ rcsdk.set_oauth2_client()
106
+
107
+ stub_token_hash = data_auth_token
108
+ stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash)
109
+
110
+ rcsdk.oauth2client.auth_code.stubs(:get_token).returns(stub_token)
111
+
112
+ token = rcsdk.authorize_code('my_test_auth_code')
113
+ assert_equal 'OAuth2::AccessToken', token.class.name
114
+ assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name
115
+
116
+ rcsdk = new_rcsdk()
117
+ rcsdk.set_oauth2_client()
118
+
119
+ stub_token_hash = data_auth_token
120
+ stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash)
121
+
122
+ rcsdk.oauth2client.auth_code.stubs(:get_token).returns(stub_token)
123
+
124
+ token = rcsdk.authorize_code('my_test_auth_code')
125
+ assert_equal 'OAuth2::AccessToken', token.class.name
126
+ assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name
127
+
128
+ rcsdk = new_rcsdk()
129
+ rcsdk.set_oauth2_client()
130
+
131
+ stub_token_hash = data_auth_token
132
+ stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash)
133
+
134
+ rcsdk.oauth2client.auth_code.stubs(:get_token).returns(stub_token)
135
+
136
+ token = rcsdk.authorize_code('my_test_auth_code', {:redirect_uri => 'http://localhost:4567/oauth'})
137
+ assert_equal 'OAuth2::AccessToken', token.class.name
138
+ assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name
139
+ end
140
+
141
+ def test_authorize_password
142
+ rcsdk = new_rcsdk()
143
+ rcsdk.set_oauth2_client()
144
+
145
+ stub_token_hash = data_auth_token
146
+ stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash)
147
+
148
+ rcsdk.oauth2client.password.stubs(:get_token).returns(stub_token)
149
+
150
+ token = rcsdk.authorize('my_test_username', 'my_test_extension', 'my_test_password')
151
+ assert_equal 'OAuth2::AccessToken', token.class.name
152
+ assert_equal 'OAuth2::AccessToken', rcsdk.token.class.name
153
+ end
154
+
155
+ def test_request
156
+ assert_raise do
157
+ @rcsdk.request()
158
+ end
159
+
160
+ rcsdk = new_rcsdk()
161
+ rcsdk.set_oauth2_client()
162
+
163
+ stub_token_hash = data_auth_token
164
+ stub_token = OAuth2::AccessToken::from_hash(rcsdk.oauth2client, stub_token_hash)
165
+
166
+ rcsdk.oauth2client.password.stubs(:get_token).returns(stub_token)
167
+
168
+ token = rcsdk.authorize('my_test_username', 'my_test_extension', 'my_test_password')
169
+
170
+ #@rcsdk.client.stubs(:post).returns(Faraday::Response.new)
171
+ Faraday::Connection.any_instance.stubs(:post).returns(Faraday::Response.new)
172
+
173
+ fax = RingCentralSdk::Helpers::CreateFaxRequest.new(
174
+ nil, # Can be nil or {} for defaults '~'
175
+ {
176
+ # phone numbers are in E.164 format with or without leading '+'
177
+ :to => '+16505551212',
178
+ :faxResolution => 'High',
179
+ :coverPageText => 'RingCentral fax demo using Ruby SDK!'
180
+ },
181
+ :text => 'RingCentral fax demo using Ruby SDK!'
182
+ )
183
+ res = rcsdk.request(fax)
184
+ assert_equal 'Faraday::Response', res.class.name
185
+ end
186
+
187
+ def new_rcsdk(opts={})
188
+ return RingCentralSdk.new(
189
+ 'my_app_key',
190
+ 'my_app_secret',
191
+ RingCentralSdk::RC_SERVER_PRODUCTION,
192
+ opts
193
+ )
194
+ end
195
+
196
+ def data_auth_token
197
+ json = '{
198
+ "access_token": "my_test_access_token",
199
+ "token_type": "bearer",
200
+ "expires_in": 3599,
201
+ "refresh_token": "my_test_refresh_token",
202
+ "refresh_token_expires_in": 604799,
203
+ "scope": "ReadCallLog DirectRingOut EditCallLog ReadAccounts Contacts EditExtensions ReadContacts SMS EditPresence RingOut EditCustomData ReadPresence EditPaymentInfo Interoperability Accounts NumberLookup InternalMessages ReadCallRecording EditAccounts Faxes EditReportingSettings ReadClientInfo EditMessages VoipCalling ReadMessages",
204
+ "owner_id": "1234567890"
205
+ }'
206
+ data = JSON.parse(json, :symbolize_names=>true)
207
+ return data
208
+ end
209
+ end