forked from Pistos/Top-Hacker-News
/
web-server.rb
52 lines (46 loc) · 1.27 KB
/
web-server.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
require 'ramaze'
require 'model/init'
module TopHN
class MainController < Ramaze::Controller
XML_MAP = {
'&' => '&',
'<' => '<',
'>' => '>',
"'" => ''',
'"' => '"',
}
def escape_xml( s )
s.gsub( /[&<>'"]/ ) do |m|
XML_MAP[ m ] || m
end
end
def rss
response[ 'Content-Type' ] = 'application/xml'
items = Models::Item.s(
"SELECT * FROM items ORDER BY time_added DESC LIMIT 50"
).map { |item|
uri_thread = "http://news.ycombinator.com/#{ escape_xml item.uri_hn }"
%{
<item>
<title>#{ escape_xml item.title }</title>
<link>#{ escape_xml item.uri }</link>
<guid>#{ uri_thread }</guid>
<description>#{ uri_thread }</description>
</item>
}
}
%{<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Top Hacker News</title>
<link>http://hn.purepistos.net</link>
<atom:link href="http://hn.purepistos.net/rss" rel="self" type="application/rss+xml"/>
<description>An RSS feed of only the better Hacker News items</description>
#{items.join}
</channel>
</rss>
}
end
end
end
Ramaze.start( :port => 8026, :adapter => :thin, :mode => :live )