-
Notifications
You must be signed in to change notification settings - Fork 92
/
server.rb
executable file
·90 lines (73 loc) · 1.85 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env ruby
require 'optparse'
require 'webrick'
require 'webrick/httpproxy'
require 'singleton'
class TwitterAPI
include Singleton
attr_accessor :res
attr_accessor :req
def self.call(req, res)
uri = req.request_uri
format = File.extname(uri.path)
format = "xml" if format == ""
format = format.gsub(/^\./, '')
endpoint = uri.path.gsub(/\.[a-z]+$/, '').gsub(/^\//, '')
params = Hash.new
(uri.query || "").split("&").each do |param|
if param =~ /([^=]+)=(.+)/
params[$~[1]] = $~[2]
params[$~[1].to_sym] = $~[2]
else
raise ArgumentError.new("Invalid query string: #{param}")
end
end
tw = self.instance
tw.res = res
tw.req = req
tw.send(endpoint.gsub(/\//, '_'), format, params)
end
def statuses_public_timeline(format, params)
@res.body = "URI: #{@req.request_uri.to_s}, format: #{format}, params: #{params.inspect}"
end
end
def parse_options(argv)
parser = OptionParser.new
opt = Hash.new
print_help = Proc.new do |msg|
$stderr.puts msg
puts
puts parser.help
exit 1
end
opt[:port] = 80
parser.on('-p', '--port=PORT',
"\n\t\tPort number to be used.") do |port|
opt[:port] = port.to_i
if opt[:port] == 0
print_help.call("Invalid port number: #{port}")
end
end
opt[:proxy] = false
parser.on('-P', '--proxy',
"\n\t\tRun as a proxy server if this option is specified.") do
opt[:proxy] = true
end
parser.parse!(argv)
opt
end
def main(argv)
opt = parse_options(argv)
server = WEBrick::HTTPServer.new({:Port => opt[:port],
:BindAddress => "127.0.0.1"})
server.mount_proc("/") do |req, res|
TwitterAPI.call(req, res)
end
Signal.trap('INT') do
server.shutdown
end
server.start
end
if __FILE__ == $0
main(ARGV.dup)
end