ringcentral-sdk 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -1
- data/lib/ringcentral.rb +33 -40
- data/lib/subscription.rb +65 -0
- data/ringcentral-sdk.gemspec +2 -1
- data/test/test_subscription.rb +18 -0
- metadata +24 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f05987bc7911b539c714d03db8541a64f9952875
|
4
|
+
data.tar.gz: af9b7d5e38f3d7c639f5d872b01e5a8367183c37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c64088595d4936f4714dac813a8eba9fb5614a44d2f9e19a7533e6ff4b5f0f88998fcac2aa2f21420b62d76eb2efb218f4138008743d87e442b1ba466ed390f0
|
7
|
+
data.tar.gz: ef5e6248621cc31bdc8607d1d3cfcdcddd7bfa49bcc09c0b91f2e144275e4c508aed061cd71813e7d68883b78dee5ee2c0d468a5cdb6ece18e5d4231add7a31c
|
data/README.md
CHANGED
@@ -13,7 +13,20 @@ https://developer.ringcentral.com/api-docs/latest/index.html
|
|
13
13
|
|
14
14
|
## Usage
|
15
15
|
|
16
|
-
|
16
|
+
```ruby
|
17
|
+
require 'ringcentral'
|
18
|
+
|
19
|
+
rc = RingCentral.new(ENV['appKey'], ENV['appSecret'], ENV['server'])
|
20
|
+
rc.authorize(username: ENV['username'], extension: ENV['extension'], password: ENV['password'])
|
21
|
+
|
22
|
+
# get
|
23
|
+
r = rc.get('/restapi/v1.0/account/~/extension/~')
|
24
|
+
assert_not_equal nil, r
|
25
|
+
assert_equal '101', JSON.parse(r.body)['extensionNumber']
|
26
|
+
```
|
27
|
+
|
28
|
+
|
29
|
+
For more sample code, please refer to the [test cases](/test).
|
17
30
|
|
18
31
|
|
19
32
|
## How to test
|
data/lib/ringcentral.rb
CHANGED
@@ -3,6 +3,7 @@ require 'addressable/uri'
|
|
3
3
|
require 'subscription'
|
4
4
|
require 'rest-client'
|
5
5
|
require 'json'
|
6
|
+
require 'concurrent'
|
6
7
|
|
7
8
|
class RingCentral
|
8
9
|
def self.SANDBOX_SERVER
|
@@ -13,7 +14,7 @@ class RingCentral
|
|
13
14
|
'https://platform.ringcentral.com'
|
14
15
|
end
|
15
16
|
|
16
|
-
attr_reader :app_key, :app_secret, :server
|
17
|
+
attr_reader :app_key, :app_secret, :server, :token
|
17
18
|
attr_accessor :auto_refresh
|
18
19
|
|
19
20
|
def initialize(app_key, app_secret, server)
|
@@ -22,10 +23,7 @@ class RingCentral
|
|
22
23
|
@server = server
|
23
24
|
@auto_refresh = true
|
24
25
|
@token = nil
|
25
|
-
|
26
|
-
|
27
|
-
def token
|
28
|
-
@token
|
26
|
+
@timer = nil
|
29
27
|
end
|
30
28
|
|
31
29
|
def token=(value)
|
@@ -35,9 +33,7 @@ class RingCentral
|
|
35
33
|
@timer = nil
|
36
34
|
end
|
37
35
|
if @auto_refresh && value != nil
|
38
|
-
@timer = Concurrent::TimerTask.new(execution_interval: value
|
39
|
-
refresh
|
40
|
-
end
|
36
|
+
@timer = Concurrent::TimerTask.new(execution_interval: value['expires_in'] - 120, timeout_interval: 60) { self.refresh }
|
41
37
|
@timer.execute
|
42
38
|
end
|
43
39
|
end
|
@@ -57,9 +53,9 @@ class RingCentral
|
|
57
53
|
password: password,
|
58
54
|
}
|
59
55
|
end
|
60
|
-
|
61
|
-
r = post('/restapi/oauth/token', payload: payload)
|
62
|
-
|
56
|
+
self.token = nil
|
57
|
+
r = self.post('/restapi/oauth/token', payload: payload)
|
58
|
+
self.token = JSON.parse(r.body)
|
63
59
|
end
|
64
60
|
|
65
61
|
def refresh
|
@@ -68,18 +64,16 @@ class RingCentral
|
|
68
64
|
grant_type: 'refresh_token',
|
69
65
|
refresh_token: @token['refresh_token']
|
70
66
|
}
|
71
|
-
|
72
|
-
r = post('/restapi/oauth/token', payload: payload)
|
73
|
-
|
67
|
+
self.token = nil
|
68
|
+
r = self.post('/restapi/oauth/token', payload: payload)
|
69
|
+
self.token = JSON.parse(r.body)
|
74
70
|
end
|
75
71
|
|
76
72
|
def revoke
|
77
73
|
return if @token == nil
|
78
|
-
payload = {
|
79
|
-
|
80
|
-
|
81
|
-
@token = nil
|
82
|
-
post('/restapi/oauth/revoke', payload: payload)
|
74
|
+
payload = { token: @token['access_token'] }
|
75
|
+
self.token = nil
|
76
|
+
self.post('/restapi/oauth/revoke', payload: payload)
|
83
77
|
end
|
84
78
|
|
85
79
|
def authorize_uri(redirect_uri, state = '')
|
@@ -109,32 +103,31 @@ class RingCentral
|
|
109
103
|
request(:delete, endpoint, params: params)
|
110
104
|
end
|
111
105
|
|
112
|
-
def subscription(
|
113
|
-
Subscription.new(
|
106
|
+
def subscription(events, callback)
|
107
|
+
Subscription.new(self, events, callback)
|
114
108
|
end
|
115
109
|
|
116
110
|
private
|
117
111
|
|
118
|
-
|
119
|
-
|
120
|
-
|
112
|
+
def basic_key
|
113
|
+
Base64.encode64("#{@app_key}:#{@app_secret}").gsub(/\s/, '')
|
114
|
+
end
|
121
115
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
end
|
116
|
+
def autorization_header
|
117
|
+
@token != nil ? "Bearer #{@token['access_token']}" : "Basic #{basic_key}"
|
118
|
+
end
|
126
119
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
120
|
+
def request(method, endpoint, params: nil, payload: nil, files: nil)
|
121
|
+
url = (Addressable::URI.parse(@server) + endpoint).to_s
|
122
|
+
user_agent_header = "ringcentral/ringcentral-ruby Ruby #{RUBY_VERSION} #{RUBY_PLATFORM}"
|
123
|
+
headers = {
|
124
|
+
'Authorization': autorization_header,
|
125
|
+
'RC-User-Agent': user_agent_header
|
126
|
+
}
|
127
|
+
if payload != nil && @token != nil
|
128
|
+
headers['Content-Type'] = 'application/json'
|
129
|
+
payload = payload.to_json
|
130
|
+
end
|
131
|
+
RestClient::Request.execute(method: method.to_sym, url: url, params: params, payload: payload, headers: headers, files: files)
|
137
132
|
end
|
138
|
-
RestClient::Request.execute(method: method.to_sym, url: url, params: params, payload: payload, headers: headers, files: files)
|
139
|
-
end
|
140
133
|
end
|
data/lib/subscription.rb
CHANGED
@@ -1,2 +1,67 @@
|
|
1
|
+
require 'pubnub'
|
2
|
+
require 'concurrent'
|
3
|
+
|
1
4
|
class Subscription
|
5
|
+
attr_accessor :events
|
6
|
+
|
7
|
+
def initialize(ringcentral, events, message_callback, status_callback = nil, presence_callback = nil)
|
8
|
+
@rc = ringcentral
|
9
|
+
@events = events
|
10
|
+
@callback = Pubnub::SubscribeCallback.new(
|
11
|
+
message: ->(envelope) {
|
12
|
+
# todo: decrypt the message
|
13
|
+
message_callback(envelope)
|
14
|
+
},
|
15
|
+
presence: ->(envelope) { presence_callback != nil && presence_callback(presence) },
|
16
|
+
status: ->(envelope) { status_callback != nil && status_callback(envelope) }
|
17
|
+
)
|
18
|
+
@subscription = nil
|
19
|
+
@timer = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def subscription=(value)
|
23
|
+
@subscription = value
|
24
|
+
if @timer != nil
|
25
|
+
@timer.shutdown
|
26
|
+
@timer = nil
|
27
|
+
end
|
28
|
+
if value != nil
|
29
|
+
@timer = Concurrent::TimerTask.new(execution_interval: value['expiresIn'] - 120, timeout_interval: 60) do
|
30
|
+
self.refresh
|
31
|
+
end
|
32
|
+
@timer.execute
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def subscribe
|
37
|
+
r = @rc.post('/restapi/v1.0/subscription', payload: request_body)
|
38
|
+
self.subscription = JSON.parse(r.body)
|
39
|
+
@pubnub = Pubnub.new(subscribe_key: @subscription['deliveryMode']['subscriberKey'])
|
40
|
+
@pubnub.add_listener(callback: @callback)
|
41
|
+
@pubnub.subscribe(channel: @subscription['deliveryMode']['address'])
|
42
|
+
end
|
43
|
+
|
44
|
+
def refresh
|
45
|
+
return if @subscription == nil
|
46
|
+
r = @rc.put("/restapi/v1.0/subscription/#{@subscription['id']}", payload: request_body)
|
47
|
+
self.subscription = JSON.parse(r.body)
|
48
|
+
end
|
49
|
+
|
50
|
+
def revoke
|
51
|
+
return if @subscription == nil
|
52
|
+
@pubnub.unsubscribe(channel: @subscription['deliveryMode']['address'])
|
53
|
+
@pubnub.remove_listener(@callback)
|
54
|
+
@pubnub = nil
|
55
|
+
rc.delete("/restapi/v1.0/subscription/#{@subscription['id']}")
|
56
|
+
self.subscription = nil
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def request_body
|
62
|
+
{
|
63
|
+
'deliveryMode': { 'transportType': 'PubNub', 'encryption': true },
|
64
|
+
'eventFilters': @events
|
65
|
+
}
|
66
|
+
end
|
2
67
|
end
|
data/ringcentral-sdk.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = 'ringcentral-sdk'
|
3
|
-
gem.version = '0.2.
|
3
|
+
gem.version = '0.2.1'
|
4
4
|
gem.authors = ['Tyler Liu']
|
5
5
|
gem.email = ['tyler.liu@ringcentral.com']
|
6
6
|
gem.description = 'Ruby SDK for you to access RingCentral platform API.'
|
@@ -16,4 +16,5 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.add_dependency('rest-client', '~> 2.0', '>= 2.0.2')
|
17
17
|
gem.add_dependency('addressable', '~> 2.5', '>= 2.5.2')
|
18
18
|
gem.add_dependency('concurrent-ruby', '~> 1.0', '>= 1.0.2')
|
19
|
+
gem.add_dependency('pubnub', '~> 4.0', '>= 4.0.27')
|
19
20
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'ringcentral'
|
3
|
+
require 'dotenv'
|
4
|
+
|
5
|
+
class SubscriptionTest < Test::Unit::TestCase
|
6
|
+
def test_create_subscription
|
7
|
+
# Dotenv.load
|
8
|
+
# rc = RingCentral.new(ENV['appKey'], ENV['appSecret'], ENV['server'])
|
9
|
+
# rc.authorize(username: ENV['username'], extension: ENV['extension'], password: ENV['password'])
|
10
|
+
# events = [
|
11
|
+
# '/restapi/v1.0/account/~/extension/~/message-store',
|
12
|
+
# ]
|
13
|
+
# subscription = rc.subscription(events, ->(message) {
|
14
|
+
# puts message
|
15
|
+
# })
|
16
|
+
# subscription.subscribe()
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ringcentral-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Liu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -70,6 +70,26 @@ dependencies:
|
|
70
70
|
- - ">="
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
version: 1.0.2
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: pubnub
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '4.0'
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.0.27
|
83
|
+
type: :runtime
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.0'
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 4.0.27
|
73
93
|
description: Ruby SDK for you to access RingCentral platform API.
|
74
94
|
email:
|
75
95
|
- tyler.liu@ringcentral.com
|
@@ -83,6 +103,7 @@ files:
|
|
83
103
|
- lib/subscription.rb
|
84
104
|
- ringcentral-sdk.gemspec
|
85
105
|
- test/test_ringcentral.rb
|
106
|
+
- test/test_subscription.rb
|
86
107
|
homepage: https://github.com/tylerlong/ringcentral-ruby
|
87
108
|
licenses:
|
88
109
|
- MIT
|
@@ -109,3 +130,4 @@ specification_version: 4
|
|
109
130
|
summary: RingCentral Ruby SDK.
|
110
131
|
test_files:
|
111
132
|
- test/test_ringcentral.rb
|
133
|
+
- test/test_subscription.rb
|