chatbox 0.3.0 → 0.3.1
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/lib/chatbox/draft.rb +3 -10
- data/lib/chatbox/memcached_store.rb +45 -31
- data/lib/chatbox/memory_store.rb +16 -7
- data/lib/chatbox/version.rb +1 -1
- data/spec/integration/stores_spec.rb +7 -5
- data/spec/lib/draft_spec.rb +1 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8e11dde0a77434ce3d109d0dafe7379723ea402
|
4
|
+
data.tar.gz: f7231a1d739ddd0db1ee719c0cb66b49d3c928cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a9aa4f200df75d0b2bf21b185c34bb4598f62e69a0bdfff0aab6fe9d5a1777fa8f95500e0d87c2ee3be6c309fe686a8d17ccac887974a004310d460a8af4c19
|
7
|
+
data.tar.gz: 0d42ecf210544a3f5e29d5c4b01f0b6c27f56beacac496be77c41a5a0a5de739c9b0212a8194fdb7a94b5561d4edfd576935db8a683c75f3a6def906be9c38df
|
data/lib/chatbox/draft.rb
CHANGED
@@ -1,29 +1,22 @@
|
|
1
|
-
require 'securerandom'
|
2
1
|
require 'chatbox/fake_missing_keywords'
|
3
2
|
|
4
3
|
module Chatbox
|
5
4
|
class Draft
|
6
5
|
include FakeMissingKeywords
|
7
6
|
|
8
|
-
def initialize(from: req(:from), to: req(:to), body: req(:body), store: req(:store)
|
7
|
+
def initialize(from: req(:from), to: req(:to), body: req(:body), store: req(:store))
|
9
8
|
@from = from
|
10
9
|
@to = to
|
11
10
|
@body = body
|
12
11
|
@store = store
|
13
|
-
@id_generator = id_generator
|
14
12
|
end
|
15
13
|
|
16
14
|
def deliver!
|
17
|
-
store.add_message
|
18
|
-
'id' => id_generator.(),
|
19
|
-
'from_id' => from.chatbox_id,
|
20
|
-
'to_id' => to.chatbox_id,
|
21
|
-
'body' => body,
|
22
|
-
)
|
15
|
+
store.add_message from_id: from.chatbox_id, to_id: to.chatbox_id, body: body
|
23
16
|
end
|
24
17
|
|
25
18
|
private
|
26
19
|
|
27
|
-
attr_reader :from, :to, :body, :store
|
20
|
+
attr_reader :from, :to, :body, :store
|
28
21
|
end
|
29
22
|
end
|
@@ -1,54 +1,58 @@
|
|
1
|
+
require 'securerandom'
|
1
2
|
require 'json'
|
2
3
|
|
3
4
|
module Chatbox
|
4
5
|
class MemcachedStore
|
5
|
-
def initialize(client)
|
6
|
+
def initialize(client, id_generator: -> { SecureRandom.uuid })
|
6
7
|
@client = client
|
8
|
+
@id_generator = id_generator
|
7
9
|
end
|
8
10
|
|
9
11
|
def add_message(attrs)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
12
|
+
id = id_generator.()
|
13
|
+
|
14
|
+
write "messages/#{id}", {
|
15
|
+
from_id: attrs[:from_id],
|
16
|
+
to_id: attrs[:to_id],
|
17
|
+
body: attrs[:body],
|
18
|
+
read: false,
|
19
|
+
}
|
20
|
+
|
21
|
+
from_list = read("from/#{attrs[:from_id]}") || []
|
22
|
+
from_list << {from_id: attrs[:from_id], message_id: id}
|
23
|
+
write "from/#{attrs[:from_id]}", from_list
|
24
|
+
|
25
|
+
to_list = read("to/#{attrs[:to_id]}") || []
|
26
|
+
to_list << {to_id: attrs[:to_id], message_id: id}
|
27
|
+
write "to/#{attrs[:to_id]}", to_list
|
24
28
|
end
|
25
29
|
|
26
30
|
##########
|
27
31
|
|
28
32
|
def mark_message_read!(id)
|
29
|
-
attrs =
|
30
|
-
attrs[
|
31
|
-
|
33
|
+
attrs = read "messages/#{id}"
|
34
|
+
attrs[:read] = true
|
35
|
+
write "messages/#{id}", attrs
|
32
36
|
end
|
33
37
|
|
34
38
|
def mark_message_unread!(id)
|
35
|
-
attrs =
|
36
|
-
attrs[
|
37
|
-
|
39
|
+
attrs = read "messages/#{id}"
|
40
|
+
attrs[:read] = false
|
41
|
+
write "messages/#{id}", attrs
|
38
42
|
end
|
39
43
|
|
40
44
|
##########
|
41
45
|
|
42
46
|
def find_message(id)
|
43
|
-
if
|
44
|
-
Record.new id,
|
47
|
+
if attrs = read("messages/#{id}")
|
48
|
+
Record.new id, attrs
|
45
49
|
end
|
46
50
|
end
|
47
51
|
|
48
52
|
def find_messages_by_to_id(id)
|
49
|
-
if
|
50
|
-
|
51
|
-
find_message attrs[
|
53
|
+
if attrs_list = read("to/#{id}")
|
54
|
+
attrs_list.map do |attrs|
|
55
|
+
find_message attrs[:message_id]
|
52
56
|
end
|
53
57
|
else
|
54
58
|
[]
|
@@ -56,9 +60,9 @@ module Chatbox
|
|
56
60
|
end
|
57
61
|
|
58
62
|
def find_messages_by_from_id(id)
|
59
|
-
if
|
60
|
-
|
61
|
-
find_message attrs[
|
63
|
+
if attrs_list = read("from/#{id}")
|
64
|
+
attrs_list.map do |attrs|
|
65
|
+
find_message attrs[:message_id]
|
62
66
|
end
|
63
67
|
else
|
64
68
|
[]
|
@@ -67,7 +71,17 @@ module Chatbox
|
|
67
71
|
|
68
72
|
private
|
69
73
|
|
70
|
-
attr_reader :client
|
74
|
+
attr_reader :client, :id_generator
|
75
|
+
|
76
|
+
def read(key)
|
77
|
+
if value = client.get(key)
|
78
|
+
JSON.parse value, symbolize_names: true
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def write(key, value)
|
83
|
+
client.set key, JSON.generate(value)
|
84
|
+
end
|
71
85
|
|
72
86
|
class Record
|
73
87
|
def initialize(id, attrs)
|
@@ -77,7 +91,7 @@ module Chatbox
|
|
77
91
|
|
78
92
|
attr_reader :id
|
79
93
|
|
80
|
-
%
|
94
|
+
%i[from_id to_id body read].each do |name|
|
81
95
|
define_method name do
|
82
96
|
attrs[name]
|
83
97
|
end
|
data/lib/chatbox/memory_store.rb
CHANGED
@@ -1,37 +1,46 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
1
3
|
module Chatbox
|
2
4
|
class MemoryStore
|
5
|
+
def initialize(id_generator: -> { SecureRandom.uuid })
|
6
|
+
@id_generator = id_generator
|
7
|
+
end
|
8
|
+
|
3
9
|
def add_message(attrs)
|
4
|
-
|
10
|
+
attrs = attrs.merge id: id_generator.(), read: false
|
11
|
+
attrs_list << attrs
|
5
12
|
end
|
6
13
|
|
7
14
|
##########
|
8
15
|
|
9
16
|
def mark_message_read!(id)
|
10
|
-
attrs_list.detect { |attrs| attrs[
|
17
|
+
attrs_list.detect { |attrs| attrs[:id] == id }.merge! read: true
|
11
18
|
end
|
12
19
|
|
13
20
|
def mark_message_unread!(id)
|
14
|
-
attrs_list.detect { |attrs| attrs[
|
21
|
+
attrs_list.detect { |attrs| attrs[:id] == id }.merge! read: false
|
15
22
|
end
|
16
23
|
|
17
24
|
##########
|
18
25
|
|
19
26
|
def find_message(id)
|
20
|
-
if attrs = attrs_list.detect { |attrs| attrs[
|
27
|
+
if attrs = attrs_list.detect { |attrs| attrs[:id] == id }
|
21
28
|
Record.new attrs
|
22
29
|
end
|
23
30
|
end
|
24
31
|
|
25
32
|
def find_messages_by_to_id(id)
|
26
|
-
attrs_list.select { |attrs| attrs[
|
33
|
+
attrs_list.select { |attrs| attrs[:to_id] == id }.map { |attrs| Record.new attrs }
|
27
34
|
end
|
28
35
|
|
29
36
|
def find_messages_by_from_id(id)
|
30
|
-
attrs_list.select { |attrs| attrs[
|
37
|
+
attrs_list.select { |attrs| attrs[:from_id] == id }.map { |attrs| Record.new attrs }
|
31
38
|
end
|
32
39
|
|
33
40
|
private
|
34
41
|
|
42
|
+
attr_reader :id_generator
|
43
|
+
|
35
44
|
def attrs_list
|
36
45
|
@attrs_list ||= []
|
37
46
|
end
|
@@ -41,7 +50,7 @@ module Chatbox
|
|
41
50
|
@attrs = attrs
|
42
51
|
end
|
43
52
|
|
44
|
-
%
|
53
|
+
%i[id from_id to_id body read].each do |name|
|
45
54
|
define_method name do
|
46
55
|
attrs[name]
|
47
56
|
end
|
data/lib/chatbox/version.rb
CHANGED
@@ -6,19 +6,21 @@ describe 'stores' do
|
|
6
6
|
[:memcached, :memory].each do |store_type|
|
7
7
|
context "using #{store_type} store" do
|
8
8
|
before do
|
9
|
+
id = 0
|
10
|
+
id_generator = -> { id += 1 }
|
9
11
|
@store = case store_type
|
10
12
|
when :memcached
|
11
13
|
client = Dalli::Client.new nil, namespace: 'chatbox-test'
|
12
14
|
client.flush
|
13
|
-
Chatbox::MemcachedStore.new client
|
15
|
+
Chatbox::MemcachedStore.new client, id_generator: id_generator
|
14
16
|
when :memory
|
15
|
-
Chatbox::MemoryStore.new
|
17
|
+
Chatbox::MemoryStore.new id_generator: id_generator
|
16
18
|
end
|
17
19
|
|
18
20
|
[
|
19
|
-
{
|
20
|
-
{
|
21
|
-
{
|
21
|
+
{from_id: 20, to_id: 30, body: 'Hi'},
|
22
|
+
{from_id: 20, to_id: 31, body: 'Hello'},
|
23
|
+
{from_id: 21, to_id: 31, body: 'Howdy'},
|
22
24
|
].each do |attrs|
|
23
25
|
@store.add_message attrs
|
24
26
|
end
|
data/spec/lib/draft_spec.rb
CHANGED
@@ -9,15 +9,9 @@ describe Chatbox::Draft do
|
|
9
9
|
to: double('entity', chatbox_id: 2),
|
10
10
|
body: 'Hi',
|
11
11
|
store: store,
|
12
|
-
id_generator: -> { 'b00c7e15-668b-44b8-9336-e87e7b7d892e' },
|
13
12
|
)
|
14
13
|
|
15
|
-
expect(store).to receive(:add_message).with
|
16
|
-
'id' => 'b00c7e15-668b-44b8-9336-e87e7b7d892e',
|
17
|
-
'from_id' => 1,
|
18
|
-
'to_id' => 2,
|
19
|
-
'body' => 'Hi',
|
20
|
-
)
|
14
|
+
expect(store).to receive(:add_message).with from_id: 1, to_id: 2, body: 'Hi'
|
21
15
|
draft.deliver!
|
22
16
|
end
|
23
17
|
end
|