-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathparse_xl_funnies.rb
61 lines (48 loc) · 1.33 KB
/
parse_xl_funnies.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
# html_ci: false
# This is due to a button rendering issue
require 'nokogiri'
require 'open-uri'
require 'tempfile'
class Comic
attr_reader :rss, :title
def initialize(file_path)
@rss = Nokogiri::XML(File.open(file_path))
@title = @rss.at("//channel/title").text
end
def items
@rss.search("//channel/item")
end
def latest_image
item = @rss.at("//channel/item[1]")
description = item.at("description").text
image_url = description.match(/src="([^"]+\.\w+)"/)[1]
title = item.at("title").text
[title, image_url]
end
end
Shoes.app width: 800, height: 600 do
background "#555"
@title = "Web Funnies"
stack margin: 10 do
title strong(@title), align: "center", stroke: "#DFA", margin: 0
para "(loaded from RSS feeds)", align: "center", stroke: "#DFA", margin: 0
url = "https://xkcd.com/rss.xml"
rss_string = URI.open(url).read
file = Tempfile.new('download')
file.write(rss_string)
file.close
file_path = file.path
c = Comic.new(file_path)
stack width: "100%", margin: 10, border: 1 do
stack do
background "#333", curve: 4
caption c.title, stroke: "#CD9", margin: 4
end
title, image_url = c.latest_image
flow do
image image_url
para strong(title), margin: 8, stroke: white
end
end
end
end