module SportsCommentary
SportsCommentary โ template play-by-play lines from SportScore match detail. Public: build, format_scoreline, format_status_line Tests: test/lib/sports_commentary_test.rb
Constants
- MAX_INCIDENTS
- MAX_STATS
Public Instance Methods
Source
# File lib/sports_commentary.rb, line 15 def build(match, sport:) lines = [] lines << format_scoreline(match) status = format_status_line(match) lines << status if status stats = format_stats(match) lines << stats if stats incidents = format_incidents(match) lines << incidents if incidents lines << match["competition"].to_s if lines.length < 3 && match["competition"].to_s.strip != "" lines.map { |line| TextUtil.squish(line) }.reject(&:empty?).first(4) end
Source
# File lib/sports_commentary.rb, line 80 def format_incidents(match) texts = Array(match["incidents"]).last(MAX_INCIDENTS).filter_map do |incident| text = incident["text"].to_s.strip next if text.empty? minute = incident["minute"] minute ? "#{minute}' #{text}" : text end return nil if texts.empty? texts.join(" ยท ") end
Source
# File lib/sports_commentary.rb, line 47 def format_score(value) text = value.to_s.strip return "-" if text.empty? || text == "null" text end
Source
# File lib/sports_commentary.rb, line 31 def format_scoreline(match) home = match["home"].to_s away = match["away"].to_s home_score = format_score(match["home_score"]) away_score = format_score(match["away_score"]) if numeric_scores?(home_score, away_score) "#{home} #{home_score}โ#{away_score} #{away}" else "#{home} #{home_score} โ #{away} #{away_score}" end end
Source
# File lib/sports_commentary.rb, line 66 def format_stats(match) stats = Array(match["stats"]).first(MAX_STATS) return nil if stats.empty? stats.map do |stat| name = stat["name"].to_s home = stat["home"].to_s away = stat["away"].to_s next if name.empty? "#{name}: #{home} โ #{away}" end.compact.join(" ยท ") end
Source
# File lib/sports_commentary.rb, line 54 def format_status_line(match) status = match["status"].to_s status_text = match["status_text"].to_s.strip minute = match["live_minute"] parts = [] parts << "#{minute}'" if minute && minute.to_i.positive? parts << status_text unless status_text.empty? parts << status unless status.empty? || status == status_text.downcase parts.join(" ยท ") end
Source
# File lib/sports_commentary.rb, line 43 def numeric_scores?(home_score, away_score) [home_score, away_score].all? { |score| score.match?(/\A\d+\z/) } end