From 2aac331fae41e1209f41d344a4712e1a7c16cc17 Mon Sep 17 00:00:00 2001 From: Andrew Kreps Date: Wed, 13 Apr 2016 15:31:05 -0700 Subject: [PATCH] Worrrrrrrks --- .../handlers/onewheel_automatic_pancake.rb | 24 +++++++++++++++++++ lita_config.rb.sample | 1 + 2 files changed, 25 insertions(+) diff --git a/lib/lita/handlers/onewheel_automatic_pancake.rb b/lib/lita/handlers/onewheel_automatic_pancake.rb index 9323d9d..973b1eb 100644 --- a/lib/lita/handlers/onewheel_automatic_pancake.rb +++ b/lib/lita/handlers/onewheel_automatic_pancake.rb @@ -3,11 +3,35 @@ module Lita module Handlers class OnewheelAutomaticPancake < Handler + config :pancake_server + route /^play (.*)$/i, :play, command: true, help: {'play' => 'Play something with this text.'} + def play(response) + search_term = response.matches[0][0] + Lita.logger.debug "Search term found #{search_term}" + list_of_matches = get_matches(search_term) + chosen_one = list_of_matches.sample + Lita.logger.debug "#{list_of_matches.count} matches, chose #{chosen_one.to_s}" + play_file(chosen_one) + end + + def get_matches(search_term) + Lita.logger.debug "searching #{config.pancake_server}/search for {query: #{search_term}}" + response = RestClient.post "#{config.pancake_server}/search", {query: search_term} + # Lita.logger.debug "Response: #{response}" + JSON.parse response + end + + def play_file(chosen_one) + uri = "#{config.pancake_server}/play/#{chosen_one['id']}" + Lita.logger.debug "POST to #{config.pancake_server}/play/#{chosen_one['id']}" + RestClient.post uri, {} + end + Lita.register_handler(self) end end diff --git a/lita_config.rb.sample b/lita_config.rb.sample index 550bad8..5a99f32 100644 --- a/lita_config.rb.sample +++ b/lita_config.rb.sample @@ -1,3 +1,4 @@ Lita.configure do |config| + config.robot.log_level = :debug config.handlers.onewheel_automatic_pancake.pancake_server = 'http://10.44.1.222:3456' end