Class: CoachZed::FeedWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/coach_zed/feed_writer.rb,
sig/coach_zed.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameString? (readonly)

Returns the value of attribute calendar_name.

Returns:

  • (String, nil)


19
20
21
# File 'lib/coach_zed/feed_writer.rb', line 19

def calendar_name
  @calendar_name
end

#scheduleHash[String, untyped] (readonly)

Returns the value of attribute schedule.

Returns:

  • (Hash[String, untyped])


19
20
21
# File 'lib/coach_zed/feed_writer.rb', line 19

def schedule
  @schedule
end

Instance Method Details

#buildString

Returns:

  • (String)


13
14
15
# File 'lib/coach_zed/feed_writer.rb', line 13

def build
  fresh_feed
end

#escape(value) ⇒ String

Parameters:

  • value (Object)

Returns:

  • (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

Parameters:

  • day (Hash[String, untyped])

Returns:

  • (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_linesArray[String]

Returns:

  • (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:#{generated_timestamp}",
      "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

Parameters:

  • day (Hash[String, untyped])

Returns:

  • (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_feedString

Returns:

  • (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_timestampString

Returns:

  • (String)


60
61
62
# File 'lib/coach_zed/feed_writer.rb', line 60

def generated_timestamp
  Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
end

#header_linesArray[String]

Returns:

  • (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_nameString

Returns:

  • (String)


56
57
58
# File 'lib/coach_zed/feed_writer.rb', line 56

def schedule_name
  calendar_name || schedule["program_name"] || "Training Schedule"
end