Skip to content

Commit e99389c

Browse files
committed
PTS component
Signed-off-by: Steven Hoffman <fustrate@me.com>
1 parent 4deb414 commit e99389c

3 files changed

Lines changed: 124 additions & 5 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
class Baseballbot
4+
module Templates
5+
module Blocks
6+
class PickTheStick < Block
7+
BASE_URL = 'https://www.pick-the-stick.com/api/standings?api_token=%<token>s&team=%<team_code>s&sort=desc'
8+
9+
include MarkdownHelpers
10+
11+
TABLE_HEADERS = [
12+
['Rank', :center],
13+
['User', :center],
14+
['Points', :center],
15+
['Total Picks', :center],
16+
['Position Change', :center]
17+
]
18+
19+
def render
20+
return '[](/pickthestick "Team not configured")' unless @subreddit.team&.code
21+
22+
table(headers: TABLE_HEADERS, rows:)
23+
end
24+
25+
protected
26+
27+
def rows
28+
pick_the_stick_data.first(10).map do |entry|
29+
[
30+
entry['ranking'],
31+
entry['username'],
32+
entry['points'],
33+
entry['total_picks'],
34+
entry['position_change']
35+
]
36+
end
37+
end
38+
39+
def pick_the_stick_data
40+
api_token = ENV.fetch('BASEBALLBOT_PTS_TOKEN', nil)
41+
42+
raise 'API token is required' unless api_token && !api_token.empty?
43+
44+
raise 'Team code is required' if team_code.nil? || team_code.empty?
45+
46+
JSON.parse(URI.parse(format(BASE_URL, api_token:, team_code:)).open.read)
47+
end
48+
end
49+
end
50+
end
51+
end

lib/baseballbot/templates/components/pick_the_stick.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,42 @@ class Baseballbot
44
module Templates
55
module Components
66
class PickTheStick
7-
BASE_URL = 'https://www.pick-the-stick.com/api/standings?api_token=%<token>s&team=%<team_code>s'
7+
BASE_URL = 'https://www.pick-the-stick.com/api/standings?api_token=%<token>s&team=%<team_code>s&sort=desc'
88

99
include MarkdownHelpers
1010

11+
TABLE_HEADERS = [
12+
['Rank', :center],
13+
['User', :center],
14+
['Points', :center],
15+
['Total Picks', :center],
16+
['Position Change', :center]
17+
]
18+
1119
def initialize(subreddit)
1220
@subreddit = subreddit
1321
end
1422

15-
def current_standings
23+
def to_s
1624
return '[](/pickthestick "Team not configured")' unless @subreddit.team&.code
1725

18-
@data = pick_the_stick_data
19-
20-
''
26+
table(headers: TABLE_HEADERS, rows:)
2127
end
2228

2329
protected
2430

31+
def rows
32+
pick_the_stick_data.first(10).map do |entry|
33+
[
34+
entry['ranking'],
35+
entry['username'],
36+
entry['points'],
37+
entry['total_picks'],
38+
entry['position_change']
39+
]
40+
end
41+
end
42+
2543
def pick_the_stick_data
2644
api_token = ENV.fetch('BASEBALLBOT_PTS_TOKEN', nil)
2745

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Baseballbot::Templates::Components::PickTheStick do
4+
let(:bot) { Baseballbot.new(user_agent: 'Baseballbot Tests') }
5+
let(:subreddit) do
6+
Baseballbot::Subreddit.new(
7+
{ name: 'dodgers', team_code: 'LAD', team_id: 119, options: {} },
8+
bot:,
9+
bot_account: Baseballbot::Bot.new(bot:, name: 'RSpecTestBot', access: '')
10+
)
11+
end
12+
13+
before { stub_requests! with_response: true }
14+
15+
describe '#to_s' do
16+
it 'returns a fallback markdown link when team is not configured' do
17+
no_team_subreddit = Baseballbot::Subreddit.new(
18+
{ name: 'baseball', team_id: nil, options: {} },
19+
bot:,
20+
bot_account: Baseballbot::Bot.new(bot:, name: 'RSpecTestBot', access: '')
21+
)
22+
23+
expect(described_class.new(no_team_subreddit).to_s).to eq '[](/pickthestick "Team not configured")'
24+
end
25+
26+
it 'renders a standings table from pick the stick data' do
27+
component = described_class.new(subreddit)
28+
29+
data = (1..11).map do |rank|
30+
{
31+
'ranking' => rank,
32+
'username' => "user#{rank}",
33+
'points' => 100 - rank,
34+
'total_picks' => 20 + rank,
35+
'position_change' => "+#{rank}"
36+
}
37+
end
38+
39+
allow(component).to receive(:pick_the_stick_data).and_return(data)
40+
41+
output = component.to_s
42+
43+
expect(output).to include('|Rank|User|Points|Total Picks|Position Change|')
44+
expect(output).to include('|:-:|:-:|:-:|:-:|:-:|')
45+
expect(output).to include('|1|user1|99|21|+1|')
46+
expect(output).to include('|10|user10|90|30|+10|')
47+
expect(output).not_to include('|11|user11|89|31|+11|')
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)