lita-sonos-commander 0.1.0 → 1.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55efde944b8f8f55076c00839759350d66dd869d
4
- data.tar.gz: c983877fd0ba9b854dcee313c250b0f5b306b14e
3
+ metadata.gz: 71a47b802acd8b74a12165728bd3b0fded035ab5
4
+ data.tar.gz: 49ecca4439098ff7827f4d2b6ac78ddde816e4e3
5
5
  SHA512:
6
- metadata.gz: a51ab068605b560102c40fc6d806eefccdc3407682f82760672d977f3d08bb04e9182b3b888e7af49abda24bb0a88dacc9b431adf4a897cdd980e7ab46b1e5d0
7
- data.tar.gz: cf4e1b01326609908b1301d099c036c6bac58709421ecf098d73d86b07f0defb7334b7803693c55b2767c3e61c36218fe1626c31609674d7bd91a0d65aa8cfc4
6
+ metadata.gz: dae726c2765371f5459269bc7c95a6468f224d32cf0f64de0656c932241e3bc24c36028959114135512adcfa876a26ed2d390fb85acd500fe9bb22f8f9f106fd
7
+ data.tar.gz: 4d22468f6a65ea06c68d426628d1908d68c53606c2cbc301ad0b26e3f6e776593a55099ce71eeea8d04e518a09f0e5dd016d8d1784720d4aa0e6353f6e5a926d
@@ -2,7 +2,6 @@ require 'faye/websocket'
2
2
  require 'eventmachine'
3
3
 
4
4
  EM.run {
5
- # ws = Faye::WebSocket::Client.new('ws://localhost:9292')
6
5
  ws = Faye::WebSocket::Client.new('ws://localhost:8080/sonos/listen')
7
6
 
8
7
  ws.on :open do |event|
@@ -5,6 +5,7 @@ Lita.load_locales Dir[File.expand_path(
5
5
  )]
6
6
 
7
7
  require "lita/handlers/sonos_commander"
8
+ require "lita/commander_middleware"
8
9
 
9
10
  Lita::Handlers::SonosCommander.template_root File.expand_path(
10
11
  File.join("..", "..", "templates"),
@@ -0,0 +1,47 @@
1
+ Lita::CommanderMiddleware = lambda do |env|
2
+
3
+ if Faye::WebSocket.websocket?(env)
4
+ ws = Faye::WebSocket.new(env)
5
+
6
+ Lita::Handlers::SonosCommander.add_socket(ws)
7
+
8
+ ws.on :open do |event|
9
+ end
10
+
11
+ ws.on :connect do |event|
12
+ end
13
+
14
+ ws.on :message do |event|
15
+ ws.send({ message: event.data }.to_json)
16
+
17
+ sleep 0.5
18
+ ws.send({ message: 'WE DID IT TWITCH', command: 'echo' }.to_json)
19
+ end
20
+
21
+ ws.on :close do |event|
22
+ Lita::Handlers::SonosCommander.drop_socket(ws)
23
+
24
+ p [:close, event.code, event.reason]
25
+ ws = nil
26
+ end
27
+
28
+ # Return async Rack response
29
+ ws.rack_response
30
+
31
+ else
32
+ # Normal HTTP request
33
+ [200, {'Content-Type' => 'text/plain'}, ['Hello']]
34
+ end
35
+ end
36
+
37
+ def register_faye(arg)
38
+ @@_sockets ||= []
39
+ middleware = robot.registry.config.http.middleware
40
+ result = middleware.use App
41
+ end
42
+
43
+ def sonos_connector(request, response)
44
+ middlewares.each do |mw|
45
+ mw.middleware.call(request.env)
46
+ end
47
+ end
@@ -1,68 +1,71 @@
1
- require 'pry'
1
+ require 'json'
2
+ require 'faye/websocket'
3
+ require 'uri'
4
+
2
5
  module Lita
3
6
  module Handlers
4
7
  class SonosCommander < Handler
5
- # insert handler code here
6
-
7
- require 'faye/websocket'
8
-
9
8
 
10
9
  http.get '/sonos/listen', :sonos_connector
11
10
 
12
- route /^sonos (.+)/, :send_to_sonos
13
- route /^explore/, :call_explorer
11
+ route /^play_url (.+)/, :sonos_play_url
12
+ route /^say_text (.+)/, :sonos_say_text
14
13
 
15
14
  on :loaded, :register_faye
16
15
 
17
- def middlewares
18
- robot.registry.config.http.middleware
16
+ def sonos_play_url(message)
17
+ text = message.matches.last.last
18
+ emit_message command: 'play_url', data: URI.escape(text)
19
19
  end
20
20
 
21
- App = lambda do |env|
22
- if Faye::WebSocket.websocket?(env)
23
- ws = Faye::WebSocket.new(env)
24
-
25
- ws.on :message do |event|
26
- ws.send(event.data)
21
+ def sonos_say_text(message)
22
+ text = message.matches.last.last
23
+ emit_message command: 'play_text', data: text
24
+ end
27
25
 
28
- sleep 2
29
- ws.send "WE DID IT TWITCH"
30
- end
26
+ def emit_message(command:, data:)
27
+ puts "emitting #{command} \t #{data}"
28
+ sockets.each do |ws|
29
+ ws.send(
30
+ { command: command, data: { text: data, volume: 20 } }.to_json
31
+ )
32
+ end
33
+ end
31
34
 
32
- ws.on :close do |event|
33
- p [:close, event.code, event.reason]
34
- ws = nil
35
- end
35
+ def middlewares
36
+ robot.registry.config.http.middleware
37
+ end
36
38
 
37
- # Return async Rack response
38
- ws.rack_response
39
+ def sockets
40
+ self.class.sockets
41
+ end
39
42
 
40
- else
41
- # Normal HTTP request
42
- [200, {'Content-Type' => 'text/plain'}, ['Hello']]
43
- end
43
+ def self.sockets
44
+ @@_sockets ||= []
44
45
  end
45
46
 
46
- def register_faye(arg)
47
- middleware = robot.registry.config.http.middleware
48
- result = middleware.use App
49
- # binding.pry
47
+ def self.add_socket(socket)
48
+ puts "Tracking socket #{socket}"
49
+ @@_sockets ||= []
50
+ @@_sockets << socket
50
51
  end
51
52
 
52
- def send_to_sonos(message)
53
- #binding.pry
53
+ def self.drop_socket(socket)
54
+ puts "Forgetting socket #{socket}"
55
+ sockets.delete_if { |s| s == socket }
54
56
  end
55
57
 
56
- def call_explorer(_message); explorer; end
58
+ def self.serialize(message)
59
+ end
57
60
 
58
- def explorer
61
+ def register_faye(arg)
62
+ @@_sockets ||= []
59
63
  middleware = robot.registry.config.http.middleware
64
+ result = middleware.use Lita::CommanderMiddleware
60
65
  end
61
66
 
62
67
  def sonos_connector(request, response)
63
- #binding.pry
64
68
  middlewares.each do |mw|
65
- # binding.pry
66
69
  mw.middleware.call(request.env)
67
70
  end
68
71
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'lita-sonos-commander'
3
- spec.version = '0.1.0'
3
+ spec.version = '1.0.0'
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'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-sonos-commander
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Pritchett
@@ -152,6 +152,7 @@ files:
152
152
  - Rakefile
153
153
  - example/client.rb
154
154
  - lib/lita-sonos-commander.rb
155
+ - lib/lita/commander_middleware.rb
155
156
  - lib/lita/handlers/sonos_commander.rb
156
157
  - lita-sonos-commander.gemspec
157
158
  - locales/en.yml