Class: CoachZed::FeedWriter
- Inherits:
-
Object
- Object
- CoachZed::FeedWriter
- Defined in:
- lib/coach_zed/feed_writer.rb,
sig/coach_zed.rbs
Instance Attribute Summary collapse
-
#calendar_name ⇒ String?
readonly
Returns the value of attribute calendar_name.
-
#schedule ⇒ Hash[String, untyped]
readonly
Returns the value of attribute schedule.
Instance Method Summary collapse
- #build ⇒ String
- #escape(value) ⇒ String
- #event_description(day) ⇒ String
- #event_lines ⇒ Array[String]
- #event_summary(day) ⇒ String
- #fresh_feed ⇒ String
- #generated_timestamp ⇒ String
- #header_lines ⇒ Array[String]
-
#initialize(schedule:, calendar_name: nil) ⇒ FeedWriter
constructor
A new instance of FeedWriter.
- #schedule_name ⇒ String
Constructor Details
#initialize(schedule:, calendar_name: nil) ⇒ FeedWriter
Returns a new instance of FeedWriter.
8 9 10 11 |
# File 'lib/coach_zed/feed_writer.rb', line 8 def initialize(schedule:, calendar_name: nil) @schedule = schedule @calendar_name = calendar_name end |
Instance Attribute Details
#calendar_name ⇒ String? (readonly)
Returns the value of attribute calendar_name.
19 20 21 |
# File 'lib/coach_zed/feed_writer.rb', line 19 def calendar_name @calendar_name end |
#schedule ⇒ Hash[String, untyped] (readonly)
Returns the value of attribute schedule.
19 20 21 |
# File 'lib/coach_zed/feed_writer.rb', line 19 def schedule @schedule end |
Instance Method Details
#build ⇒ String
13 14 15 |
# File 'lib/coach_zed/feed_writer.rb', line 13 def build fresh_feed end |
#escape(value) ⇒ String
82 83 84 85 86 87 88 89 |
# File 'lib/coach_zed/feed_writer.rb', line 82 def escape(value) value.to_s .gsub("\\", "\\\\") .gsub(";", "\\;") .gsub(",", "\\,") .gsub("\r\n", "\\n") .gsub("\n", "\\n") end |
#event_description(day) ⇒ String
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/coach_zed/feed_writer.rb', line 70 def event_description(day) # @type var pieces: Array[String] pieces = [] pieces << day["notes"].to_s if day["notes"] && !day["notes"].to_s.empty? if day["day_type"] == "workout" workout = day.fetch("workout") catalog_text = workout["catalog_text"].to_s pieces << catalog_text unless catalog_text.empty? end pieces.join("\n") end |
#event_lines ⇒ Array[String]
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/coach_zed/feed_writer.rb', line 40 def event_lines schedule.fetch("days").flat_map do |day| date = Date.strptime(day.fetch("date"), "%Y-%m-%d") [ "BEGIN:VEVENT", "UID:#{date.strftime("%Y%m%d")}@coach_zed", "DTSTAMP:#{}", "DTSTART;VALUE=DATE:#{date.strftime("%Y%m%d")}", "DTEND;VALUE=DATE:#{(date + 1).strftime("%Y%m%d")}", "SUMMARY:#{escape(event_summary(day))}", "DESCRIPTION:#{escape(event_description(day))}", "END:VEVENT" ] end end |
#event_summary(day) ⇒ String
64 65 66 67 68 |
# File 'lib/coach_zed/feed_writer.rb', line 64 def event_summary(day) return "Rest" if day["day_type"] == "rest" day.fetch("workout").fetch("title") end |
#fresh_feed ⇒ String
21 22 23 24 25 26 |
# File 'lib/coach_zed/feed_writer.rb', line 21 def fresh_feed lines = header_lines lines.concat(event_lines) lines << "END:VCALENDAR" lines.join("\r\n") + "\r\n" end |
#generated_timestamp ⇒ String
60 61 62 |
# File 'lib/coach_zed/feed_writer.rb', line 60 def Time.now.utc.strftime("%Y%m%dT%H%M%SZ") end |
#header_lines ⇒ Array[String]
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/coach_zed/feed_writer.rb', line 28 def header_lines [ "BEGIN:VCALENDAR", "VERSION:2.0", "PRODID:-//CoachZed//EN", "CALSCALE:GREGORIAN", "METHOD:PUBLISH", "X-WR-CALNAME:#{escape(schedule_name)}", "X-WR-TIMEZONE:America/New_York" ] end |
#schedule_name ⇒ String
56 57 58 |
# File 'lib/coach_zed/feed_writer.rb', line 56 def schedule_name calendar_name || schedule["program_name"] || "Training Schedule" end |