class Lita::Handlers::SonosCommander

Public Class Methods

sockets() click to toggle source
# File lib/lita/handlers/sonos_commander.rb, line 14
def self.sockets
  @_sockets
end

Public Instance Methods

emit_message(command:, data:) click to toggle source

START:message_emission

# File lib/lita/handlers/sonos_commander.rb, line 83
def emit_message(command:, data:)
  puts "emitting #{command} \t #{data}"

  sockets.each do |ws|
    ws.send serialize(command: command, text: data)
  end
end
handle_sonos_play_url(message) click to toggle source

START:chat_handlers

# File lib/lita/handlers/sonos_commander.rb, line 44
def handle_sonos_play_url(message)
  return message.reply('No clients found!') unless sockets.any?

  text = message.matches.last.last
  play_url message.matches.last.last

  message.reply "Command sent: [play_url] #{text}!"
end
handle_sonos_say_text(message) click to toggle source
# File lib/lita/handlers/sonos_commander.rb, line 57
def handle_sonos_say_text(message)
  return message.reply('No clients found!') unless sockets.any?

  text = message.matches.last.last
  say_text text

  message.reply "Command sent: [play_text] #{text}!"
end
middleware_registry() click to toggle source
# File lib/lita/handlers/sonos_commander.rb, line 77
def middleware_registry
  robot.registry.config.http.middleware
end
play_url(url) click to toggle source
# File lib/lita/handlers/sonos_commander.rb, line 53
def play_url(url)
  emit_message command: 'play_url', data: URI.escape(url)
end
register_faye(_arg) click to toggle source

START:faye_hookup

# File lib/lita/handlers/sonos_commander.rb, line 72
def register_faye(_arg)
  socket_manager = Lita::CommanderMiddleware.build(open_sockets: sockets)
  middleware_registry.use socket_manager
end
say_text(text) click to toggle source
# File lib/lita/handlers/sonos_commander.rb, line 66
def say_text(text)
  emit_message command: 'play_text', data: text
end
serialize(command:, text:) click to toggle source
# File lib/lita/handlers/sonos_commander.rb, line 91
def serialize(command:, text:)
  {
    command: command,
    data: { text: text, volume: 20 }
  }.to_json
end
sockets() click to toggle source
# File lib/lita/handlers/sonos_commander.rb, line 18
def sockets
  self.class.sockets
end
websocket_creator(request, _response) click to toggle source

START:create_sockets

# File lib/lita/handlers/sonos_commander.rb, line 33
def websocket_creator(request, _response)
  # could probably skip straight to the Commander middleware
  # but it's cleaner to leave them all in play.

  middleware_registry.each do |mw|
    mw.middleware.call(request.env)
  end
end