beeline-rb 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/beeline-rb.gemspec +1 -1
- data/lib/beeline/bot.rb +28 -4
- data/lib/beeline/session.rb +64 -3
- data/lib/beeline/version.rb +1 -1
- metadata +4 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c8246a0606de7f3d8c8e1096d6460493652304ebc5b751d43b120b8e319e3d6
|
4
|
+
data.tar.gz: 600690db2274d7a6013e5c7b59fe31d184f7b407a90838d9bba2dbc90cb7fb5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5dc559c796937ae9247a467f8dc5974231481b67a48024f827cfcfd8e46438f06ed4dccef3caee2fc1edc26ad780fc04277005c0ed3b153d3a781d2b8cd030b2
|
7
|
+
data.tar.gz: 72590503e8c7d7b45c7fd654141d3e9360c2dcbaa00c833dbb20fbbe15da5ca49e2dfb3017396ffbebd49f3dac3386ac3401fd457a99e3cde9f4427fc858eab4
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
[beeline](https://github.com/inertia186/beeline-rb)
|
2
2
|
=======
|
3
3
|
|
4
|
-
BeeLine is a [BeeChat](https://beechat.hive-engine.com/) Client Framework for Ruby.
|
4
|
+
BeeLine is a [BeeChat](https://beechat.hive-engine.com/) Client Framework for Ruby. BeeChat has been designed by [@reazuliqbal](https://hive.blog/@reazuliqbal). BeeLine provides abstraction to for ruby developers to interact through the BeeChat [API](https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md).
|
5
5
|
|
6
6
|
#### Installation
|
7
7
|
|
data/beeline-rb.gemspec
CHANGED
@@ -33,6 +33,6 @@ Gem::Specification.new do |spec|
|
|
33
33
|
spec.add_development_dependency 'rb-readline', '~> 0.5', '= 0.5.5'
|
34
34
|
|
35
35
|
spec.add_dependency 'hive-ruby', '~> 1.0', '= 1.0.0'
|
36
|
-
spec.add_dependency 'faye-websocket', '~> 0.
|
36
|
+
spec.add_dependency 'faye-websocket', '~> 0.10'
|
37
37
|
spec.add_dependency 'permessage_deflate', '~> 0.1', '= 0.1.4'
|
38
38
|
end
|
data/lib/beeline/bot.rb
CHANGED
@@ -20,22 +20,24 @@ module Beeline
|
|
20
20
|
|
21
21
|
def process_chat_message(payload)
|
22
22
|
from = payload['from']
|
23
|
+
|
24
|
+
cooldown(from)
|
25
|
+
|
26
|
+
conversation_id = payload['conversation_id']
|
23
27
|
content = payload['content'].to_s
|
24
28
|
command_key = content.split(' ').first.split(prefix).last.to_sym
|
25
29
|
reply = if commands.keys.include? command_key
|
26
30
|
args = (content.split(' ') - ["#{prefix}#{command_key}"]).join(' ')
|
27
31
|
args = args.empty? ? nil : args
|
28
32
|
|
29
|
-
commands[command_key][:block].call(args, from)
|
33
|
+
commands[command_key][:block].call(args, from, conversation_id)
|
30
34
|
elsif (matching_messages = messages.select{|k| Regexp.new(k).match?(content)}).any?
|
31
35
|
message = matching_messages.values.first # match in order of declaration
|
32
36
|
|
33
|
-
message[:block].call(content, from)
|
37
|
+
message[:block].call(content, from, conversation_id)
|
34
38
|
end
|
35
39
|
|
36
40
|
if !!reply
|
37
|
-
conversation_id = payload['conversation_id']
|
38
|
-
|
39
41
|
chat_message(conversation_id, from, reply)
|
40
42
|
end
|
41
43
|
end
|
@@ -45,5 +47,27 @@ module Beeline
|
|
45
47
|
|
46
48
|
accept_pending_friend_requests
|
47
49
|
end
|
50
|
+
private
|
51
|
+
MIN_COOLDOWN = 0.25
|
52
|
+
BASE_COOLDOWN = 0.1
|
53
|
+
MAX_COOLDOWN = 90.0
|
54
|
+
|
55
|
+
# Exponential backoff for key. This will apply a rate-limit for each key
|
56
|
+
# that depends on how often the key is used.
|
57
|
+
#
|
58
|
+
# @private
|
59
|
+
def cooldown(key)
|
60
|
+
@cooldown ||= {}
|
61
|
+
@cooldown[key] ||= Time.now
|
62
|
+
elapsed = Time.now - @cooldown[key]
|
63
|
+
|
64
|
+
if elapsed > MAX_COOLDOWN
|
65
|
+
@cooldown[key] = nil
|
66
|
+
else
|
67
|
+
interval = [BASE_COOLDOWN * elapsed, MIN_COOLDOWN].max
|
68
|
+
|
69
|
+
sleep interval
|
70
|
+
end
|
71
|
+
end
|
48
72
|
end
|
49
73
|
end
|
data/lib/beeline/session.rb
CHANGED
@@ -5,6 +5,9 @@ require 'net/http'
|
|
5
5
|
require 'digest/sha2'
|
6
6
|
|
7
7
|
module Beeline
|
8
|
+
|
9
|
+
# Manages the http and websockets session for the bot. Also interacts with
|
10
|
+
# the http API.
|
8
11
|
class Session
|
9
12
|
include Config
|
10
13
|
include Hive::Utils
|
@@ -40,6 +43,9 @@ module Beeline
|
|
40
43
|
end
|
41
44
|
end
|
42
45
|
|
46
|
+
# Logs in the bot.
|
47
|
+
#
|
48
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-userslogin GET /users/login}
|
43
49
|
def login
|
44
50
|
return @beeline_session if !!@beeline_session
|
45
51
|
|
@@ -61,6 +67,9 @@ module Beeline
|
|
61
67
|
end
|
62
68
|
end
|
63
69
|
|
70
|
+
# Verifies if the current access token is valid.
|
71
|
+
#
|
72
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-usersverify GET /users/verify}
|
64
73
|
def verify
|
65
74
|
raise "Unable to verify before logging in" unless !!@beeline_session
|
66
75
|
|
@@ -76,6 +85,9 @@ module Beeline
|
|
76
85
|
end
|
77
86
|
end
|
78
87
|
|
88
|
+
# Requests a new access token.
|
89
|
+
#
|
90
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-usersrefresh-token GET /users/refresh-token}
|
79
91
|
def refresh_token
|
80
92
|
raise "Unable to refresh token before logging in" unless !!@beeline_session
|
81
93
|
|
@@ -95,6 +107,9 @@ module Beeline
|
|
95
107
|
end
|
96
108
|
end
|
97
109
|
|
110
|
+
# Generalized get method.
|
111
|
+
#
|
112
|
+
# @param resource [String] Resource to get, including query parameters.
|
98
113
|
def get(resource)
|
99
114
|
raise "Unable to request #{resource} before logging in" unless !!@beeline_session
|
100
115
|
|
@@ -110,10 +125,28 @@ module Beeline
|
|
110
125
|
end
|
111
126
|
end
|
112
127
|
|
128
|
+
# Returns bot's friends and blocked list.
|
129
|
+
#
|
130
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-usersfriends GET /users/friends}
|
113
131
|
def friends; get('/users/friends'); end
|
132
|
+
|
133
|
+
# Returns an array of bot's pending friend requests.
|
134
|
+
#
|
135
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-usersfriend-requests GET /users/friend-requests}
|
114
136
|
def friend_requests; get('/users/friend-requests'); end
|
137
|
+
|
138
|
+
# Returns bot's settings.
|
139
|
+
#
|
140
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-userssettings GET /users/settings}
|
115
141
|
def settings; get('/users/settings'); end
|
116
142
|
|
143
|
+
# Updates the bot's settings.
|
144
|
+
#
|
145
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#post-userssettings POST /users/settings}
|
146
|
+
#
|
147
|
+
# @param new_settings [Hash]
|
148
|
+
# @option new_settings [Hash] :dm
|
149
|
+
# * :only_from_friends (Boolean) Only allow direct messages from friends.
|
117
150
|
def settings=(new_settings)
|
118
151
|
raise "Unable to post settings before logging in" unless !!@beeline_session
|
119
152
|
|
@@ -131,16 +164,24 @@ module Beeline
|
|
131
164
|
end
|
132
165
|
end
|
133
166
|
|
167
|
+
# Returns an array of bot-created channels.
|
168
|
+
#
|
169
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-userschannels GET /users/channels}
|
134
170
|
def channels; get('/users/channels'); end
|
135
171
|
|
136
|
-
|
172
|
+
# Creates a new channel.
|
173
|
+
#
|
174
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#post-userschannels POST /users/channels}
|
175
|
+
#
|
176
|
+
# @param name [String]
|
177
|
+
def channels=(name)
|
137
178
|
raise "Unable to post channels before logging in" unless !!@beeline_session
|
138
179
|
|
139
180
|
resource = "#{base_uri.path}/users/channels"
|
140
181
|
|
141
182
|
http do |http|
|
142
183
|
request = Net::HTTP::Post.new resource
|
143
|
-
request.body =
|
184
|
+
request.body = name
|
144
185
|
request['Content-Type'] = 'application/json; charset=UTF-8'
|
145
186
|
request['User-Agent'] = AGENT_ID
|
146
187
|
request['Authorization'] = "Bearer #{token}"
|
@@ -149,10 +190,22 @@ module Beeline
|
|
149
190
|
JSON[response.body]
|
150
191
|
end
|
151
192
|
end
|
152
|
-
|
193
|
+
|
194
|
+
# Logs out the bot.
|
195
|
+
#
|
196
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-userslogout GET /users/logout}
|
153
197
|
def logout; get('/users/logout'); end
|
198
|
+
|
199
|
+
# Returns details of a conversation.
|
200
|
+
#
|
201
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-messagesconversation GET /messages/conversation}
|
154
202
|
def conversations; get('/messages/conversations'); end
|
155
203
|
|
204
|
+
# Returns details of a conversation.
|
205
|
+
#
|
206
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-messagesconversation GET /messages/conversation}
|
207
|
+
#
|
208
|
+
# @param id [String] (or Array<String>) Conversation id or array of ids.
|
156
209
|
def conversation(*id)
|
157
210
|
raise "Unable to get conversation before logging in" unless !!@beeline_session
|
158
211
|
|
@@ -169,8 +222,14 @@ module Beeline
|
|
169
222
|
end
|
170
223
|
end
|
171
224
|
|
225
|
+
# Return an array of unread messages.
|
226
|
+
#
|
227
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-messagesnew GET /messages/new}
|
172
228
|
def new_conversations; get('/messages/new'); end
|
173
229
|
|
230
|
+
# Query chat messages by id, \
|
231
|
+
#
|
232
|
+
# See: {https://github.com/hive-engine/beechat-frontend/blob/master/DOCUMENTATION.md#get-messageschats GET /messages/chats}
|
174
233
|
def chats(id, before = nil, limit = nil)
|
175
234
|
raise "Unable to get conversation before logging in" unless !!@beeline_session
|
176
235
|
|
@@ -188,6 +247,8 @@ module Beeline
|
|
188
247
|
end
|
189
248
|
end
|
190
249
|
|
250
|
+
# Calls #new_conversations and dumps all messages.
|
251
|
+
#
|
191
252
|
def dump_conversations
|
192
253
|
new_conversations.map{|c| "#{c['timestamp']} :: #{c['conversation_id']} :: #{c['from']}: #{c['content']}"}
|
193
254
|
end
|
data/lib/beeline/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: beeline-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anthony Martin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -314,22 +314,16 @@ dependencies:
|
|
314
314
|
name: faye-websocket
|
315
315
|
requirement: !ruby/object:Gem::Requirement
|
316
316
|
requirements:
|
317
|
-
- - '='
|
318
|
-
- !ruby/object:Gem::Version
|
319
|
-
version: 0.11.0
|
320
317
|
- - "~>"
|
321
318
|
- !ruby/object:Gem::Version
|
322
|
-
version: '0.
|
319
|
+
version: '0.10'
|
323
320
|
type: :runtime
|
324
321
|
prerelease: false
|
325
322
|
version_requirements: !ruby/object:Gem::Requirement
|
326
323
|
requirements:
|
327
|
-
- - '='
|
328
|
-
- !ruby/object:Gem::Version
|
329
|
-
version: 0.11.0
|
330
324
|
- - "~>"
|
331
325
|
- !ruby/object:Gem::Version
|
332
|
-
version: '0.
|
326
|
+
version: '0.10'
|
333
327
|
- !ruby/object:Gem::Dependency
|
334
328
|
name: permessage_deflate
|
335
329
|
requirement: !ruby/object:Gem::Requirement
|