Skip to content

Commit fd012cb

Browse files
committed
Add option for weekly and daily and hourly events
1 parent 7de4d66 commit fd012cb

File tree

4 files changed

+102
-27
lines changed

4 files changed

+102
-27
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: "Push events daily"
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '0 15 * * *'
7+
8+
jobs:
9+
syndicate:
10+
name: "Post events"
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: "Check out repository"
15+
uses: actions/checkout@v3
16+
17+
- uses: ruby/setup-ruby@v1
18+
with:
19+
bundler-cache: true
20+
21+
- run: bundle exec ruby main.rb --destinations=TD
22+
env:
23+
SYN_ENV: production
24+
TD_SLACK_WEBHOOK: ${{ secrets.TD_SLACK_WEBHOOK }}

event.rb

+2-12
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,9 @@ def self.parse_duration(iso8601_duration)
2020
parts.join(", ") + " long"
2121
end
2222

23-
def self.within_next_two_weeks?(date_string)
24-
date = Date.parse(date_string)
25-
today = Date.today
26-
date >= today && date <= (today + 14)
27-
end
28-
2923
def self.format_slack(group)
3024
return if group["unifiedEvents"]["count"] == 0
3125

32-
return unless within_next_two_weeks?(group["unifiedEvents"]["edges"][0]["node"]["dateTime"])
33-
3426
event_blocks = [{
3527
type: "section",
3628
text: {
@@ -57,12 +49,10 @@ def self.format_slack(group)
5749
}
5850
]
5951
},
60-
{
61-
type: "divider"
62-
}]
52+
]
6353

6454
if group["name"] == "Tampa Devs"
65-
event_blocks[0][:text][:text].prepend(":tampadevs: ")
55+
event_blocks[0][:text][:text] = ":tampadevs:" + " " + event_blocks[0][:text][:text]
6656
end
6757

6858
if group["unifiedEvents"]["edges"][0]["node"]["venue"]

main.rb

+50-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "json"
44
require "net/http"
5+
require 'optparse'
56
require_relative "event"
67
require_relative "slack"
78

@@ -12,8 +13,18 @@ def initialize
1213
@dry_run = ENV["SYN_ENV"] != "production"
1314
end
1415

15-
def fetch
16-
groups = JSON.parse(Net::HTTP.get(URI("https://events.api.tampa.dev/")))
16+
def fetch(announcement_type, destinations)
17+
18+
events_url = case announcement_type
19+
when :weekly
20+
'https://events.api.tampa.dev?within_days=7'
21+
when :daily
22+
'https://events.api.tampa.dev?within_hours=24'
23+
when :hourly
24+
'https://events.api.tampa.dev?within_hours=1'
25+
end
26+
27+
groups = JSON.parse(Net::HTTP.get(URI(events_url)))
1728

1829
sorted_events = []
1930
formatted_events = []
@@ -31,14 +42,48 @@ def fetch
3142
formatted_events << event unless event.nil?
3243
end
3344

45+
# fencepost
46+
formatted_events[0...-1].each do |element|
47+
element << { type: "divider" }
48+
end
49+
3450
if formatted_events.empty?
3551
puts "No events to post, exiting with nothing to do."
3652
exit
3753
end
3854

39-
Slack.syndicate(formatted_events, @dry_run)
55+
Slack.syndicate(formatted_events, announcement_type, destinations, @dry_run)
4056
end
4157
end
4258

43-
syn = EventSyndicator.new
44-
syn.fetch
59+
def main
60+
options = {}
61+
OptionParser.new do |opts|
62+
opts.banner = "Usage: main.rb [options]"
63+
64+
# Boolean argument for --daily, --hourly, or --weekly
65+
announcement_types = [:daily, :hourly, :weekly]
66+
opts.on("--daily", "Set announcement type to daily") do
67+
options[:announcement_type] = :daily
68+
end
69+
opts.on("--hourly", "Set announcement type to hourly") do
70+
options[:announcement_type] = :hourly
71+
end
72+
opts.on("--weekly", "Set announcement type to weekly") do
73+
options[:announcement_type] = :weekly
74+
end
75+
76+
# Argument for --destinations=<destinations>
77+
opts.on("--destinations=DESTINATIONS", "Comma-separated list of destinations") do |destinations|
78+
options[:destinations] = destinations.split(',')
79+
end
80+
end.parse!
81+
82+
announcement_type = options[:announcement_type] || :weekly
83+
destinations = options[:destinations] || ['TD', 'TBT', 'TBUX']
84+
85+
syn = EventSyndicator.new
86+
syn.fetch(announcement_type, destinations)
87+
end
88+
89+
main

slack.rb

+26-10
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,33 @@ def initialize
1717
@message = []
1818
end
1919

20-
def self.syndicate(events, dry_run)
20+
def self.syndicate(events, announcement_type, destinations, dry_run)
2121
return if events.empty?
2222

23-
@payload = payload(events)
23+
@payload = payload(events, announcement_type)
2424

2525
if dry_run
2626
puts message_json
2727
else
28-
post
28+
post destinations
2929
end
3030
end
3131

32-
def self.payload(events)
32+
def self.payload(events, announcement_type)
33+
header_text = case announcement_type
34+
when :weekly
35+
":balloon: Happening This Week"
36+
when :daily
37+
":earth_americas: Happening Today"
38+
when :hourly
39+
":loudspeaker: Happening Soon"
40+
end
3341
header = [
3442
{
35-
type: "section",
43+
type: "header",
3644
text: {
37-
type: "mrkdwn",
38-
text: ":balloon: Upcoming events:"
45+
type: "plain_text",
46+
text: header_text,
3947
}
4048
},
4149
{
@@ -44,6 +52,7 @@ def self.payload(events)
4452
]
4553

4654
footer = [
55+
{ type: "divider" },
4756
{
4857
type: "actions",
4958
elements: [
@@ -101,7 +110,12 @@ def self.payload(events)
101110
}
102111
]
103112

104-
[header, events.reduce([], :concat), footer].reduce([], :concat)
113+
elements = [header, events.reduce([], :concat)]
114+
if @announcement_type == 'weekly'
115+
elements << footer
116+
end
117+
118+
elements.reduce([], :concat)
105119
end
106120

107121
def self.message_json
@@ -110,10 +124,12 @@ def self.message_json
110124
}.to_json
111125
end
112126

113-
def self.post
127+
def self.post(destinations)
114128
return if @payload.length == 0
115129

116-
targets = [ENV["TD_SLACK_WEBHOOK"], ENV["TBT_SLACK_WEBHOOK"], ENV["TBUX_SLACK_WEBHOOK"]]
130+
targets = destinations.map do |channel|
131+
ENV["#{channel}_SLACK_WEBHOOK"]
132+
end
117133

118134
targets.each do |t|
119135
uri = URI.parse(t)

0 commit comments

Comments
 (0)