Browse Source

Production ready I hope

master
Daniel Muckerman 9 years ago
parent
commit
c40d0c3262
9 changed files with 135 additions and 51 deletions
  1. +16
    -0
      Config.ru
  2. +12
    -0
      Gemfile
  3. +35
    -0
      Gemfile.lock
  4. +1
    -1
      Procfile
  5. +0
    -50
      numberman.rb
  6. +4
    -0
      slack-numberman.rb
  7. +4
    -0
      slack-numberman/app.rb
  8. +54
    -0
      slack-numberman/commands/calculate.rb
  9. +9
    -0
      web.rb

+ 16
- 0
Config.ru View File

@ -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

+ 12
- 0
Gemfile View File

@ -2,3 +2,15 @@ source 'http://rubygems.org'
gem 'slack-ruby-bot' gem 'slack-ruby-bot'
gem 'wolfram' gem 'wolfram'
gem 'puma'
gem 'sinatra'
group :development, :test do
gem 'rake'
gem 'foreman'
end
group :test do
gem 'rspec'
gem 'rack-test'
end

+ 35
- 0
Gemfile.lock View File

@ -8,6 +8,7 @@ GEM
thread_safe (~> 0.3, >= 0.3.4) thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1) tzinfo (~> 1.1)
addressable (2.3.8) addressable (2.3.8)
diff-lcs (1.2.5)
eventmachine (1.0.7) eventmachine (1.0.7)
faraday (0.9.1) faraday (0.9.1)
multipart-post (>= 1.2, < 3) multipart-post (>= 1.2, < 3)
@ -20,6 +21,8 @@ GEM
faye-websocket (0.10.0) faye-websocket (0.10.0)
eventmachine (>= 0.12.0) eventmachine (>= 0.12.0)
websocket-driver (>= 0.5.1) websocket-driver (>= 0.5.1)
foreman (0.78.0)
thor (~> 0.19.1)
giphy (2.0.2) giphy (2.0.2)
faraday (~> 0.9) faraday (~> 0.9)
faraday_middleware (~> 0.9) faraday_middleware (~> 0.9)
@ -36,6 +39,30 @@ GEM
nokogiri (1.6.6.4) nokogiri (1.6.6.4)
mini_portile (~> 0.6.0) mini_portile (~> 0.6.0)
oj (2.12.10) 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) slack-ruby-bot (0.4.0)
activesupport activesupport
giphy (~> 2.0.2) giphy (~> 2.0.2)
@ -47,7 +74,9 @@ GEM
faraday faraday
faraday_middleware faraday_middleware
faye-websocket faye-websocket
thor (0.19.1)
thread_safe (0.3.5) thread_safe (0.3.5)
tilt (2.0.1)
tzinfo (1.2.2) tzinfo (1.2.2)
thread_safe (~> 0.1) thread_safe (~> 0.1)
websocket-driver (0.5.4) websocket-driver (0.5.4)
@ -60,6 +89,12 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
foreman
puma
rack-test
rake
rspec
sinatra
slack-ruby-bot slack-ruby-bot
wolfram wolfram

+ 1
- 1
Procfile View File

@ -1 +1 @@
console: bundle exec ruby numberman.rb
web: bundle exec puma -p $PORT

+ 0
- 50
numberman.rb View File

@ -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 (?<location>\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

+ 4
- 0
slack-numberman.rb View File

@ -0,0 +1,4 @@
require 'slack-ruby-bot'
require 'wolfram'
require 'slack-numberman/commands/calculate'
require 'slack-numberman/app'

+ 4
- 0
slack-numberman/app.rb View File

@ -0,0 +1,4 @@
module SlackNumberman
class App < SlackRubyBot::App
end
end

+ 54
- 0
slack-numberman/commands/calculate.rb View File

@ -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

+ 9
- 0
web.rb View File

@ -0,0 +1,9 @@
require 'sinatra/base'
module SlackMathbot
class Web < Sinatra::Base
get '/' do
'Math is good for you.'
end
end
end

Loading…
Cancel
Save