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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22713ecf76dc139e0a97fc0947a1e81c8782d3ae
4
- data.tar.gz: 9d3a34d9a689633fcfa6116eda4ce9545657da73
3
+ metadata.gz: f05987bc7911b539c714d03db8541a64f9952875
4
+ data.tar.gz: af9b7d5e38f3d7c639f5d872b01e5a8367183c37
5
5
  SHA512:
6
- metadata.gz: 4518d6cc3bf373040f63960cedab921b6e13a776e6ef41b9121c3d0c2b2a04f046f821730ef07445cabfc488306bfb3c7b9252086ab32a5c27e87b291d9dc60e
7
- data.tar.gz: bf884572171894c8df910610e0f45ea762155970d342e74a5cbdb164f94ed8756e42f108443e512b47bc7a92309b8b9a01acf3de9063c0d821bfd75d072e152f
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
- WIP.
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
- end
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.expires_in - 120, timeout_interval: 60) do
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
- @token = nil
61
- r = post('/restapi/oauth/token', payload: payload)
62
- @token = JSON.parse(r.body)
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
- @token = nil
72
- r = post('/restapi/oauth/token', payload: payload)
73
- @token = JSON.parse(r.body)
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
- token: @token['access_token']
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(event_filters, callback)
113
- Subscription.new(event_filters, callback)
106
+ def subscription(events, callback)
107
+ Subscription.new(self, events, callback)
114
108
  end
115
109
 
116
110
  private
117
111
 
118
- def basic_key
119
- Base64.encode64("#{@app_key}:#{@app_secret}").gsub(/\s/, '')
120
- end
112
+ def basic_key
113
+ Base64.encode64("#{@app_key}:#{@app_secret}").gsub(/\s/, '')
114
+ end
121
115
 
122
- def autorization_header
123
- return "Bearer #{@token['access_token']}" if @token != nil
124
- return "Basic #{basic_key}"
125
- end
116
+ def autorization_header
117
+ @token != nil ? "Bearer #{@token['access_token']}" : "Basic #{basic_key}"
118
+ end
126
119
 
127
- def request(method, endpoint, params: nil, payload: nil, files: nil)
128
- url = (Addressable::URI.parse(@server) + endpoint).to_s
129
- user_agent_header = "ringcentral/ringcentral-ruby Ruby #{RUBY_VERSION} #{RUBY_PLATFORM}"
130
- headers = {
131
- 'Authorization': autorization_header,
132
- 'RC-User-Agent': user_agent_header
133
- }
134
- if payload != nil && @token != nil
135
- headers['Content-Type'] = 'application/json'
136
- payload = payload.to_json
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'ringcentral-sdk'
3
- gem.version = '0.2.0'
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.0
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-20 00:00:00.000000000 Z
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