1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require "erb"
require "minitest/test_task"
require "pathname"

Minitest::TestTask.create

task default: :test

# MealViewer school lookups, as they appear in https://schools.mealviewer.com/school/<lookup>.
SCHOOLS = ["McDonald"]
# Fixed rather than read from the menu so feeds keep existing over breaks.
BLOCKS = ["Breakfast", "Lunch"]
SITE = Pathname("_site")

INDEX = ERB.new(<<~'ERB', trim_mode: "-")
  <!doctype html>
  <meta charset="utf-8">
  <title>School menus</title>
  <h1>School menus</h1>
  <p>Click a meal to subscribe to its calendar.</p>
  <ul>
  <%- schools.each do |school, feeds| -%>
    <li>
      <%= ERB::Util.h(school) %>:
  <%- feeds.each do |block, href| -%>
      <a class="subscribe" href="<%= ERB::Util.h(href) %>"><%= ERB::Util.h(block) %></a>
  <%- end -%>
    </li>
  <%- end -%>
  </ul>
  <script>
    for (const link of document.querySelectorAll("a.subscribe")) {
      link.href = link.href.replace(/^https?:/, "webcal:");
    }
  </script>
ERB

def slug(name) = name.downcase.gsub(/[^a-z0-9]+/, "-")

desc "Write a calendar feed per school and meal, plus a landing page, to #{SITE}"
task :build do
  require_relative "lib/menu_feed"

  today = Date.today
  SITE.mkpath
  schools = SCHOOLS.to_h do |school|
    feed = MenuFeed.fetch(school, today - 7, today + 35)
    feeds = BLOCKS.to_h do |block|
      ics = "#{slug(school)}-#{slug(block)}.ics"
      (SITE / ics).write(feed.to_ical(block))
      puts "Wrote #{feed.meals.count { it.block_name == block }} meals to #{SITE / ics}"

      [block, ics]
    end

    [feed.school, feeds]
  end

  (SITE / "index.html").write(INDEX.result_with_hash(schools:))
end