rubycent 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +66 -0
- data/.rspec +3 -0
- data/.travis.yml +21 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +344 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/rubycent.rb +52 -0
- data/lib/rubycent/client.rb +375 -0
- data/lib/rubycent/error.rb +9 -0
- data/lib/rubycent/errors/network_error.rb +11 -0
- data/lib/rubycent/errors/request_error.rb +18 -0
- data/lib/rubycent/errors/response_error.rb +17 -0
- data/lib/rubycent/query.rb +65 -0
- data/lib/rubycent/request.rb +117 -0
- data/lib/rubycent/version.rb +5 -0
- data/rubycent.gemspec +45 -0
- metadata +163 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dfaf925e6e01a08b22195a2dfe36acda46a55bc4aee1217c8cd562f94c0de649
|
4
|
+
data.tar.gz: e22cc700bd3f33907b8423a760f6386470dad5800743e8d27b3ce7d713f71442
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba9ccc0a3f4452b905cdd1979b5ea6098841d18c6418aef3a97953745f27bb98f3b69a0e4df62d50f204800308306488f6bcfcce6e4c8e19cb6142610cd15ffa
|
7
|
+
data.tar.gz: 9d3aa3459813be9de20e24f176924e260638e88b0a4ef2d1d02a6c14864f2f74d2d0db2901c34863565410f1c37a814822a40a0f3452a26c4284ac4146f0b26e
|
data/.gitignore
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
# Ignore Byebug command history file.
|
17
|
+
.byebug_history
|
18
|
+
|
19
|
+
## Specific to RubyMotion:
|
20
|
+
.dat*
|
21
|
+
.repl_history
|
22
|
+
build/
|
23
|
+
*.bridgesupport
|
24
|
+
build-iPhoneOS/
|
25
|
+
build-iPhoneSimulator/
|
26
|
+
|
27
|
+
## Specific to RubyMotion (use of CocoaPods):
|
28
|
+
#
|
29
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
30
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
31
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
32
|
+
#
|
33
|
+
# vendor/Pods/
|
34
|
+
|
35
|
+
## Documentation cache and generated files:
|
36
|
+
/.yardoc/
|
37
|
+
/_yardoc/
|
38
|
+
/doc/
|
39
|
+
/rdoc/
|
40
|
+
|
41
|
+
## Environment normalization:
|
42
|
+
/.bundle/
|
43
|
+
/vendor/bundle
|
44
|
+
/lib/bundler/man/
|
45
|
+
|
46
|
+
# for a library or gem, you might want to ignore these files since the code is
|
47
|
+
# intended to run in multiple environments; otherwise, check them in:
|
48
|
+
# Gemfile.lock
|
49
|
+
# .ruby-version
|
50
|
+
# .ruby-gemset
|
51
|
+
|
52
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
53
|
+
.rvmrc
|
54
|
+
|
55
|
+
# IDE files
|
56
|
+
.idea
|
57
|
+
|
58
|
+
# rspec failure tracking
|
59
|
+
.rspec_status
|
60
|
+
|
61
|
+
# For those using rbenv
|
62
|
+
.ruby-version
|
63
|
+
|
64
|
+
# Don't checkin Gemfile.lock
|
65
|
+
# See: http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/
|
66
|
+
Gemfile.lock
|
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
---
|
2
|
+
sudo: false
|
3
|
+
language: ruby
|
4
|
+
|
5
|
+
cache: bundler
|
6
|
+
|
7
|
+
before_install: gem install bundler
|
8
|
+
|
9
|
+
rvm:
|
10
|
+
- 2.6
|
11
|
+
- 2.5
|
12
|
+
- 2.4
|
13
|
+
- 2.3
|
14
|
+
|
15
|
+
deploy:
|
16
|
+
provider: rubygems
|
17
|
+
api_key:
|
18
|
+
secure: cABVqc1cTs2BNU7Ult+vcEQjtgX/JSpFEWFJnB6QJlkmjyag+HJr522XO4r46c5/lx/SE5S5/Qnw590FMhTpsaJy6HLzHXoxrP0ydgkznPIl6GagVs4fudPD+2x5YmO0PPgC975+BqdsiJVV8HOu6CxjK2AEICUWMOUzJkwCTXBkIb+gW6aYwO7AjIxlddENCcMUz50nduFAoNv0JxV8rKc41ygDPSLF36SRXf3PrdkkBjHRoQ9tq2xJCjqIdxVSwJpVZO2eLj76BtfjGy2sS3WieoG5CeKUkg7TBEzX9fYinAibJydfS3pUUlPZkZGetUfj3Y4oC+9TqJvHrzAHrHGFu/QTXDY4kOgsOmO473p+mc/91V6epU3Gui/uI6pkiUAHTyOLZsf3V/nnd4QDPXmOD/N2rttxTlzsQ/cq+tIl/szFDq9I9yxnz83iHgRQ7yN8kjLyn+ndiklH1OOMY4X+K1/rx2Vjx8oCHWbHlMcQ1+XmxZn2S2jEourzyCZm1kxTAIoJcijBtyxS0CTw1S7GUY5X/sjIzgFDQgNgGryZl+36XCtfFZEkgPqrbdR/5M+65nNJZs2LF+aGmzfYMdDF0Qn+kKbnYDN1Y6DrzFxGl1FTDRKb2HFjY8/o8i0eC9Qgmj+uRYQP2mVLbpRhlEr0Ijz3IF9hadB5XB9titA=
|
19
|
+
on:
|
20
|
+
tags: true
|
21
|
+
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 warshavski
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,344 @@
|
|
1
|
+
# Rubycent
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.com/Warshavski/rubycent.svg?branch=master)](https://travis-ci.com/Warshavski/rubycent)
|
4
|
+
|
5
|
+
Ruby tools to communicate with [Centrifugo v2 HTTP API.](https://centrifugal.github.io/centrifugo/server/http_api/)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rubycent'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install rubycent
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
The top-level object needed for gem functionality is the Rubycent::Client object.
|
26
|
+
A client requires your Centrifugo API key to execute all requests and Centrifugo secret to issue JWT(user and private channels)
|
27
|
+
|
28
|
+
To use a client you need first configure it by setting Centrifugo API address.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
client = Rubycent::Client.new(scheme: :http, host: :localhost, port: 80, api_key: 'key', secret: 'secret')
|
32
|
+
```
|
33
|
+
|
34
|
+
or
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
Rubycent.scheme = :http
|
38
|
+
Rubycent.host = 'localhost'
|
39
|
+
Rubycent.port = 8000
|
40
|
+
Rubycent.secret = 'secret'
|
41
|
+
Rubycent.api_key = 'key'
|
42
|
+
```
|
43
|
+
|
44
|
+
This gem use [Faraday](https://github.com/lostisland/faraday) in this case you can switch request adapters.
|
45
|
+
|
46
|
+
Request configuration:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Rubycent.request_adapter = :httpclient
|
50
|
+
Rubycent.timeout = 10 # Number of seconds to wait for the connection to open.
|
51
|
+
Rubycent.open_timeout = 10 # Number of seconds to wait for one block to be read.
|
52
|
+
```
|
53
|
+
|
54
|
+
### Publish
|
55
|
+
|
56
|
+
Allows to send data into channel. Receives channel name and data to post
|
57
|
+
|
58
|
+
Example:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
# Publish `content: 'hello'` to channel `chat`
|
62
|
+
#
|
63
|
+
Rubycent.publish('chat', content: 'hello') # => {}
|
64
|
+
```
|
65
|
+
|
66
|
+
### Broadcast
|
67
|
+
|
68
|
+
Similar to publish but allows to send the same data into many channels.
|
69
|
+
|
70
|
+
Example:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# Publish { content: 'hello' } to channels 'channel_1', 'channel_2'
|
74
|
+
#
|
75
|
+
Rubycent.broadcast(%w(channel_1 channel_2), content: 'hello') # => {}
|
76
|
+
```
|
77
|
+
|
78
|
+
### Unsubscribe
|
79
|
+
|
80
|
+
Allows to unsubscribe user from channel. Receives to arguments: channel and user (user ID you want to unsubscribe)
|
81
|
+
|
82
|
+
Example:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
# Unsubscribe user with id = 1 from 'chat' channel
|
86
|
+
#
|
87
|
+
Rubycent.unsubscribe('chat', 1) # => {}
|
88
|
+
```
|
89
|
+
|
90
|
+
### Disconnect
|
91
|
+
|
92
|
+
Allows to disconnect user by it's ID. Receives user ID as an argument.
|
93
|
+
|
94
|
+
Example:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
# Disconnect user with `id = 1`
|
98
|
+
#
|
99
|
+
Rubycent.disconnect(1) # => {}
|
100
|
+
```
|
101
|
+
|
102
|
+
### Presence
|
103
|
+
|
104
|
+
Allows to get channel presence information(all clients currently subscribed on this channel). Receives channel name as an argument.
|
105
|
+
|
106
|
+
Example:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
# Get presence information for channel 'chat'
|
110
|
+
#
|
111
|
+
Rubycent.presence('chat')
|
112
|
+
|
113
|
+
# {
|
114
|
+
# 'result' => {
|
115
|
+
# 'presence' => {
|
116
|
+
# 'c54313b2-0442-499a-a70c-051f8588020f' => {
|
117
|
+
# 'client' => 'c54313b2-0442-499a-a70c-051f8588020f',
|
118
|
+
# 'user' => '42'
|
119
|
+
# },
|
120
|
+
# 'adad13b1-0442-499a-a70c-051f858802da' => {
|
121
|
+
# 'client' => 'adad13b1-0442-499a-a70c-051f858802da',
|
122
|
+
# 'user' => '42'
|
123
|
+
# }
|
124
|
+
# }
|
125
|
+
# }
|
126
|
+
# }
|
127
|
+
```
|
128
|
+
|
129
|
+
### Presence stats
|
130
|
+
|
131
|
+
Allows to get short channel presence information. Receives channel name as an argument.
|
132
|
+
|
133
|
+
Example:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
# Get short presence information for channel 'chat'
|
137
|
+
#
|
138
|
+
Rubycent.presence_stats('chat')
|
139
|
+
|
140
|
+
# {
|
141
|
+
# "result" => {
|
142
|
+
# "num_clients" => 0,
|
143
|
+
# "num_users" => 0
|
144
|
+
# }
|
145
|
+
# }
|
146
|
+
```
|
147
|
+
|
148
|
+
### History
|
149
|
+
|
150
|
+
Allows to get channel history information (list of last messages published into channel). Receives channel name as an argument.
|
151
|
+
|
152
|
+
Example:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
# Get history for channel 'chat'
|
156
|
+
#
|
157
|
+
Rubycent.history('chat')
|
158
|
+
|
159
|
+
# {
|
160
|
+
# 'result' => {
|
161
|
+
# 'publications' => [
|
162
|
+
# {
|
163
|
+
# 'data' => {
|
164
|
+
# 'text' => 'hello'
|
165
|
+
# },
|
166
|
+
# 'uid' => 'BWcn14OTBrqUhTXyjNg0fg'
|
167
|
+
# },
|
168
|
+
# {
|
169
|
+
# 'data' => {
|
170
|
+
# 'text' => 'hi!'
|
171
|
+
# },
|
172
|
+
# 'uid' => 'Ascn14OTBrq14OXyjNg0hg'
|
173
|
+
# }
|
174
|
+
# ]
|
175
|
+
# }
|
176
|
+
# }
|
177
|
+
```
|
178
|
+
|
179
|
+
### Channels
|
180
|
+
|
181
|
+
Allows to get list of active(with one or more subscribers) channels.
|
182
|
+
|
183
|
+
Example:
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
# Get active channels list
|
187
|
+
#
|
188
|
+
Rubycent.channels
|
189
|
+
|
190
|
+
# {
|
191
|
+
# 'result' => {
|
192
|
+
# 'channels' => [
|
193
|
+
# 'chat'
|
194
|
+
# ]
|
195
|
+
# }
|
196
|
+
# }
|
197
|
+
```
|
198
|
+
|
199
|
+
### Info
|
200
|
+
|
201
|
+
Allows to get information about running Centrifugo nodes.
|
202
|
+
|
203
|
+
Example:
|
204
|
+
|
205
|
+
```ruby
|
206
|
+
# Get running centrifugo nodes list
|
207
|
+
#
|
208
|
+
Rubycent.info
|
209
|
+
|
210
|
+
# {
|
211
|
+
# 'result' => {
|
212
|
+
# 'nodes' => [
|
213
|
+
# {
|
214
|
+
# 'name' => 'Alexanders-MacBook-Pro.local_8000',
|
215
|
+
# 'num_channels' => 0,
|
216
|
+
# 'num_clients' => 0,
|
217
|
+
# 'num_users' => 0,
|
218
|
+
# 'uid' => 'f844a2ed-5edf-4815-b83c-271974003db9',
|
219
|
+
# 'uptime' => 0,
|
220
|
+
# 'version' => ''
|
221
|
+
# }
|
222
|
+
# ]
|
223
|
+
# }
|
224
|
+
# }
|
225
|
+
```
|
226
|
+
|
227
|
+
### User JWT
|
228
|
+
|
229
|
+
When connecting to Centrifugo client must provide connection JWT token with several predefined credential claims.
|
230
|
+
|
231
|
+
#### Claims
|
232
|
+
|
233
|
+
##### sub
|
234
|
+
This is a standard JWT claim which must contain an ID of current application user (as string).
|
235
|
+
|
236
|
+
If your user is not currently authenticated in your application but you want to let him connect to Centrifugo anyway you can use empty string as user ID in this sub claim.
|
237
|
+
This is called anonymous access.
|
238
|
+
In this case anonymous option must be enabled in Centrifugo configuration for channels that client will subscribe to.
|
239
|
+
|
240
|
+
##### exp
|
241
|
+
This is standard JWT claim a UNIX timestamp seconds when token will expire.
|
242
|
+
|
243
|
+
If exp claim not provided then Centrifugo won’t expire any connections.
|
244
|
+
When provided special algorithm will find connections with exp in the past and activate connection refresh mechanism.
|
245
|
+
Refresh mechanism allows connection to survive and be prolonged.
|
246
|
+
In case of refresh failure client connection will be eventually closed by Centrifugo and won’t be accepted until new valid and actual credentials provided in connection token.
|
247
|
+
|
248
|
+
You can use connection expiration mechanism in cases when you don’t want users of your app be subscribed on channels after being banned/deactivated in application.
|
249
|
+
Or to protect your users from token leak (providing reasonably small time of expiration).
|
250
|
+
|
251
|
+
Choose exp value wisely, you don’t need small values because refresh mechanism will hit your application often with refresh requests.
|
252
|
+
But setting this value too large can lead to non very fast user connection deactivation.
|
253
|
+
This is a trade off.
|
254
|
+
|
255
|
+
Read more about connection expiration in special chapter.
|
256
|
+
|
257
|
+
##### info
|
258
|
+
This claim is optional - this is additional information about client connection that can be provided for Centrifugo.
|
259
|
+
This information will be included in presence information, join/leave events and in channel publication message if it was published from client side.
|
260
|
+
|
261
|
+
Example:
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
Rubycent.issue_user_token('1', 3600, info: { message: 'watever' })
|
265
|
+
|
266
|
+
#=> "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiaW5mbyI6eyJpbmZvIjp7Im1lc3NhZ2UiOiJ3YXRldmVyIn19LCJleHAiOjM2MDB9.iflaYrNxc_qQAtj52gr1q1G80jHoCgCJ0Pz3wzeKYpU"
|
267
|
+
```
|
268
|
+
|
269
|
+
### Channel JWT
|
270
|
+
|
271
|
+
All channels starting with $ considered private.
|
272
|
+
|
273
|
+
Private channel subscription token is also JWT (like connection token described in authentication chapter).
|
274
|
+
But it has different claims.
|
275
|
+
|
276
|
+
#### Claims
|
277
|
+
|
278
|
+
##### client
|
279
|
+
Required. Client ID which wants to subscribe on channel (string).
|
280
|
+
|
281
|
+
Centrifugo server have own Client ID representation in UUIDv4 format.
|
282
|
+
You have to use Client ID generated by Centrifugo server for private channel subscrition purposes.
|
283
|
+
If you using Centrifuge-JS library - Client ID and Subscription Channels will be automaticaly added to POST request.
|
284
|
+
|
285
|
+
##### channel
|
286
|
+
Required. Channel that client tries to subscribe to (string).
|
287
|
+
|
288
|
+
##### info
|
289
|
+
Optional. Additional information for connection regarding to channel (valid JSON).
|
290
|
+
|
291
|
+
##### exp
|
292
|
+
Optional. This is standard JWT claim that allows to set private channel subscription token expiration time.
|
293
|
+
|
294
|
+
At moment if subscription token expires client connection will be closed and client will try to reconnect.
|
295
|
+
In most cases you don’t need this and should prefer using exp of connection token to deactivate connection.
|
296
|
+
But if you need more granular per-channel control this may fit your needs.
|
297
|
+
|
298
|
+
Once exp set in token every subscription token must be periodically refreshed.
|
299
|
+
|
300
|
+
Example:
|
301
|
+
|
302
|
+
```ruby
|
303
|
+
Rubycent.issue_channel_token('client', 'channel', 3600, info: { message: 'watever' })
|
304
|
+
|
305
|
+
#=> "eyJhbGciOiJIUzI1NiJ9.eyJjbGllbnQiOiJjbGllbnQiLCJjaGFubmVsIjoiY2hhbm5lbCIsImluZm8iOnsiaW5mbyI6eyJtZXNzYWdlIjoid2F0ZXZlciJ9fSwiZXhwIjozNjAwfQ.jo9WFbbrUyvZt3BtHpPA1J6WMQTuJfr6jgNn9fm0SJQ"
|
306
|
+
```
|
307
|
+
|
308
|
+
### Errors
|
309
|
+
|
310
|
+
```ruby
|
311
|
+
# Wrapper for all rubycent errors(failures).
|
312
|
+
#
|
313
|
+
Rubycent::Error
|
314
|
+
|
315
|
+
# Raised when request to Centrifugo API failed due the network problems.
|
316
|
+
#
|
317
|
+
Rubycent::NetworkError
|
318
|
+
|
319
|
+
# Raised when request to Centrifugo API failed in some way.
|
320
|
+
#
|
321
|
+
Rubycent::RequestError
|
322
|
+
|
323
|
+
# Raised when response from Centrifugo contains any error as result of API command execution.
|
324
|
+
#
|
325
|
+
Rubycent::ResponseError
|
326
|
+
```
|
327
|
+
|
328
|
+
## Development
|
329
|
+
|
330
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
331
|
+
|
332
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
333
|
+
|
334
|
+
## Contributing
|
335
|
+
|
336
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/warshavski/rubycent. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
337
|
+
|
338
|
+
## License
|
339
|
+
|
340
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
341
|
+
|
342
|
+
## Code of Conduct
|
343
|
+
|
344
|
+
Everyone interacting in the Rubycent project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rubycent/blob/master/CODE_OF_CONDUCT.md).
|