Skip to content

Commit

Permalink
Support for stocks that Yahoo Finance doesn't return a name for (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljz authored and dblock committed Aug 4, 2016
1 parent 4ed63ff commit 76b6540
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Changelog

* 8/1/2016: [#37](https://github.com/dblock/slack-market/pull/37): Support stocks that Yahoo Finance fails to return a name for, e.g. `300024.SZ` - [@pauljz](https://github.com/pauljz).
* 7/31/2016: [#36](https://github.com/dblock/slack-market/pull/36): Support numeric and alphanumeric stock tickers, eg. `Z74.SI` or `0941.HK` - [@pauljz](https://github.com/pauljz).
* 7/25/2016: Notify subscribing teams - [@dblock](https://github.com/dblock).
* 7/4/2016: [#28](https://github.com/dblock/slack-market/issues/28): Rename Slack action endpoint - [@ddruker](https://github.com/ddruker).
Expand Down
3 changes: 2 additions & 1 deletion slack-market/models/market.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def qualify(stocks, dollars = false)
# return stock quotes
def quotes(stocks, fields = [:name, :symbol, :last_trade_price, :change, :change_in_percent])
YahooFinance::Client.new.quotes(stocks, fields).select do |quote|
quote.name != 'N/A'
quote.name = quote.name != 'N/A' ? quote.name : quote.symbol
quote.last_trade_price != 'N/A'
end
end

Expand Down
51 changes: 51 additions & 0 deletions spec/fixtures/slack/300024_sz.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions spec/models/market_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,25 @@
describe '#quotes' do
it 'filters out N/A responses' do
allow_any_instance_of(YahooFinance::Client).to receive(:quotes).and_return([
Hashie::Mash.new(name: 'N/A'),
Hashie::Mash.new(name: 'MSFT')
Hashie::Mash.new(name: 'N/A', last_trade_price: 'N/A'),
Hashie::Mash.new(name: 'MSFT', last_trade_price: '56.58')
])
quotes = Market.quotes(%w(FOO MSFT))
expect(quotes.size).to eq 1
expect(quotes[0].name).to eq 'MSFT'
end
it 'uses the symbol for the name if the name is N/A' do
allow_any_instance_of(YahooFinance::Client).to receive(:quotes).and_return([
Hashie::Mash.new(
name: 'N/A',
symbol: '300024.SZ',
last_trade_price: '23.71',
change: '+0.10',
change_in_percent: '+0.42%'
)
])
quotes = Market.quotes(%w(300024.SZ))
expect(quotes[0].name).to eq '300024.SZ'
end
end
end
38 changes: 38 additions & 0 deletions spec/slack-market/commands/quote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,44 @@
message_command.call(client, Hashie::Mash.new(channel: 'channel', text: '$z74.si?'))
message_command.call(client, Hashie::Mash.new(channel: 'channel', text: "How's $z74.SI?"))
end
it 'returns a quote for 300024.SZ (no name)', vcr: { cassette_name: '300024.sz', allow_playback_repeats: true } do
expect(client.web_client).to receive(:chat_postMessage).with(
channel: 'channel',
as_user: true,
attachments: [
{
fallback: '300024.SZ (300024.SZ): $23.76',
title_link: 'http://finance.yahoo.com/q?s=300024.SZ',
title: '300024.SZ (300024.SZ)',
text: '$23.76 (-0.17%)',
color: '#FF0000',
callback_id: '300024.SZ',
actions: [
{
name: '1d',
text: '1d',
type: 'button',
value: '300024.SZ- 1d'
},
{
name: '1m',
text: '1m',
type: 'button',
value: '300024.SZ- 1m'
},
{
name: '1y',
text: '1y',
type: 'button',
value: '300024.SZ- 1y'
}
],
image_url: 'http://chart.finance.yahoo.com/z?s=300024.SZ&z=l'
}
]
).exactly(1).times
message_command.call(client, Hashie::Mash.new(channel: 'channel', text: '$300024.SZ?'))
end
it 'returns a quote for MSFT and YHOO', vcr: { cassette_name: 'msft_yahoo_invalid' } do
expect(client.web_client).to receive(:chat_postMessage).with(
channel: 'channel',
Expand Down

0 comments on commit 76b6540

Please sign in to comment.