ringcentral_sdk 0.4.0 → 0.5.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: df84f0ee4eab215d88476494ab7ff4681c59fe34
4
- data.tar.gz: 0bcb1c96920506ee4d63d4a162c9cab33a25078d
3
+ metadata.gz: 1f9dcd84f1f05d75c88713e2670ef117ff10fbbc
4
+ data.tar.gz: 592200667132fc64debd755707acb7b89e820926
5
5
  SHA512:
6
- metadata.gz: 89675600de9040918b4fead9d4134d00d2107ff4501e81108189126452201a6c2b7d1130e9a92921dfe744f39fd84b190c25cc6a34ceec65758649f63d2d1733
7
- data.tar.gz: d64154488b9a64cdf2385112b1a2b548a6092f15c57b1550eecdbae005b19f080343108f917d07ff281603d44468cc7d4a3dad211b85b3966286e78e433036fa
6
+ metadata.gz: fdb5b49fd8ee320bf2309e976a106f4d721251e5b355b523c358394e6341ea50e6f8e5b869b40988efff9bba687a1b3a66860c132eaa782f7c5c2631a021a2f3
7
+ data.tar.gz: 0d84fb441b6be1b14fd92c6a164856ec3b80a6762c822bf8dad71adc7c8e5207f463124b40997d78f9ddf0704eaee17dfdc25d2ac1ac2645489e1b917031d5df
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  CHANGELOG
2
2
  ---------
3
+ - **2015-10-18**: 0.5.0
4
+ - Add stubbed tests via `mocha`
5
+ - Add `RingCentralSdk.new()` sugar method
6
+ - Modify `RingCentralSdk::RC_SERVER_PRODUCTION` url from `RingCentralSdk::Sdk::RC_SERVER_Production`
7
+ - Remove `lib/ringcentral_sdk/sdk.rb` file and `RingCentralSdk::Sdk` class
8
+ - Remove `lib/ringcentral_sdk/version.rb` file
9
+ - Remove `VERSION.txt` file
3
10
  - **2015-10-15**: 0.4.0
4
11
  - Add 3-Legged OAuth support via platform `authorize_url` and `authorize_code` methods
5
12
  - Add `oauth2-sinatra` demo app in `scripts` directory
data/Gemfile ADDED
@@ -0,0 +1,26 @@
1
+ source ENV['GEM_SOURCE'] || "https://rubygems.org"
2
+
3
+ gem 'coveralls'
4
+ gem 'faraday', '~> 0.9.0'
5
+ gem 'faraday_middleware'
6
+ gem 'faraday_middleware-oauth2_refresh'
7
+ gem 'logger'
8
+ gem 'mime'
9
+ gem 'mime-types'
10
+ gem 'mocha'
11
+ gem 'multi_json'
12
+ gem 'oauth2'
13
+ gem 'pubnub', '~> 3.7.3'
14
+ gem 'rake'
15
+ gem 'simplecov'
16
+ gem 'test-unit'
17
+ gem 'timers'
18
+
19
+ # Ruby 1.8.7 is not currently supported
20
+ # platforms :ruby_18 do
21
+ # gem 'mime-types', '~> 1.25'
22
+ # gem 'rest-client', '~> 1.6.0'
23
+ # # gem 'mime-types', '~> 2.5'
24
+ # end
25
+
26
+ # vim:ft=ruby
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
- RingCentral SDK
2
- ===============
1
+ RingCentral SDK for Ruby
2
+ ========================
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/ringcentral_sdk.svg)](http://badge.fury.io/rb/ringcentral_sdk)
5
5
  [![Build Status](https://img.shields.io/travis/grokify/ringcentral-sdk-ruby/master.svg)](https://travis-ci.org/grokify/ringcentral-sdk-ruby)
6
- [![Dependency Status](https://gemnasium.com/bogdanRada/ruby-gem-downloads-badge.svg)](https://gemnasium.com/bogdanRada/ruby-gem-downloads-badge)
6
+ [![Coverage Status](https://coveralls.io/repos/grokify/ringcentral-sdk-ruby/badge.svg?branch=master)](https://coveralls.io/r/grokify/ringcentral-sdk-ruby?branch=master)
7
+ [![Dependency Status](https://gemnasium.com/grokify/ringcentral-sdk-ruby.svg)](https://gemnasium.com/grokify/ringcentral-sdk-ruby)
7
8
  [![Code Climate](https://codeclimate.com/github/grokify/ringcentral-sdk-ruby/badges/gpa.svg)](https://codeclimate.com/github/grokify/ringcentral-sdk-ruby)
8
9
  [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/grokify/ringcentral-sdk-ruby/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/grokify/ringcentral-sdk-ruby/?branch=master)
9
- [![Coverage Status](https://coveralls.io/repos/grokify/ringcentral-sdk-ruby/badge.svg?branch=master)](https://coveralls.io/r/grokify/ringcentral-sdk-ruby?branch=master)
10
10
  [![Docs](https://img.shields.io/badge/docs-readthedocs-blue.svg)](http://ringcentral-sdk-ruby.readthedocs.org/)
11
11
  [![Docs](https://img.shields.io/badge/docs-rubydoc-blue.svg)](http://www.rubydoc.info/gems/ringcentral_sdk/)
12
12
  [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/grokify/ringcentral-sdk-ruby/master/LICENSE.txt)
@@ -18,19 +18,22 @@ RingCentral SDK
18
18
  2. [To Do](#to-do)
19
19
  2. [Documentation](#documentation)
20
20
  3. [Installation](#installation)
21
- 2. [Usage](#usage)
21
+ 4. [Usage](#usage)
22
22
  1. [Instantiation](#instantiation)
23
23
  1. [Authorization](#authorization)
24
24
  1. [Password Grant](#password-grant)
25
25
  1. [Authorization Code Grant](#authorization-code-grant)
26
26
  1. [API Requests](#api-requests)
27
- 1. [SMS Example](#sms-example)
28
- 2. [Fax Example](#fax-example)
27
+ 1. [Generic HTTP Requests](#generic-http-requests)
28
+ 2. [SMS Example](#sms-example)
29
+ 3. [Fax Example](#fax-example)
29
30
  1. [Subscriptions](#subscriptions)
30
- 5. [Change Log](#change-log)
31
- 6. [Links](#links)
32
- 7. [Contributions](#contributions)
33
- 8. [License](#license)
31
+ 5. [Supported Ruby Versions](#supported-ruby-versions)
32
+ 6. [Versioning](#versioning)
33
+ 7. [Change Log](#change-log)
34
+ 8. [Links](#links)
35
+ 9. [Contributions](#contributions)
36
+ 10. [License](#license)
34
37
 
35
38
  ## Overview
36
39
 
@@ -42,17 +45,16 @@ This SDK is an early stage library and subject to breaking changes.
42
45
 
43
46
  ### Included
44
47
 
45
- * OAuth2 authorization code and password grant flows including token refresh via INTRIDEA `OAuth2::AccessToken`
46
- * Generic API requests handled via `Faraday` client
47
- * Subscription support via `Pubnub` with auto-decryption
48
+ * Authorization via `OAuth2` with authorization code and password grant flows including token refresh
49
+ * Generic API requests via `Faraday` client
50
+ * Subscriptions via `Pubnub` with auto-decryption
48
51
  * Fax request helper to create `multipart/mixed` messages
52
+ * Unit test suite via `Test::Unit` and `mocha`
49
53
  * Docs via [Read the Docs](http://ringcentral-sdk-ruby.readthedocs.org/) and [RubyDoc](http://www.rubydoc.info/gems/ringcentral_sdk/)
50
54
 
51
55
  ### To Do
52
56
 
53
- The following items are still needed for this SDK. Contributions are most welcome.
54
-
55
- * Mock tests
57
+ There are no major to do items at this time.
56
58
 
57
59
  ## Documentation
58
60
 
@@ -100,18 +102,18 @@ This provides a very basic guide to using the SDK. Please use the following reso
100
102
 
101
103
  The RingCentral server URLs can be populated manually or via the included constants:
102
104
 
103
- * `RingCentralSdk::Sdk::RC_SERVER_PRODUCTION`
104
- * `RingCentralSdk::Sdk::RC_SERVER_SANDBOX`
105
+ * `RingCentralSdk::RC_SERVER_PRODUCTION`
106
+ * `RingCentralSdk::RC_SERVER_SANDBOX`
105
107
 
106
108
  ```ruby
107
109
  require 'ringcentral_sdk'
108
110
 
109
- rcsdk = RingCentralSdk::Sdk.new(
111
+ # Returns RingCentralSdk::Platform instance
112
+ rcsdk = RingCentralSdk.new(
110
113
  'myAppKey',
111
114
  'myAppSecret',
112
- RingCentralSdk::Sdk::RC_SERVER_SANDBOX
115
+ RingCentralSdk::RC_SERVER_SANDBOX
113
116
  )
114
- platform = rcsdk.platform
115
117
  ```
116
118
 
117
119
  ### Authorization
@@ -122,29 +124,41 @@ The 2-legged OAuth 2.0 flow using a password grant is designed for server applic
122
124
 
123
125
  ```ruby
124
126
  # Initialize using main phone number and extension number
125
- platform.authorize('myUsername', 'myExtension', 'myPassword')
127
+ rcsdk.authorize('myUsername', 'myExtension', 'myPassword')
126
128
 
127
129
  # Initialize using user phone number without extension number
128
130
  # Extension defaults to company admin extension
129
- platform.authorize('myUsername', nil, 'myPassword')
131
+ rcsdk.authorize('myUsername', nil, 'myPassword')
130
132
  ```
131
133
 
132
134
  #### Authorization Code Grant
133
135
 
134
136
  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
137
 
138
+ Using the default authorization URL:
139
+
136
140
  ```ruby
137
141
  # Initialize SDK with OAuth redirect URI
138
- rcsdk = RingCentralSdk::Sdk.new(
142
+ rcsdk = RingCentralSdk.new(
139
143
  'myAppKey',
140
144
  'myAppSecret',
141
- RingCentralSdk::Sdk::RC_SERVER_SANDBOX,
145
+ RingCentralSdk::RC_SERVER_SANDBOX,
142
146
  {:redirect_uri => 'http://example.com/oauth'}
143
147
  )
144
148
  # Retrieve OAuth authorize url using default redirect URL
145
- auth_url = rcsdk.platform.authorize_url()
149
+ auth_url = rcsdk.authorize_url()
150
+ ```
151
+
152
+ Customizing authorize URL:
153
+
154
+ ```ruby
155
+ rcsdk = RingCentralSdk.new(
156
+ 'myAppKey',
157
+ 'myAppSecret',
158
+ RingCentralSdk::RC_SERVER_SANDBOX
159
+ )
146
160
  # Retrieve OAuth authorize url using override redirect URL
147
- auth_url = rcsdk.platform.authorize_url({
161
+ auth_url = rcsdk.authorize_url({
148
162
  :redirect_uri => 'my_registered_oauth_url', # optional override of default URL
149
163
  :display => '', # optional: page|popup|touch|mobile, default 'page'
150
164
  :prompt => '', # optional: sso|login|consent, default is 'login sso consent'
@@ -158,7 +172,7 @@ On your redirect page, you can exchange your authorization code for an access to
158
172
 
159
173
  ```ruby
160
174
  code = params['code'] # retrieve GET 'code' parameter in Sinatra
161
- rcsdk.platform.authorize_code(code)
175
+ rcsdk.authorize_code(code)
162
176
  ```
163
177
 
164
178
  For a complete working example, a demo Sinatra app is available in the scripts directory at [scripts/oauth2-sinatra](scripts/oauth2-sinatra).
@@ -170,20 +184,20 @@ The platform class performs token refresh procedure automatically if needed. To
170
184
  ```ruby
171
185
  # Retrieve and save access token when program is to be stopped
172
186
  # `token` is an `OAuth2::AccessToken` object
173
- token_hash = rcsdk.platform.token.to_hash
187
+ token_hash = rcsdk.token.to_hash
174
188
  ```
175
189
 
176
190
  After you have saved the token hash, e.g. as JSON, you can reload it in another instance of the SDK as follows:
177
191
 
178
192
  ```ruby
179
193
  # Reuse token_hash in another SDK instance
180
- rcsdk2 = RingCentralSdk::Sdk.new(
194
+ rcsdk2 = RingCentralSdk.new(
181
195
  'myAppKey',
182
196
  'myAppSecret',
183
- RingCentralSdk::Sdk::RC_SERVER_SANDBOX
197
+ RingCentralSdk::RC_SERVER_SANDBOX
184
198
  )
185
199
  # set_token() accepts a hash or OAuth2::AccessToken object
186
- rcsdk2.platform.set_token(token_hash)
200
+ rcsdk2.set_token(token_hash)
187
201
  ```
188
202
 
189
203
  Important! You have to manually maintain synchronization of SDK's between requests if you share authentication. When two simultaneous requests will perform refresh, only one will succeed. One of the solutions would be to have semaphor and pause other pending requests while one of them is performing refresh.
@@ -192,20 +206,33 @@ See [the authorization docs](http://ringcentral-sdk-ruby.readthedocs.org/en/late
192
206
 
193
207
  ### API Requests
194
208
 
195
- Requests are made using the inclued Faraday client which you can
196
- retrieve by calling `rcsdk.platform.client` or using it directly.
209
+ API requests can be made via the included `Faraday` client or `RingCentralSdk::Helpers::Request` subclasses. These are described below.
210
+
211
+ #### Generic HTTP Requests
212
+
213
+ To make generic API requests, use included `Faraday` client which can be accessed via `rcsdk.client`. The client automatically adds the correct access token to the HTTP request and handles OAuth token refresh using the `OAuth` gem.
197
214
 
198
215
  ```ruby
199
- client = rcsdk.platform.client
216
+ client = rcsdk.client
200
217
  ```
201
218
 
219
+ Faraday request middleware has been loaded for the following:
220
+
221
+ * `:oauth2_refresh`
222
+ * `:json`
223
+ * `:url_encoded`
224
+
225
+ Faraday response middleware is loaded for the following:
226
+
227
+ * `:json`
228
+
202
229
  #### SMS Example
203
230
 
204
231
  SMS and other requests can be easily sent directly without helpers.
205
232
 
206
233
  ```ruby
207
234
  # SMS Example
208
- response = client.post do |req|
235
+ response = rcsdk.client.post do |req|
209
236
  req.url 'account/~/extension/~/sms'
210
237
  req.headers['Content-Type'] = 'application/json'
211
238
  req.body = {
@@ -219,7 +246,7 @@ end
219
246
  #### Fax Example
220
247
 
221
248
  Request helpers are subclasses of `RingCentralSdk::Helpers::Request` and provide standard methods
222
- that can be called by the `.request()` method of the SDK and Platform objects. This enables the
249
+ that can be called by the `.request()` method of the Platform object. This enables the
223
250
  requisite information for Faraday to be generated in a standard way.
224
251
 
225
252
  To create your own request helpers, please take a look at the fax one shown below:
@@ -242,11 +269,10 @@ fax = RingCentralSdk::Helpers::CreateFaxRequest.new(
242
269
  )
243
270
 
244
271
  # sending request via request helper methods
245
- response = sdk.request(fax)
246
- response = sdk.platform.request(fax)
272
+ response = rcsdk.request(fax)
247
273
 
248
274
  # sending request via standard Faraday client
249
- response = client.post do |req|
275
+ response = rcsdk.client.post do |req|
250
276
  req.url fax.url
251
277
  req.headers['Content-Type'] = fax.content_type
252
278
  req.body = fax.body
@@ -274,6 +300,19 @@ sub.add_observer(MyObserver.new())
274
300
  sub.destroy() # end the subscription
275
301
  ```
276
302
 
303
+ ## Supported Ruby Versions
304
+
305
+ This library supports and is [tested against](https://travis-ci.org/grokify/ringcentral-sdk-ruby) the following Ruby implementations:
306
+
307
+ * Ruby 2.2.3, 2.2.0
308
+ * Ruby 2.1.7, 2.1.0
309
+ * Ruby 2.0.0
310
+ * Ruby 1.9.3
311
+ * [JRuby](http://jruby.org/)
312
+ * [Rubinius](http://rubini.us/)
313
+
314
+ Note: Ruby 1.8.7 works except for subscription support which relies on the `pubnub` gem. If there is a need for 1.8.7 support, consider creating a GitHub issue so we can evalute creating a separate library for subscription handling.
315
+
277
316
  ## Versioning
278
317
 
279
318
  This project is currently in development and the API can change. During initial development (Version 0.x.x), minor version changes will indicate either substantial feature inclusion or breaking changes.
@@ -1,9 +1,18 @@
1
- require 'ringcentral_sdk/version.rb'
2
-
3
1
  module RingCentralSdk
2
+
3
+ VERSION = '0.5.0'
4
+
5
+ RC_SERVER_PRODUCTION = 'https://platform.ringcentral.com'
6
+ RC_SERVER_SANDBOX = 'https://platform.devtest.ringcentral.com'
7
+
4
8
  autoload :Helpers, 'ringcentral_sdk/helpers'
5
9
  autoload :Platform, 'ringcentral_sdk/platform'
6
- autoload :PubnubFactory, 'ringcentral_sdk/pubnub_factory'
7
- autoload :Sdk, 'ringcentral_sdk/sdk'
8
10
  autoload :Subscription, 'ringcentral_sdk/subscription'
11
+
12
+ class << self
13
+ def new(app_key, app_secret, server_url=nil, opts={})
14
+ RingCentralSdk::Platform.new(app_key, app_secret, server_url, opts)
15
+ end
16
+ end
17
+
9
18
  end
@@ -62,15 +62,14 @@ module RingCentralSdk::Helpers
62
62
  @msg.add(text_part)
63
63
  end
64
64
 
65
- def add_file(file_name=nil, content_type=nil, base64_encode=false)
65
+ def add_file(file_name, content_type=nil, base64_encode=false)
66
66
  file_part = get_file_part(file_name, content_type, base64_encode)
67
67
 
68
68
  @msg.add(file_part)
69
69
  return true
70
70
  end
71
71
 
72
- def get_file_part(file_name=nil, content_type=nil, base64_encode=false)
73
-
72
+ def get_file_part(file_name, content_type=nil, base64_encode=false)
74
73
  file_bytes = get_file_bytes(file_name)
75
74
 
76
75
  file_part = base64_encode \
@@ -85,17 +84,17 @@ module RingCentralSdk::Helpers
85
84
  end
86
85
 
87
86
  def get_file_bytes(file_name=nil)
88
-
89
87
  unless File.file?(file_name.to_s)
90
88
  raise "File \"#{file_name.to_s}\" does not exist or cannot be read"
91
89
  end
92
90
 
93
- file_bytes = RUBY_VERSION < '1.9' \
94
- ? File.open(file_name, 'rb') { |f| f.read } \
95
- : File.open(file_name, 'rb:BINARY') { |f| f.read }
91
+ # file_bytes = RUBY_VERSION < '1.9' \ # 1.8.7 removed due to subscription
92
+ # ? File.open(file_name, 'rb') { |f| f.read } \
93
+ # : File.open(file_name, 'rb:BINARY') { |f| f.read }
96
94
 
97
- return file_bytes
95
+ file_bytes = File.open(file_name, 'rb:BINARY') { |f| f.read }
98
96
 
97
+ return file_bytes
99
98
  end
100
99
 
101
100
  def get_file_content_type(file_name=nil, content_type=nil)
@@ -26,15 +26,22 @@ module RingCentralSdk
26
26
  attr_reader :user_agent
27
27
  attr_reader :redirect_uri
28
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
29
+ def initialize(app_key, app_secret, server_url=RingCentralSdk::RC_SERVER_SANDBOX, opts={})
30
+ @app_key = app_key.to_s
31
+ @app_secret = app_secret.to_s
32
32
  @server_url = server_url
33
33
  @token = nil
34
34
  @client = nil
35
- @redirect_uri = opts.has_key?(:redirect_uri) ? opts[:redirect_uri] : ''
35
+ @redirect_uri = ''
36
36
  @user_agent = get_user_agent()
37
37
  @oauth2client = new_oauth2_client()
38
+ if opts.is_a?(Hash)
39
+ @redirect_uri = opts.has_key?(:redirect_uri) ? opts[:redirect_uri] : ''
40
+ if opts.has_key?(:username) && opts.has_key?(:password)
41
+ extension = opts.has_key?(:extension) ? opts[:extension] : ''
42
+ authorize_password(opts[:username], extension, opts[:password])
43
+ end
44
+ end
38
45
  end
39
46
 
40
47
  def get_api_version_url()
@@ -70,6 +77,17 @@ module RingCentralSdk
70
77
  return built_url
71
78
  end
72
79
 
80
+ def create_urls(urls, add_server=false, add_method=nil, add_token=false)
81
+ unless urls.is_a?(Array)
82
+ raise "URLs is not an array"
83
+ end
84
+ built_urls = []
85
+ urls.each do |url|
86
+ built_urls.push(create_url(url, add_server, add_method, add_token))
87
+ end
88
+ return built_urls
89
+ end
90
+
73
91
  def authorize_url(opts={})
74
92
  if ! opts.has_key?(:redirect_uri) && @redirect_uri.to_s.length>0
75
93
  opts[:redirect_uri] = @redirect_uri.to_s
@@ -86,7 +104,7 @@ module RingCentralSdk
86
104
  return token
87
105
  end
88
106
 
89
- def authorize_password(username='', extension='', password='', remember=false)
107
+ def authorize_password(username, extension='', password='', remember=false)
90
108
  token = @oauth2client.password.get_token(username, password, {
91
109
  :extension => extension,
92
110
  :headers => { 'Authorization' => 'Basic ' + get_api_key() } })
@@ -111,7 +129,7 @@ module RingCentralSdk
111
129
  conn.request :url_encoded
112
130
  conn.headers['User-Agent'] = @user_agent
113
131
  conn.headers['Rc-User-Agent'] = @user_agent
114
- conn.response :json, :content_type => 'application/json'
132
+ conn.response :json, :content_type => /\bjson$/
115
133
  conn.adapter Faraday.default_adapter
116
134
  end
117
135
  end
@@ -123,6 +141,16 @@ module RingCentralSdk
123
141
  :token_url => TOKEN_ENDPOINT)
124
142
  end
125
143
 
144
+ def set_oauth2_client(client=nil)
145
+ if client.nil?
146
+ @oauth2client = new_oauth2_client()
147
+ elsif client.is_a?(OAuth2::Client)
148
+ @oauth2client = client
149
+ else
150
+ raise "client is not an OAuth2::Client"
151
+ end
152
+ end
153
+
126
154
  def get_api_key()
127
155
  api_key = (@app_key.is_a?(String) && @app_secret.is_a?(String)) \
128
156
  ? Base64.encode64("#{@app_key}:#{@app_secret}").gsub(/[\s\t\r\n]/,'') : ''
@@ -142,7 +170,6 @@ module RingCentralSdk
142
170
  end
143
171
  return resp
144
172
  end
145
- return nil
146
173
  end
147
174
 
148
175
  def get_user_agent()
@@ -154,6 +181,10 @@ module RingCentralSdk
154
181
  return ua.strip
155
182
  end
156
183
 
184
+ def create_subscription()
185
+ return RingCentralSdk::Subscription.new(self)
186
+ end
187
+
157
188
  alias_method :authorize, :authorize_password
158
189
  alias_method :login, :authorize_password
159
190
  private :get_api_version_url