diff --git a/Config.ru b/Config.ru new file mode 100644 index 0000000..d193c85 --- /dev/null +++ b/Config.ru @@ -0,0 +1,16 @@ +$LOAD_PATH.unshift(File.dirname(__FILE__)) + +require 'slack-numberman' +require 'web' + +Thread.new do + begin + SlackNumberman::App.instance.run + rescue Exception => e + STDERR.puts "ERROR: #{e}" + STDERR.puts e.backtrace + raise e + end +end + +run SlackNumberman::Web diff --git a/Gemfile b/Gemfile index 7c71e52..8ec292b 100644 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,15 @@ source 'http://rubygems.org' gem 'slack-ruby-bot' gem 'wolfram' +gem 'puma' +gem 'sinatra' + +group :development, :test do + gem 'rake' + gem 'foreman' +end + +group :test do + gem 'rspec' + gem 'rack-test' +end diff --git a/Gemfile.lock b/Gemfile.lock index 6e281d5..f6e0847 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,6 +8,7 @@ GEM thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) addressable (2.3.8) + diff-lcs (1.2.5) eventmachine (1.0.7) faraday (0.9.1) multipart-post (>= 1.2, < 3) @@ -20,6 +21,8 @@ GEM faye-websocket (0.10.0) eventmachine (>= 0.12.0) websocket-driver (>= 0.5.1) + foreman (0.78.0) + thor (~> 0.19.1) giphy (2.0.2) faraday (~> 0.9) faraday_middleware (~> 0.9) @@ -36,6 +39,30 @@ GEM nokogiri (1.6.6.4) mini_portile (~> 0.6.0) oj (2.12.10) + puma (2.12.2) + rack (1.6.4) + rack-protection (1.5.3) + rack + rack-test (0.6.3) + rack (>= 1.0) + rake (10.4.2) + rspec (3.3.0) + rspec-core (~> 3.3.0) + rspec-expectations (~> 3.3.0) + rspec-mocks (~> 3.3.0) + rspec-core (3.3.2) + rspec-support (~> 3.3.0) + rspec-expectations (3.3.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-mocks (3.3.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.3.0) + rspec-support (3.3.0) + sinatra (1.4.6) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (>= 1.3, < 3) slack-ruby-bot (0.4.0) activesupport giphy (~> 2.0.2) @@ -47,7 +74,9 @@ GEM faraday faraday_middleware faye-websocket + thor (0.19.1) thread_safe (0.3.5) + tilt (2.0.1) tzinfo (1.2.2) thread_safe (~> 0.1) websocket-driver (0.5.4) @@ -60,6 +89,12 @@ PLATFORMS ruby DEPENDENCIES + foreman + puma + rack-test + rake + rspec + sinatra slack-ruby-bot wolfram diff --git a/Procfile b/Procfile index d94f69e..4fd3163 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -console: bundle exec ruby numberman.rb +web: bundle exec puma -p $PORT diff --git a/numberman.rb b/numberman.rb deleted file mode 100644 index 54cb4b6..0000000 --- a/numberman.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'slack-ruby-bot' -require 'wolfram' - -module NumberManBot - class App < SlackRubyBot::App - end - - class Weather < SlackRubyBot::Commands::Base - match(/^How is the weather in (?\w*)\?$/i) do |client, data, match| - send_message client, data.channel, "The weather in #{match[:location]} is nice." - end - end - - class Calculator < SlackRubyBot::Commands::Base - operator '=' do |client, _data, _match| - # implementation detail - Wolfram.appid = "R3KHQ2-2T2769PP4P" - - eq = _match[1..-1] - result = Wolfram.fetch(eq[1]) - # to see the result as a hash of pods and assumptions: - hash = Wolfram::HashPresenter.new(result).to_hash - puts hash - - pods = hash[:pods] - result = pods["Result"] - #solution = pods["Real solutions"] - puts result - - if result != nil - result[0].sub! "~~", "≈" - send_message client, _data.channel, result[0] - else - solution = pods["Real solution"] - puts solution - if solution != nil - solution[0].sub! "~~", "≈" - send_message client, _data.channel, solution[0] - else - solution = pods["Real solutions"] - solution[0].sub! "~~", "≈" - puts solution - send_message client, _data.channel, solution[0] - end - end - end - end -end - -NumberManBot::App.instance.run diff --git a/slack-numberman.rb b/slack-numberman.rb new file mode 100644 index 0000000..3354730 --- /dev/null +++ b/slack-numberman.rb @@ -0,0 +1,4 @@ +require 'slack-ruby-bot' +require 'wolfram' +require 'slack-numberman/commands/calculate' +require 'slack-numberman/app' diff --git a/slack-numberman/app.rb b/slack-numberman/app.rb new file mode 100644 index 0000000..f57a6de --- /dev/null +++ b/slack-numberman/app.rb @@ -0,0 +1,4 @@ +module SlackNumberman + class App < SlackRubyBot::App + end +end diff --git a/slack-numberman/commands/calculate.rb b/slack-numberman/commands/calculate.rb new file mode 100644 index 0000000..5799ecc --- /dev/null +++ b/slack-numberman/commands/calculate.rb @@ -0,0 +1,54 @@ +module SlackMathbot + module Commands + class Calculator < SlackRubyBot::Commands::Base + operator '=' do |client, _data, _match| + # Set API key (donut steal) + Wolfram.appid = "R3KHQ2-2T2769PP4P" + + # Get equation part of argument + eq = _match[1..-1] + + # Get results from Wolfram Alpha + result = Wolfram.fetch(eq[1]) + + # Get hash of results + hash = Wolfram::HashPresenter.new(result).to_hash + + # Debug output hash + puts hash + + # Breakdown hash + pods = hash[:pods] + + # Try for results + result = pods["Result"] + puts result + + # If result exists + if result != nil + # Prettify equivalency + result[0].sub! "~~", "≈" + send_message client, _data.channel, result[0] + else + # If result doesn't exist, look for the real solution + solution = pods["Real solution"] + puts solution + + # If the real solution exists + if solution != nil + # Prettify equivalency + solution[0].sub! "~~", "≈" + send_message client, _data.channel, solution[0] + else + # Check the other possible name for real solutions + solution = pods["Real solutions"] + # Prettify equivalency + solution[0].sub! "~~", "≈" + puts solution + send_message client, _data.channel, solution[0] + end + end + end + end + end +end diff --git a/web.rb b/web.rb new file mode 100644 index 0000000..70d9d1d --- /dev/null +++ b/web.rb @@ -0,0 +1,9 @@ +require 'sinatra/base' + +module SlackMathbot + class Web < Sinatra::Base + get '/' do + 'Math is good for you.' + end + end +end