ringcentral_sdk 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 239dea9ba4e4c8e62d7766757e538136ecda1576
4
- data.tar.gz: b4f83504a9a0e48110174b4233da10b0619b7868
3
+ metadata.gz: df84f0ee4eab215d88476494ab7ff4681c59fe34
4
+ data.tar.gz: 0bcb1c96920506ee4d63d4a162c9cab33a25078d
5
5
  SHA512:
6
- metadata.gz: 9fa0276afb85635e2865c00a63c7a14ca2dd2213cb0b894223cfd0ad6863509a234b158e85c9bb8c5c318512a3eb75fa566f952c96c7cd5911f65b65b4234ce9
7
- data.tar.gz: 3114700d02cc0e3c31c86864c60240a55ff884d69e707235888a7cae7f6390fc01a831e4573b4ab9e5411ba001692fa43940737cfc105610cb5607ac6d531a93
6
+ metadata.gz: 89675600de9040918b4fead9d4134d00d2107ff4501e81108189126452201a6c2b7d1130e9a92921dfe744f39fd84b190c25cc6a34ceec65758649f63d2d1733
7
+ data.tar.gz: d64154488b9a64cdf2385112b1a2b548a6092f15c57b1550eecdbae005b19f080343108f917d07ff281603d44468cc7d4a3dad211b85b3966286e78e433036fa
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  CHANGELOG
2
2
  ---------
3
+ - **2015-10-15**: 0.4.0
4
+ - Add 3-Legged OAuth support via platform `authorize_url` and `authorize_code` methods
5
+ - Add `oauth2-sinatra` demo app in `scripts` directory
6
+ - Add `call_recording_download.rb` demo script
7
+ - Add `rc-credentials.json` separate demo credentials file
8
+ - Update `fax_send.rb` demo script with name change
9
+ - Modify SDK initialization by converting optional params to options hash
10
+ - Refactor `RingCentralSdk::Platform::Platform` to `RingCentralSdk::Platform`
3
11
  - **2015-10-07**: 0.3.0
4
12
  - Add subscription support via PubNub
5
13
  - Add `ruby-head`, `jruby`, `jruby-head`, and `rbx-2` to Travis CI
data/README.md CHANGED
@@ -21,6 +21,8 @@ RingCentral SDK
21
21
  2. [Usage](#usage)
22
22
  1. [Instantiation](#instantiation)
23
23
  1. [Authorization](#authorization)
24
+ 1. [Password Grant](#password-grant)
25
+ 1. [Authorization Code Grant](#authorization-code-grant)
24
26
  1. [API Requests](#api-requests)
25
27
  1. [SMS Example](#sms-example)
26
28
  2. [Fax Example](#fax-example)
@@ -32,7 +34,7 @@ RingCentral SDK
32
34
 
33
35
  ## Overview
34
36
 
35
- This is an unofficial Ruby SDK for the RingCentral for Developers Platform REST API (https://developers.ringcentral.com).
37
+ This is a Ruby SDK for the RingCentral for Developers Platform REST API (https://developers.ringcentral.com).
36
38
 
37
39
  The core SDK objects follow the general design of the [official RingCentral SDKs](https://github.com/ringcentral). Additional functionality is provided for ease of use including request helpers and generalized OAuth2 support.
38
40
 
@@ -40,8 +42,9 @@ This SDK is an early stage library and subject to breaking changes.
40
42
 
41
43
  ### Included
42
44
 
43
- * OAuth2 authorization & token refresh via INTRIDEA `OAuth2::AccessToken`
45
+ * OAuth2 authorization code and password grant flows including token refresh via INTRIDEA `OAuth2::AccessToken`
44
46
  * Generic API requests handled via `Faraday` client
47
+ * Subscription support via `Pubnub` with auto-decryption
45
48
  * Fax request helper to create `multipart/mixed` messages
46
49
  * Docs via [Read the Docs](http://ringcentral-sdk-ruby.readthedocs.org/) and [RubyDoc](http://www.rubydoc.info/gems/ringcentral_sdk/)
47
50
 
@@ -49,7 +52,6 @@ This SDK is an early stage library and subject to breaking changes.
49
52
 
50
53
  The following items are still needed for this SDK. Contributions are most welcome.
51
54
 
52
- * Subscriptions
53
55
  * Mock tests
54
56
 
55
57
  ## Documentation
@@ -64,7 +66,7 @@ more information on individual API calls:
64
66
 
65
67
  1. [API Developer and Reference Guide](https://developers.ringcentral.com/api-docs/latest/index.html) for information on specific APIs.
66
68
  1. [API Explorer](http://ringcentral.github.io/api-explorer/)
67
- 1. [CTI Tutorial](http://ringcentral.github.io/cti-tutorial/)
69
+ 1. [Dev Tutorial](http://ringcentral.github.io/tutorial/)
68
70
 
69
71
  ## Installation
70
72
 
@@ -105,8 +107,8 @@ The RingCentral server URLs can be populated manually or via the included consta
105
107
  require 'ringcentral_sdk'
106
108
 
107
109
  rcsdk = RingCentralSdk::Sdk.new(
108
- "myAppKey",
109
- "myAppSecret",
110
+ 'myAppKey',
111
+ 'myAppSecret',
110
112
  RingCentralSdk::Sdk::RC_SERVER_SANDBOX
111
113
  )
112
114
  platform = rcsdk.platform
@@ -114,15 +116,53 @@ platform = rcsdk.platform
114
116
 
115
117
  ### Authorization
116
118
 
119
+ #### Password Grant
120
+
121
+ The 2-legged OAuth 2.0 flow using a password grant is designed for server applications where the app and resource owners are the same.
122
+
117
123
  ```ruby
118
124
  # Initialize using main phone number and extension number
119
- platform.authorize("myUsername", "myExtension", "myPassword")
125
+ platform.authorize('myUsername', 'myExtension', 'myPassword')
120
126
 
121
127
  # Initialize using user phone number without extension number
122
128
  # Extension defaults to company admin extension
123
- platform.authorize("myUsername", nil, "myPassword")
129
+ platform.authorize('myUsername', nil, 'myPassword')
130
+ ```
131
+
132
+ #### Authorization Code Grant
133
+
134
+ The 3-legged OAuth 2.0 flow using an authorization code grant is designed for web apps and public apps where authorization needs to be granted by a 3rd party resource owner.
135
+
136
+ ```ruby
137
+ # Initialize SDK with OAuth redirect URI
138
+ rcsdk = RingCentralSdk::Sdk.new(
139
+ 'myAppKey',
140
+ 'myAppSecret',
141
+ RingCentralSdk::Sdk::RC_SERVER_SANDBOX,
142
+ {:redirect_uri => 'http://example.com/oauth'}
143
+ )
144
+ # Retrieve OAuth authorize url using default redirect URL
145
+ auth_url = rcsdk.platform.authorize_url()
146
+ # Retrieve OAuth authorize url using override redirect URL
147
+ auth_url = rcsdk.platform.authorize_url({
148
+ :redirect_uri => 'my_registered_oauth_url', # optional override of default URL
149
+ :display => '', # optional: page|popup|touch|mobile, default 'page'
150
+ :prompt => '', # optional: sso|login|consent, default is 'login sso consent'
151
+ :state => '', # optional
152
+ :brand_id => '' # optional: string|number
153
+ })
154
+ # Open browser window to authorization url and retrieve authorize code from redirect uri.
124
155
  ```
125
156
 
157
+ On your redirect page, you can exchange your authorization code for an access token using the following:
158
+
159
+ ```ruby
160
+ code = params['code'] # retrieve GET 'code' parameter in Sinatra
161
+ rcsdk.platform.authorize_code(code)
162
+ ```
163
+
164
+ For a complete working example, a demo Sinatra app is available in the scripts directory at [scripts/oauth2-sinatra](scripts/oauth2-sinatra).
165
+
126
166
  #### Authentication Lifecycle
127
167
 
128
168
  The platform class performs token refresh procedure automatically if needed. To save the access and refresh tokens between instances of the SDK, you can save and reuse the token as follows:
@@ -138,8 +178,8 @@ After you have saved the token hash, e.g. as JSON, you can reload it in another
138
178
  ```ruby
139
179
  # Reuse token_hash in another SDK instance
140
180
  rcsdk2 = RingCentralSdk::Sdk.new(
141
- "myAppKey",
142
- "myAppSecret",
181
+ 'myAppKey',
182
+ 'myAppSecret',
143
183
  RingCentralSdk::Sdk::RC_SERVER_SANDBOX
144
184
  )
145
185
  # set_token() accepts a hash or OAuth2::AccessToken object
@@ -272,4 +312,4 @@ Please report these on [Github](https://github.com/grokify/ringcentral-sdk-ruby)
272
312
 
273
313
  RingCentral SDK is available under an MIT-style license. See [LICENSE.txt](LICENSE.txt) for details.
274
314
 
275
- RingCentral SDK © 2015 by John Wang
315
+ RingCentral SDK © 2015 by John Wang
data/VERSION.txt CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -1,3 +1,161 @@
1
- module RingCentralSdk::Platform
2
- autoload :Platform, 'ringcentral_sdk/platform/platform'
3
- end
1
+ require 'base64'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'faraday_middleware/oauth2_refresh'
5
+ require 'oauth2'
6
+
7
+ module RingCentralSdk
8
+ class Platform
9
+
10
+ ACCESS_TOKEN_TTL = 600 # 10 minutes
11
+ REFRESH_TOKEN_TTL = 36000 # 10 hours
12
+ REFRESH_TOKEN_TTL_REMEMBER = 604800 # 1 week
13
+ ACCOUNT_PREFIX = '/account/'
14
+ ACCOUNT_ID = '~'
15
+ AUTHZ_ENDPOINT = '/restapi/oauth/authorize'
16
+ TOKEN_ENDPOINT = '/restapi/oauth/token'
17
+ REVOKE_ENDPOINT = '/restapi/oauth/revoke'
18
+ API_VERSION = 'v1.0'
19
+ URL_PREFIX = '/restapi'
20
+
21
+ attr_accessor :server_url
22
+
23
+ attr_reader :client
24
+ attr_reader :oauth2client
25
+ attr_reader :token
26
+ attr_reader :user_agent
27
+ attr_reader :redirect_uri
28
+
29
+ def initialize(app_key='', app_secret='', server_url=RingCentralSdk::Sdk::RC_SERVER_SANDBOX, opts={})
30
+ @app_key = app_key
31
+ @app_secret = app_secret
32
+ @server_url = server_url
33
+ @token = nil
34
+ @client = nil
35
+ @redirect_uri = opts.has_key?(:redirect_uri) ? opts[:redirect_uri] : ''
36
+ @user_agent = get_user_agent()
37
+ @oauth2client = new_oauth2_client()
38
+ end
39
+
40
+ def get_api_version_url()
41
+ return @server_url + URL_PREFIX + '/' + API_VERSION
42
+ end
43
+
44
+ def create_url(url, add_server=false, add_method=nil, add_token=false)
45
+ built_url = ''
46
+ has_http = !url.index('http://').nil? && !url.index('https://').nil?
47
+
48
+ if add_server && ! has_http
49
+ built_url += @server_url
50
+ end
51
+
52
+ if url.index(URL_PREFIX).nil? && ! has_http
53
+ built_url += URL_PREFIX + '/' + API_VERSION + '/'
54
+ end
55
+
56
+ if url.index('/') == 0
57
+ if built_url =~ /\/$/
58
+ built_url += url.gsub(/^\//, '')
59
+ else
60
+ built_url += url
61
+ end
62
+ else # no /
63
+ if built_url =~ /\/$/
64
+ built_url += url
65
+ else
66
+ built_url += '/' + url
67
+ end
68
+ end
69
+
70
+ return built_url
71
+ end
72
+
73
+ def authorize_url(opts={})
74
+ if ! opts.has_key?(:redirect_uri) && @redirect_uri.to_s.length>0
75
+ opts[:redirect_uri] = @redirect_uri.to_s
76
+ end
77
+ @oauth2client.auth_code.authorize_url(opts)
78
+ end
79
+
80
+ def authorize_code(code, opts={})
81
+ if ! opts.has_key?(:redirect_uri) && @redirect_uri.to_s.length>0
82
+ opts[:redirect_uri] = @redirect_uri.to_s
83
+ end
84
+ token = @oauth2client.auth_code.get_token(code, opts)
85
+ set_token(token)
86
+ return token
87
+ end
88
+
89
+ def authorize_password(username='', extension='', password='', remember=false)
90
+ token = @oauth2client.password.get_token(username, password, {
91
+ :extension => extension,
92
+ :headers => { 'Authorization' => 'Basic ' + get_api_key() } })
93
+ set_token(token)
94
+ return token
95
+ end
96
+
97
+ def set_token(token)
98
+ if token.is_a?(Hash)
99
+ token = OAuth2::AccessToken::from_hash(@oauth2client, token)
100
+ end
101
+
102
+ unless token.is_a?(OAuth2::AccessToken)
103
+ raise "Token is not a OAuth2::AccessToken"
104
+ end
105
+
106
+ @token = token
107
+
108
+ @client = Faraday.new(:url => get_api_version_url()) do |conn|
109
+ conn.request :oauth2_refresh, @token
110
+ conn.request :json
111
+ conn.request :url_encoded
112
+ conn.headers['User-Agent'] = @user_agent
113
+ conn.headers['Rc-User-Agent'] = @user_agent
114
+ conn.response :json, :content_type => 'application/json'
115
+ conn.adapter Faraday.default_adapter
116
+ end
117
+ end
118
+
119
+ def new_oauth2_client()
120
+ return OAuth2::Client.new(@app_key, @app_secret,
121
+ :site => @server_url,
122
+ :authorize_url => AUTHZ_ENDPOINT,
123
+ :token_url => TOKEN_ENDPOINT)
124
+ end
125
+
126
+ def get_api_key()
127
+ api_key = (@app_key.is_a?(String) && @app_secret.is_a?(String)) \
128
+ ? Base64.encode64("#{@app_key}:#{@app_secret}").gsub(/[\s\t\r\n]/,'') : ''
129
+ return api_key
130
+ end
131
+
132
+ def request(helper=nil)
133
+ unless helper.is_a?(RingCentralSdk::Helpers::Request)
134
+ raise 'Request is not a RingCentralSdk::Helpers::Request'
135
+ end
136
+
137
+ if helper.method.downcase == 'post'
138
+ resp = @client.post do |req|
139
+ req.url helper.url
140
+ req.headers['Content-Type'] = helper.content_type if helper.content_type
141
+ req.body = helper.body if helper.body
142
+ end
143
+ return resp
144
+ end
145
+ return nil
146
+ end
147
+
148
+ def get_user_agent()
149
+ ua = "ringcentral-sdk-ruby/#{RingCentralSdk::VERSION} %s/%s %s" % [
150
+ (RUBY_ENGINE rescue nil or "ruby"),
151
+ RUBY_VERSION,
152
+ RUBY_PLATFORM
153
+ ]
154
+ return ua.strip
155
+ end
156
+
157
+ alias_method :authorize, :authorize_password
158
+ alias_method :login, :authorize_password
159
+ private :get_api_version_url
160
+ end
161
+ end
@@ -6,12 +6,13 @@ module RingCentralSdk
6
6
 
7
7
  attr_reader :platform
8
8
 
9
- def initialize(app_key=nil,app_secret=nil,server_url=nil,username=nil,extension=nil,password=nil)
9
+ def initialize(app_key=nil,app_secret=nil,server_url=nil,opts={})
10
10
  use_pubnub_mock = false
11
11
 
12
- @platform = RingCentralSdk::Platform::Platform.new(app_key, app_secret, server_url)
13
- if not username.nil? and not password.nil?
14
- @platform.authorize(username, extension, password)
12
+ @platform = RingCentralSdk::Platform.new(app_key, app_secret, server_url, opts)
13
+ if opts.has_key?(:username) && opts.has_key?(:password)
14
+ extension = opts.has_key?(:extension) ? opts[:extension] : ''
15
+ @platform.authorize(opts[:username], extension, opts[:password])
15
16
  end
16
17
 
17
18
  @_pubnub_factory = RingCentralSdk::PubnubFactory.new(use_pubnub_mock)
@@ -1,7 +1,7 @@
1
1
  require 'base64'
2
2
  require 'multi_json'
3
3
  require 'observer'
4
- require 'timers' # Also see http://ruby-doc.org/stdlib-2.2.3/libdoc/timeout/rdoc/Timeout.html
4
+ require 'timers'
5
5
 
6
6
  module RingCentralSdk
7
7
  class Subscription
@@ -9,10 +9,12 @@ module RingCentralSdk
9
9
 
10
10
  RENEW_HANDICAP = 60
11
11
 
12
+ attr_reader :event_filters
13
+
12
14
  def initialize(platform, pubnub_factory)
13
15
  @_platform = platform
14
16
  @_pubnub_factory = pubnub_factory
15
- @_event_filters = []
17
+ @event_filters = []
16
18
  @_timeout = nil
17
19
  @_subscription = nil_subscription()
18
20
  @_pubnub = nil
@@ -43,27 +45,27 @@ module RingCentralSdk
43
45
  end
44
46
 
45
47
  def register(events=nil)
46
- return alive() ? renew(events) : subscribe(events)
48
+ return alive?() ? renew(events) : subscribe(events)
47
49
  end
48
50
 
49
51
  def add_events(events)
50
52
  unless events.is_a?(Array)
51
53
  raise 'Events is not an array.'
52
54
  end
53
- @_event_filters.push(events) if events.length>0
55
+ @event_filters.push(events) if events.length>0
54
56
  end
55
57
 
56
58
  def set_events(events)
57
59
  unless events.is_a?(Array)
58
60
  raise 'Events is not an array.'
59
61
  end
60
- @_event_filters.push(*events) if events.length>0
62
+ @event_filters = events
61
63
  end
62
64
 
63
65
  def subscribe(events=nil)
64
66
  set_events(events) if events.is_a?(Array)
65
67
 
66
- if !@_event_filters.is_a?(Array) || @_event_filters.length ==0
68
+ if !@event_filters.is_a?(Array) || @event_filters.length ==0
67
69
  raise 'Events are undefined'
68
70
  end
69
71
 
@@ -95,11 +97,11 @@ module RingCentralSdk
95
97
  def renew(events=nil)
96
98
  set_events(events) if events.is_a?(Array)
97
99
 
98
- unless alive()
100
+ unless alive?()
99
101
  raise 'Subscription is not alive'
100
102
  end
101
103
 
102
- if !@_event_filters.is_a?(Array) || @_event_filters.length ==0
104
+ if !@event_filters.is_a?(Array) || @event_filters.length ==0
103
105
  raise 'Events are undefined'
104
106
  end
105
107
 
@@ -126,7 +128,7 @@ module RingCentralSdk
126
128
  end
127
129
 
128
130
  def remove()
129
- unless alive()
131
+ unless alive?()
130
132
  raise 'Subscription is not alive'
131
133
  end
132
134
 
@@ -145,11 +147,14 @@ module RingCentralSdk
145
147
  end
146
148
  end
147
149
 
148
- def alive()
150
+ def alive?()
149
151
  s = @_subscription
150
152
  return (s.has_key?('deliveryMode') && s['deliveryMode']) && \
151
153
  (s['deliveryMode'].has_key?('subscriberKey') && s['deliveryMode']['subscriberKey']) && \
152
- (s['deliveryMode'].has_key?('address') && s['deliveryMode']['address'])
154
+ (
155
+ s['deliveryMode'].has_key?('address') && s['deliveryMode']['address'] && \
156
+ s['deliveryMode']['address'].length>0) \
157
+ ? true : false
153
158
  end
154
159
 
155
160
  def subscription()
@@ -165,16 +170,15 @@ module RingCentralSdk
165
170
  def reset()
166
171
  _clear_timeout()
167
172
  _unsubscribe_at_pubnub()
168
- _subscription = nil
173
+ @_subscription = nil_subscription()
169
174
  end
170
175
 
171
176
  def destroy()
172
177
  reset()
173
- off()
174
178
  end
175
179
 
176
180
  def _subscribe_at_pubnub()
177
- unless alive()
181
+ if ! alive?()
178
182
  raise 'Subscription is not alive'
179
183
  end
180
184
 
@@ -187,29 +191,13 @@ module RingCentralSdk
187
191
  notify_observers('GOT_PUBNUB_MESSAGE_NOTIFY')
188
192
  }
189
193
 
190
- error = lambda { |envelope|
191
- puts('ERROR : ' + envelope.msg.to_s) # FIXME Handle this
192
- }
193
-
194
- connect = lambda { |envelope|
195
- puts('CONNECTED')
196
- }
197
-
198
- reconnect = lambda { |envelope|
199
- puts('RECONNECTED')
200
- }
201
-
202
- disconnect = lambda { |envelope|
203
- puts('DISCONNECTED')
204
- }
205
-
206
194
  @_pubnub.subscribe(
207
195
  :channel => @_subscription['deliveryMode']['address'],
208
196
  :callback => callback,
209
- :error => error,
210
- :connect => connect,
211
- :reconnect => reconnect,
212
- :disconnect => disconnect
197
+ :error => lambda { |envelope| puts('ERROR: ' + envelope.msg.to_s) },
198
+ :connect => lambda { |envelope| puts('CONNECTED') },
199
+ :reconnect => lambda { |envelope| puts('RECONNECTED') },
200
+ :disconnect => lambda { |envelope| puts('DISCONNECTED') }
213
201
  )
214
202
  end
215
203
 
@@ -220,18 +208,12 @@ module RingCentralSdk
220
208
  end
221
209
 
222
210
  def _decrypt(message)
223
- unless alive()
211
+ unless alive?()
224
212
  raise 'Subscription is not alive'
225
213
  end
226
214
 
227
- delivery_mode = @_subscription['deliveryMode']
228
-
229
- is_encrypted = delivery_mode.has_key?('encryption') && \
230
- delivery_mode['encryption'] && \
231
- delivery_mode.has_key?('encryptionKey') && \
232
- delivery_mode['encryptionKey']
233
-
234
- if is_encrypted
215
+ if _encrypted?()
216
+ delivery_mode = @_subscription['deliveryMode']
235
217
  key = Base64.decode64(delivery_mode['encryptionKey'])
236
218
  ciphertext = Base64.decode64(message)
237
219
 
@@ -247,15 +229,24 @@ module RingCentralSdk
247
229
  return message
248
230
  end
249
231
 
232
+ def _encrypted?()
233
+ delivery_mode = @_subscription['deliveryMode']
234
+ is_encrypted = delivery_mode.has_key?('encryption') && \
235
+ delivery_mode['encryption'] && \
236
+ delivery_mode.has_key?('encryptionKey') && \
237
+ delivery_mode['encryptionKey']
238
+ return is_encrypted
239
+ end
240
+
250
241
  def _unsubscribe_at_pubnub()
251
- if @_pubnub && alive()
242
+ if @_pubnub && alive?()
252
243
  @_pubnub.unsubscribe(@_subscription['deliveryMode']['address'])
253
244
  end
254
245
  end
255
246
 
256
247
  def _get_full_events_filter()
257
248
  full_events_filter = []
258
- @_event_filters.each do |filter|
249
+ @event_filters.each do |filter|
259
250
  if filter.to_s
260
251
  full_events_filter.push(@_platform.create_url(filter.to_s))
261
252
  end
@@ -1,3 +1,3 @@
1
1
  module RingCentralSdk
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
data/test/test_setup.rb CHANGED
@@ -10,7 +10,11 @@ class RingCentralSdkTest < Test::Unit::TestCase
10
10
  )
11
11
 
12
12
  assert_equal "RingCentralSdk::Sdk", rcsdk.class.name
13
- assert_equal "RingCentralSdk::Platform::Platform", rcsdk.platform.class.name
13
+ assert_equal "RingCentralSdk::Platform", rcsdk.platform.class.name
14
+
15
+ assert_raise do
16
+ rcsdk.request(nil)
17
+ end
14
18
 
15
19
  end
16
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ringcentral_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Wang
@@ -180,7 +180,6 @@ files:
180
180
  - lib/ringcentral_sdk/helpers/inflator/contact_info.rb
181
181
  - lib/ringcentral_sdk/helpers/request.rb
182
182
  - lib/ringcentral_sdk/platform.rb
183
- - lib/ringcentral_sdk/platform/platform.rb
184
183
  - lib/ringcentral_sdk/pubnub_factory.rb
185
184
  - lib/ringcentral_sdk/sdk.rb
186
185
  - lib/ringcentral_sdk/subscription.rb
@@ -1,131 +0,0 @@
1
- require 'base64'
2
- require 'faraday'
3
- require 'faraday_middleware'
4
- require 'faraday_middleware/oauth2_refresh'
5
- require 'oauth2'
6
-
7
- module RingCentralSdk::Platform
8
- class Platform
9
-
10
- ACCESS_TOKEN_TTL = 600 # 10 minutes
11
- REFRESH_TOKEN_TTL = 36000 # 10 hours
12
- REFRESH_TOKEN_TTL_REMEMBER = 604800 # 1 week
13
- ACCOUNT_PREFIX = '/account/'
14
- ACCOUNT_ID = '~'
15
- TOKEN_ENDPOINT = '/restapi/oauth/token'
16
- REVOKE_ENDPOINT = '/restapi/oauth/revoke'
17
- API_VERSION = 'v1.0'
18
- URL_PREFIX = '/restapi'
19
-
20
- attr_accessor :server_url
21
-
22
- attr_reader :client
23
- attr_reader :token
24
- attr_reader :user_agent
25
-
26
- def initialize(app_key='', app_secret='', server_url=RingCentralSdk::Sdk::RC_SERVER_SANDBOX)
27
- @app_key = app_key
28
- @app_secret = app_secret
29
- @server_url = server_url
30
- @token = nil
31
- @client = nil
32
- @user_agent = get_user_agent()
33
- end
34
-
35
- def get_api_version_url()
36
- return @server_url + URL_PREFIX + '/' + API_VERSION
37
- end
38
-
39
- def create_url(url, add_server=false, add_method=nil, add_token=false)
40
- built_url = ''
41
- has_http = !url.index('http://').nil? && !url.index('https://').nil?
42
-
43
- if add_server && ! has_http
44
- built_url += @server_url
45
- end
46
-
47
- if url.index(URL_PREFIX).nil? && ! has_http
48
- built_url += URL_PREFIX + '/' + API_VERSION
49
- end
50
-
51
- built_url += url
52
-
53
- return built_url
54
- end
55
-
56
- def login(username, extension, password)
57
- return authorize(username, extension, password)
58
- end
59
-
60
- def authorize(username='', extension='', password='', remember=false)
61
- oauth2client = new_oauth2_client()
62
-
63
- token = oauth2client.password.get_token(username, password, {
64
- :extension => extension,
65
- :headers => { 'Authorization' => 'Basic ' + get_api_key() } })
66
-
67
- set_token(token)
68
- end
69
-
70
- def set_token(token)
71
- if token.is_a?(Hash)
72
- token = OAuth2::AccessToken::from_hash(new_oauth2_client(), token)
73
- end
74
-
75
- unless token.is_a?(OAuth2::AccessToken)
76
- raise "Token is not a OAuth2::AccessToken"
77
- end
78
-
79
- @token = token
80
-
81
- @client = Faraday.new(:url => get_api_version_url()) do |conn|
82
- conn.request :oauth2_refresh, @token
83
- conn.request :json
84
- conn.request :url_encoded
85
- conn.headers['User-Agent'] = @user_agent
86
- conn.headers['Rc-User-Agent'] = @user_agent
87
- conn.response :json, :content_type => 'application/json'
88
- conn.adapter Faraday.default_adapter
89
- end
90
- end
91
-
92
- def new_oauth2_client()
93
- return OAuth2::Client.new(@app_key, @app_secret,
94
- :site => @server_url,
95
- :token_url => TOKEN_ENDPOINT)
96
- end
97
-
98
- def get_api_key()
99
- api_key = (@app_key.is_a?(String) && @app_secret.is_a?(String)) \
100
- ? Base64.encode64("#{@app_key}:#{@app_secret}").gsub(/[\s\t\r\n]/,'') : ''
101
- return api_key
102
- end
103
-
104
- def request(helper=nil)
105
- unless helper.is_a?(RingCentralSdk::Helpers::Request)
106
- raise 'Request is not a RingCentralSdk::Helpers::Request'
107
- end
108
-
109
- if helper.method.downcase == 'post'
110
- resp = @client.post do |req|
111
- req.url helper.url
112
- req.headers['Content-Type'] = helper.content_type if helper.content_type
113
- req.body = helper.body if helper.body
114
- end
115
- return resp
116
- end
117
- return nil
118
- end
119
-
120
- def get_user_agent()
121
- ua = "ringcentral-sdk-ruby/#{RingCentralSdk::VERSION} %s/%s %s" % [
122
- (RUBY_ENGINE rescue nil or "ruby"),
123
- RUBY_VERSION,
124
- RUBY_PLATFORM
125
- ]
126
- return ua.strip
127
- end
128
-
129
- private :get_api_version_url
130
- end
131
- end