mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Calendar feed: due items (inspections, ADs, component limits, documents) as a private iCalendar link, hour-based dates projected from the past year's flying
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
87e30946fe
commit
98e943cf0a
14 files changed
+266
-7
No files matched your search
@@ -0,0 +1,46 @@
|
||||
# A minimal iCalendar (RFC 5545) writer for all-day events.
|
||||
class Ical
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@events = []
|
||||
end
|
||||
|
||||
def add(uid:, date:, summary:, description: nil, url: nil)
|
||||
@events << { uid: uid, date: date, summary: summary, description: description, url: url }
|
||||
end
|
||||
|
||||
def to_s
|
||||
lines = [ "BEGIN:VCALENDAR", "VERSION:2.0", "PRODID:-//Simple Aircraft Manager//EN", "CALSCALE:GREGORIAN",
|
||||
"METHOD:PUBLISH", "X-WR-CALNAME:#{escape(@name)}", "REFRESH-INTERVAL;VALUE=DURATION:PT6H", "X-PUBLISHED-TTL:PT6H" ]
|
||||
stamp = Time.current.utc.strftime("%Y%m%dT%H%M%SZ")
|
||||
@events.each do |e|
|
||||
lines += [ "BEGIN:VEVENT", "UID:#{e[:uid]}", "DTSTAMP:#{stamp}", "DTSTART;VALUE=DATE:#{e[:date].strftime('%Y%m%d')}",
|
||||
"DTEND;VALUE=DATE:#{(e[:date] + 1).strftime('%Y%m%d')}", "SUMMARY:#{escape(e[:summary])}", "TRANSP:TRANSPARENT" ]
|
||||
lines << "DESCRIPTION:#{escape(e[:description])}" if e[:description].present?
|
||||
lines << "URL:#{e[:url]}" if e[:url].present?
|
||||
lines << "END:VEVENT"
|
||||
end
|
||||
lines << "END:VCALENDAR"
|
||||
lines.map { fold(_1) }.join("\r\n") + "\r\n"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def escape(text) = text.to_s.gsub("\\", "\\\\\\\\").gsub(";", "\;").gsub(",", "\\,").gsub(/\r?\n/, "\\n")
|
||||
|
||||
# Lines over 75 octets continue on the next line after a space.
|
||||
def fold(line)
|
||||
return line if line.bytesize <= 75
|
||||
parts = []
|
||||
current = +""
|
||||
line.each_char do |ch|
|
||||
if (current + ch).bytesize > (parts.empty? ? 75 : 74)
|
||||
parts << current
|
||||
current = +""
|
||||
end
|
||||
current << ch
|
||||
end
|
||||
parts << current
|
||||
parts.first + parts.drop(1).map { "\r\n #{_1}" }.join
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user