-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb.old
151 lines (123 loc) · 3.47 KB
/
server.rb.old
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'net/http'
require 'json'
require 'sinatra'
set :run, true
set :server, %w{ thin }
set :port, 8080
enable :sessions
before { request.path_info.sub! %r{/$}, "" }
$INDIFFERENT = proc do |h, k|
case k
when String then sym = k.to_sym; h[sym] if h.key?(sym)
when Symbol then str = k.to_s; h[str] if h.key?(str)
end
end
$polls = {
:'this-is-a-poll-name' => {
:name => 'This is a poll name',
:votes => {},
:alt => true,
:voters => []
}
} # Example.
$JSON_ID = '1arifg'
$JSON_BASE = 'https://api.myjson.com'
String.class_eval { def to_uri; URI(self); end }
$polls.default_proc = $INDIFFERENT
def request_json
puts "[!!] Requesting JSON, ID: #{$JSON_ID}"
response = Net::HTTP.get "#{$JSON_BASE}/bins/#{$JSON_ID}".to_uri
$polls = JSON.parse response, {:symbolize_names => true}
$polls.default_proc = $INDIFFERENT
pp $polls
end
def save_json
puts "[!!] Saving JSON, ID: #{$JSON_ID}"
uri = "#{$JSON_BASE}/bins/#{$JSON_ID}".to_uri
puts "[!!]\tURI: #{uri}"
req = Net::HTTP::Put.new uri
req.set_content_type 'application/json'
req.body = $polls.to_json
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request req }
puts "[!!]\tResponse: #{res.inspect}"
end
request_json # Initial JSON retrieval
def make_poll code, name, alt
$polls[code] = {
:name => name,
:votes => {},
:alt => alt,
:voters => []
}
save_json
end
$HEAD_TAGS = <<-HTML
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.0/normalize.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" />
<link rel="stylesheet" type="text/css" href="/styles.css" />
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
<script
src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous">
</script>
HTML
get '/' do
erb :index, :locals => {:head_tags => $HEAD_TAGS}
end
get '/poll' do
redirect '/'
end
get '/share/:code' do
erb :share, :locals => {:head_tags => $HEAD_TAGS}
end
post '/new' do
return nil if $polls.keys.include? params[:code]
make_poll(
params[:code],
params[:name],
params[:alt])
params[:primary].each do |option|
$polls[params[:code]][:votes][option] = {
:number => 0,
:primary => true
}
end
save_json
end
get '/poll/:poll' do
unless $polls.keys.map(&:to_s).include? params[:poll]
return "This poll has not been created/does not exist!"
end
local = {:code => params[:poll], :head_tags => $HEAD_TAGS}
local.merge! $polls[params[:poll]]
erb :poll, :locals => local
end
post '/poll/:poll/cast' do
return nil if $polls[params[:poll]][:voters].include? request.ip
$polls[params[:poll]][:voters].push request.ip
if $polls[params[:poll]][:votes].keys.include? params[:vote]
$polls[params[:poll]][:votes][params[:vote]][:number] += 1
else
$polls[params[:poll]][:votes][params[:vote]] = {
:number => 1,
:primary => false
}
end
save_json
end
get '/poll/:poll/votes.json' do
$polls[params[:poll]][:votes].to_json
end
get '/polls.json' do
$polls.keys.map(&:to_s).to_json
end
get '/exported.json' do
$polls.to_json
end