Skip to content

Commit

Permalink
Added set dollars on|off, respond to , but not MSFT.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Feb 8, 2016
1 parent b95479f commit ad3797f
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Changelog

* 2/8/2016: [#3](https://github.com/dblock/slack-market/issues/3): Added `set dollars on|off`, respond to $MSFT but not MSFT - [@dblock](https://github.com/dblock).
* 2/5/2016: Respond to single character stocks with a $ sign, eg. `$F` - [@dblock](https://github.com/dblock).
* 2/5/2016: Don't respond to channel mentions that contain what looks like a ticker - [@dblock](https://github.com/dblock).
* 2/4/2016: Initial public release - [@dblock](https://github.com/dblock).
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gem 'rack-rewrite'
gem 'rack-server-pages'
gem 'yahoo-finance'
gem 'money'
gem 'wannabe_bool'

group :development, :test do
gem 'rake', '~> 10.4'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ GEM
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
wannabe_bool (0.5.0)
webmock (1.22.6)
addressable (>= 2.3.6)
crack (>= 0.3.2)
Expand Down Expand Up @@ -315,6 +316,7 @@ DEPENDENCIES
selenium-webdriver
slack-ruby-bot!
vcr
wannabe_bool
webmock
yahoo-finance

Expand Down
6 changes: 6 additions & 0 deletions public/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ <h3>Help</h3>

Invite <b>@market</b> to a channel. Mention a stock ticker and its price will appear in Slack.

<p>
<img src='img/market.gif'>
</p>

The bot will try to respond to anything UPPERCASE longer than 2 characters or anything $UPPERCASE, prefixed with a dollar sign.<br>
You can <b>set dollars on</b> to always require a dollar sign if you think the bot is too spammy.
</body>
</html>

1 change: 1 addition & 0 deletions slack-market/commands.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require 'slack-market/commands/help'
require 'slack-market/commands/quote'
require 'slack-market/commands/set'
13 changes: 7 additions & 6 deletions slack-market/commands/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ class Help < SlackRubyBot::Commands::Base
HELP = <<-EOS
```
I am your friendly market bot, providing Yahoo Finance data.
Market
------
Try _What is the price of MSFT?_ or _Tell me about YHOO and AAPL, please._
Try "What is the price of MSFT?" or "Tell me about YHOO, AAPL and $I, please."
General
-------
help - get this helpful message
help - get this helpful message
Settings
--------
set dollars on|off - respond to $QUOTE, but not $QUOTE
```
EOS
Expand Down
4 changes: 3 additions & 1 deletion slack-market/commands/quote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module SlackMarket
module Commands
class Quote < SlackRubyBot::Commands::Base
scan(/\b[A-Z]{2,}\b|\$[A-Z]{1,}\b|\b[A-Z]{1,}\$/) do |client, data, stocks|
stocks = stocks.flatten.map { |s| s.tr('$', '') }
stocks = stocks.flatten
stocks = stocks.select { |s| s[0] == '$' } if client.team.dollars?
stocks = stocks.map { |s| s.tr('$', '') }
YahooFinance::Client.new.quotes(stocks, [:name, :symbol, :last_trade_price, :change, :change_in_percent]).each do |quote|
next if quote.name == 'N/A'
logger.info "#{client.team}, user=#{data.user} - #{quote.name} (#{quote.symbol}): $#{quote.last_trade_price}"
Expand Down
22 changes: 22 additions & 0 deletions slack-market/commands/set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module SlackMarket
module Commands
class Set < SlackRubyBot::Commands::Base
def self.call(client, data, match)
if !match.names.include?('expression')
client.say(channel: data.channel, text: 'Missing setting, eg. _set dollars on_.', gif: 'help')
logger.info "SET: #{client.team} - failed, missing setting"
else
k, v = match['expression'].split(/\W+/, 2)
case k
when 'dollars' then
client.team.update_attributes!(dollars: v.to_b) unless v.nil?
client.say(channel: data.channel, text: "Dollar signs for team #{client.team.name} are #{client.team.dollars? ? 'on!' : 'off.'}", gif: 'dollars')
logger.info "SET: #{client.team} - dollar signs are #{client.team.dollars? ? 'on' : 'off'}"
else
fail "Invalid setting #{k}, you can _set dollars on|off_."
end
end
end
end
end
end
1 change: 1 addition & 0 deletions slack-market/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Team
field :token, type: String
field :active, type: Boolean, default: true
field :api, type: Boolean, default: false
field :dollars, type: Boolean, default: false

scope :active, -> { where(active: true) }
scope :api, -> { where(api: true) }
Expand Down
31 changes: 28 additions & 3 deletions spec/slack-market/commands/quote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
let(:team) { Fabricate(:team) }
let(:app) { SlackMarket::Server.new(team: team) }
let(:client) { app.send(:client) }
context 'quote', vcr: { cassette_name: 'msft' } do
it 'returns a quote for MSFT' do
context 'quote' do
it 'returns a quote for MSFT', vcr: { cassette_name: 'msft' } do
expect(client.web_client).to receive(:chat_postMessage).with(
channel: 'channel',
as_user: true,
Expand All @@ -21,7 +21,7 @@
)
app.send(:message, client, Hashie::Mash.new(channel: 'channel', text: "How's MSFT?"))
end
it 'returns a quote for $MSFT' do
it 'returns a quote for $MSFT', vcr: { cassette_name: 'msft' } do
expect(client.web_client).to receive(:chat_postMessage).with(
channel: 'channel',
as_user: true,
Expand Down Expand Up @@ -110,5 +110,30 @@
)
app.send(:message, client, Hashie::Mash.new(channel: 'channel', text: "How's F$?"))
end
context 'with dollars on' do
before do
team.update_attributes!(dollars: true)
end
it 'does not trigger with MSFT' do
expect(client.web_client).to_not receive(:chat_postMessage)
app.send(:message, client, Hashie::Mash.new(channel: 'channel', text: 'How is MSFT?'))
end
it 'returns a quote for $MSFT', vcr: { cassette_name: 'msft' } do
expect(client.web_client).to receive(:chat_postMessage).with(
channel: 'channel',
as_user: true,
attachments: [
{
fallback: 'Microsoft Corporation (MSFT): $51.91',
title_link: 'http://finance.yahoo.com/q?s=MSFT',
title: 'Microsoft Corporation (MSFT)',
text: '$51.91 (-0.48%)',
color: '#FF0000'
}
]
)
app.send(:message, client, Hashie::Mash.new(channel: 'channel', text: "How's $MSFT?"))
end
end
end
end
41 changes: 41 additions & 0 deletions spec/slack-market/commands/set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe SlackMarket::Commands::Set do
let!(:team) { Fabricate(:team) }
let(:app) { SlackMarket::Server.new(team: team) }
let(:client) { app.send(:client) }
it 'gives help' do
expect(message: "#{SlackRubyBot.config.user} set").to respond_with_slack_message(
'Missing setting, eg. _set dollars on_.'
)
end
context 'dollars' do
it 'shows current value of dollars off' do
expect(message: "#{SlackRubyBot.config.user} set dollars").to respond_with_slack_message(
"Dollar signs for team #{team.name} are off."
)
end
it 'shows current value of dollars on' do
team.update_attributes!(dollars: true)
expect(message: "#{SlackRubyBot.config.user} set dollars").to respond_with_slack_message(
"Dollar signs for team #{team.name} are on!"
)
end
it 'enables dollars' do
team.update_attributes!(dollars: false)
expect(message: "#{SlackRubyBot.config.user} set dollars on").to respond_with_slack_message(
"Dollar signs for team #{team.name} are on!"
)
expect(client.team.dollars).to be true
expect(team.reload.dollars).to be true
end
it 'disables dollars' do
team.update_attributes!(dollars: true)
expect(message: "#{SlackRubyBot.config.user} set dollars off").to respond_with_slack_message(
"Dollar signs for team #{team.name} are off."
)
expect(client.team.dollars).to be false
expect(team.reload.dollars).to be false
end
end
end

0 comments on commit ad3797f

Please sign in to comment.