Class: CoachZed::FeedReader
- Inherits:
-
Object
- Object
- CoachZed::FeedReader
- Defined in:
- lib/coach_zed/feed_reader.rb,
sig/coach_zed.rbs
Defined Under Namespace
Classes: Event
Instance Attribute Summary collapse
-
#feed_content ⇒ String
readonly
Returns the value of attribute feed_content.
Class Method Summary collapse
Instance Method Summary collapse
- #events ⇒ Array[Event]
-
#initialize(feed_content) ⇒ FeedReader
constructor
A new instance of FeedReader.
- #last_date ⇒ Date?
- #parse_date(event_text) ⇒ Date?
- #parse_events ⇒ Array[Event]
- #parse_line(event_text, pattern) ⇒ String?
- #recent_events(limit_days: 28) ⇒ Array[Event]
- #to_context(limit_days: 28) ⇒ String
- #unescape(value) ⇒ String
Constructor Details
#initialize(feed_content) ⇒ FeedReader
Returns a new instance of FeedReader.
25 26 27 |
# File 'lib/coach_zed/feed_reader.rb', line 25 def initialize(feed_content) @feed_content = feed_content end |
Instance Attribute Details
#feed_content ⇒ String (readonly)
Returns the value of attribute feed_content.
29 30 31 |
# File 'lib/coach_zed/feed_reader.rb', line 29 def feed_content @feed_content end |
Class Method Details
.load(path) ⇒ Array[Event]
17 18 19 |
# File 'lib/coach_zed/feed_reader.rb', line 17 def self.load(path) new(File.read(path.to_s)).events end |
.load_existing(path) ⇒ FeedReader
21 22 23 |
# File 'lib/coach_zed/feed_reader.rb', line 21 def self.load_existing(path) new(File.read(path.to_s)) end |
Instance Method Details
#events ⇒ Array[Event]
31 32 33 |
# File 'lib/coach_zed/feed_reader.rb', line 31 def events @events ||= parse_events end |
#last_date ⇒ Date?
35 36 37 |
# File 'lib/coach_zed/feed_reader.rb', line 35 def last_date events.map(&:date).compact.max end |
#parse_date(event_text) ⇒ Date?
74 75 76 77 78 79 |
# File 'lib/coach_zed/feed_reader.rb', line 74 def parse_date(event_text) value = parse_line(event_text, /^DTSTART;VALUE=DATE:(\d{8})$/) return if value.nil? Date.strptime(value, "%Y%m%d") end |
#parse_events ⇒ Array[Event]
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/coach_zed/feed_reader.rb', line 60 def parse_events blocks = feed_content.split(/BEGIN:VEVENT\r?\n/).drop(1) blocks.filter_map do |block| event_text = block.split("END:VEVENT").first next if event_text.nil? Event.new( date: parse_date(event_text), summary: parse_line(event_text, /^SUMMARY:(.*)$/), description: unescape(parse_line(event_text, /^DESCRIPTION:(.*)$/)) ) end end |
#parse_line(event_text, pattern) ⇒ String?
81 82 83 84 |
# File 'lib/coach_zed/feed_reader.rb', line 81 def parse_line(event_text, pattern) match = event_text.match(pattern) match && match[1] end |
#recent_events(limit_days: 28) ⇒ Array[Event]
39 40 41 42 43 44 45 |
# File 'lib/coach_zed/feed_reader.rb', line 39 def recent_events(limit_days: 28) last = last_date cutoff = last ? last - (limit_days - 1) : nil return events if cutoff.nil? events.select { |event| event.date && event.date >= cutoff } end |
#to_context(limit_days: 28) ⇒ String
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/coach_zed/feed_reader.rb', line 47 def to_context(limit_days: 28) recent_events(limit_days:).map do |event| # @type var pieces: Array[String] pieces = [] pieces << event.date.iso8601 if event.date pieces << event.summary if event.summary && !event.summary.empty? pieces << event.description if event.description && !event.description.empty? pieces.join(" | ") end.join("\n") end |
#unescape(value) ⇒ String
86 87 88 89 90 91 92 |
# File 'lib/coach_zed/feed_reader.rb', line 86 def unescape(value) value.to_s .gsub("\\n", "\n") .gsub("\\,", ",") .gsub("\\;", ";") .gsub("\\\\", "\\") end |