/
episode_summary.rb
65 lines (58 loc) · 1.68 KB
/
episode_summary.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Creates a formatted summary of a collection of episodes.
#
module EpisodeSummary
module_function
# Converts the passed episode items in a summary
# that is formatted based on the passed format.
# @param [Array<#title>] items The episodes to summarize
# @param [Symbol] format The summary format (:html and :json
# supported)
# @return [String]
def process(items, format=:html)
if format == :html
output = html_header + "\n"
output += sorted_summaries(items, :show_name, :title).join("\n")
output += html_footer
output
elsif format == :json
items.map(&:to_json)
else
"Format #{format} not supported"
end
end
def sorted_summaries(items, by_attribute, fallback=nil)
fallback_value = ->(item){ fallback ? item.send(fallback).to_s : 'zzzzzzzz' }
sorted = items.sort do |a,b|
(a.show_name || fallback_value.call(a)) <=> (b.show_name || fallback_value.call(b))
end
sorted.map{|i| html_episode_summary(i) }
end
def html_header
<<-EOS
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body>
<div>
<h1>List of episodes</h1>
<ul>
EOS
end
def html_footer
"</ul></div></body></html>"
end
def html_episode_summary(item)
<<-EOS
<li>
<h2>#{item.show_name} - #{item.title}</h2>
#{("<div><img href='#{item.url}'
src='" + item.image_url +
"' /img></div>") if item.image_url}
<a href="#{item.url}">
#{(item.notes.nil? || item.notes == "") ? 'link' : item.notes }
</a>
#{("<span> Show ref: " + item.show_ref + "</span>") if item.show_ref}
</li>
EOS
end
end