lita-sonos-commander 1.1.0 → 1.1.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/lita/commander_middleware.rb +23 -16
- data/lita-sonos-commander.gemspec +1 -1
- data/spec/lita/commander_middleware_spec.rb +44 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2dc5e325b18f3682e6a3a1dc90e59be191506fb6
|
4
|
+
data.tar.gz: 343895e09282f80ab5c96ff48892b14ad0a4ad79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bd63de801134e7bf98984b4c7ec2dc1358382ebd0384575cc2fcf9fa43253207294557004f5c76fd8b1ce9c8900ac082b2c470fcefa79db9f5ea07b40002558
|
7
|
+
data.tar.gz: 47f4c65ffa13e9794547bf02cbf814c9cea2bd8f6c0ada21f5b05dddc240e6bfc1291b41384c8c7d49e55188b5c7486f9fb13afa4fc29f6d10e6dc45b4b83872
|
@@ -1,23 +1,21 @@
|
|
1
1
|
class Lita::CommanderMiddleware
|
2
2
|
def self.build(open_sockets:)
|
3
|
-
new
|
3
|
+
new(open_sockets: open_sockets).build
|
4
4
|
end
|
5
5
|
|
6
6
|
attr_reader :env, :open_sockets
|
7
7
|
|
8
|
-
def
|
8
|
+
def initialize(open_sockets:)
|
9
9
|
@open_sockets = open_sockets
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
+
def build
|
13
|
+
lambda do |env|
|
12
14
|
if Faye::WebSocket.websocket?(env)
|
13
15
|
@env = env
|
14
16
|
handle_env_has_socket
|
15
17
|
else
|
16
|
-
|
17
|
-
200,
|
18
|
-
{ 'Content-Type' => 'text/plain' },
|
19
|
-
['Hello from a Lita chatbot! Feed me a websocket connection!']
|
20
|
-
]
|
18
|
+
http_explainer_payload
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
@@ -29,30 +27,39 @@ class Lita::CommanderMiddleware
|
|
29
27
|
ws
|
30
28
|
end
|
31
29
|
|
32
|
-
def close_socket(ws)
|
30
|
+
def close_socket(ws, event)
|
33
31
|
open_sockets.delete_if { |s| s == ws }
|
34
32
|
Lita.logger.debug "Sonos client count: #{open_sockets.count}"
|
35
|
-
|
33
|
+
Lita.logger.debug "Socket close: #{[:close, event.code, event.reason]}"
|
36
34
|
ws = nil
|
37
35
|
end
|
38
36
|
|
39
37
|
def handle_message(ws, event)
|
40
|
-
ws.send({ message: event.data }.to_json)
|
41
|
-
maybe_send_debug_message(ws)
|
38
|
+
ws.send({ message: "ACK: #{event.data}" }.to_json)
|
42
39
|
end
|
43
40
|
|
44
|
-
def
|
45
|
-
|
46
|
-
ws.send(
|
41
|
+
def send_welcome_message(ws)
|
42
|
+
payload = { message: 'Welcome to Lita Sonos Commander!', command: 'echo' }
|
43
|
+
ws.send(payload.to_json)
|
47
44
|
end
|
48
45
|
|
49
46
|
def handle_env_has_socket
|
50
47
|
ws = build_socket(env)
|
51
48
|
|
49
|
+
send_welcome_message(ws)
|
50
|
+
|
52
51
|
ws.on(:message) { |event| handle_message(ws, event) }
|
53
|
-
ws.on(:close) {
|
52
|
+
ws.on(:close) { close_socket(ws, event) }
|
54
53
|
|
55
54
|
# Return async Rack response
|
56
55
|
ws.rack_response
|
57
56
|
end
|
57
|
+
|
58
|
+
def http_explainer_payload
|
59
|
+
[
|
60
|
+
200,
|
61
|
+
{ 'Content-Type' => 'text/plain' },
|
62
|
+
['Hello from a Lita chatbot! Feed me a websocket connection!']
|
63
|
+
]
|
64
|
+
end
|
58
65
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = 'lita-sonos-commander'
|
3
|
-
spec.version = '1.1.
|
3
|
+
spec.version = '1.1.1'
|
4
4
|
spec.authors = ['Daniel J. Pritchett']
|
5
5
|
spec.email = ['dpritchett@gmail.com']
|
6
6
|
spec.description = 'Control your Sonos with Lita chatbot commands'
|
@@ -1,12 +1,55 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
2
3
|
require 'date'
|
3
4
|
|
4
5
|
describe Lita::CommanderMiddleware do
|
5
6
|
let(:handler) { double 'handler' }
|
6
7
|
let(:result) { subject.build({}) }
|
8
|
+
let(:open_sockets) { [] }
|
9
|
+
let(:a_socket) { double 'a web socket' }
|
10
|
+
|
11
|
+
subject { Lita::CommanderMiddleware.new(open_sockets: open_sockets) }
|
7
12
|
|
8
13
|
it 'returns a lambda' do
|
9
|
-
result = subject.
|
14
|
+
result = subject.build
|
10
15
|
expect(result.is_a?(Proc)).to be_truthy
|
11
16
|
end
|
17
|
+
|
18
|
+
context 'adding a new client' do
|
19
|
+
before { Faye::WebSocket.stub(:new).and_return(a_socket) }
|
20
|
+
let(:result) { subject.build_socket(nil) }
|
21
|
+
|
22
|
+
it 'builds websockets on demand' do
|
23
|
+
expect(result).to eq(a_socket)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'adds the new socket to :open_sockets' do
|
27
|
+
expect(open_sockets).to include(result)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'client disconnects' do
|
32
|
+
let(:event) { double 'socket event' }
|
33
|
+
before { a_socket.stub(:send) }
|
34
|
+
before { event.stub(:code) }
|
35
|
+
before { event.stub(:reason) }
|
36
|
+
|
37
|
+
it 'removes the client from :open_sockets' do
|
38
|
+
result = subject.close_socket(a_socket, event)
|
39
|
+
expect(result).to be_nil
|
40
|
+
expect(open_sockets).to_not include(a_socket)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'logs to debug' do
|
44
|
+
expect(Lita.logger).to receive(:debug).exactly(2).times
|
45
|
+
subject.close_socket(a_socket, event)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'acknowledges messages from clients' do
|
50
|
+
event = OpenStruct.new(data: 'This is a message!')
|
51
|
+
a_socket.stub(:send)
|
52
|
+
expect(a_socket).to receive(:send)
|
53
|
+
subject.handle_message(a_socket, event)
|
54
|
+
end
|
12
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lita-sonos-commander
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel J. Pritchett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lita
|