Skip to content

Commit

Permalink
lint a random file (#3617)
Browse files Browse the repository at this point in the history
  • Loading branch information
osc-bot authored Jun 20, 2024
1 parent c4668f2 commit 78bed0e
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions apps/dashboard/app/models/motd_formatter/osc.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
# frozen_string_literal: true

module MotdFormatter
# Utility class for rendering MOTD files in the OSC format.
class Osc

attr_reader :content, :title

# Parse the MOTD in OSC style
# See: https://github.com/OSC/ood-dashboard/wiki/Message-of-the-Day
#
# @param [MotdFile] motd_file an MotdFile object that contains a URI path to a message of the day in OSC format
def initialize(motd_file)
motd_file = MotdFile.new unless motd_file
motd_file ||= MotdFile.new
@title = motd_file.title
@content = motd_file.content
end

Message = Struct.new :date, :title, :body do
def self.from(str)
if str =~ /(\d+[-\/\.]\d+[-\/\.]\d+)\n--- ([ \S ]*)\n(.*)/m
Message.new(Date.parse($1), $2, $3)
else
nil
if str =~ %r{(\d+[-/.]\d+[-/.]\d+)\n--- ([ \S ]*)\n(.*)}m
Message.new(Date.parse(Regexp.last_match(1)), Regexp.last_match(2), Regexp.last_match(3))
end
rescue ArgumentError => e
Rails.logger.warn("MOTD message poorly formatted: #{e} => #{e.message}")
Expand All @@ -29,16 +28,16 @@ def self.from(str)
end

def to_partial_path
"dashboard/motd_osc"
'dashboard/motd_osc'
end

# since this actually splits the file content into separate messages
# we can still use this as it is
def messages
if content
# get array of sections which are delimited by a row of ******
sections = content.split(/^\*+$/).map(&:strip).select { |x| ! x.empty? }
return sections.map { |s| Message.from(s) }.compact.sort_by {|s| s.date }.reverse
sections = content.split(/^\*+$/).map(&:strip).reject(&:empty?)
sections.map { |s| Message.from(s) }.compact.sort_by(&:date).reverse
else
[]
end
Expand Down

0 comments on commit 78bed0e

Please sign in to comment.