This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
73 lines (59 loc) · 1.5 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/ruby
require 'cgi'
require 'drb'
require 'rss'
require 'rubygems'
require 'json'
require 'sinatra'
require 'post'
$history = DRbObject.new(nil, "druby://localhost:8777")
def is_valid?(text)
word.size > 2 && !%(test foobar asdf).member?(text) && !text.strip.empty?
end
get '/ajax/last' do
if $history.empty?
init_post = Post.new
seeds = File.read('data/seed.txt').split(/\n+===\n+/)
init_post.text = seeds[rand(seeds.size)]
init_post.ts = Time.now
init_post.num = 0
$history << init_post
end
headers('Content-Type' => 'text/x-json')
$history.last.to_json
end
post '/ajax/next' do
curr_sz = $history.size
seq = params[:num].to_i
p = Post.new
text = params[:text]
if curr_sz == (seq + 1) and is_valid?(text)
p.text = CGI.escapeHTML(text)
p.user = params[:user].gsub(/[^-.\w]/, '')
p.ts = Time.now
p.num = curr_sz
$history << p
$history.save_data
end
headers('Content-Type' => 'text/x-json')
p.to_json
end
get '/ajax/story' do
headers('Content-Type' => 'text/x-json')
$history.to_a.to_json
end
get '/feed/rss' do
feed = RSS::Maker.make("2.0") do |rss|
rss.channel.title = 'misfict'
rss.channel.link = 'http://rcoder.net/misfict/'
rss.channel.description = 'micro.serial.fiction'
recent = $history.recent(10).reverse
recent.each do |entry|
item = rss.items.new_item
item.title = entry.text
item.date = entry.ts
end
end
headers('Content-Type' => 'application/rss+xml')
feed.to_s
end