/
standalone.rb
executable file
·58 lines (52 loc) · 1.48 KB
/
standalone.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
#!/usr/bin/ruby
require 'webrick'
require 'net/http'
module Ssb
class App
def update(user, token, data, params = nil)
Ssb::Api.update_a(user, token, data)
end
def get_instance(server)
self
end
def service(req, res)
match_data = (%r!\A/(\w+?)/(\w+?)/stocks/(\w+?)\z!).match(req.path_info)
user, token, m = match_data.values_at(1, 2, 3)
params = { }
req.query_string.split('&').each do |v|
h = v.split('=')
params[h[0]] = h[1]
end
data = req.query
original_response = __send__(m, user, token, data, params)
res.body = "#{params['callback']}(#{original_response.body})"
res['Content-Type'] = original_response['Content-Type']
end
end
module Api
def self.update_a(user, token, data)
response = nil
Net::HTTP.start("stack.nayutaya.jp", 80) do |http|
uri = "/api/#{user}/#{token}/stocks/update.1"
response = http.post(uri, data.map{|k, v| "#{k}=#{v}" }.join("&"))
end
response
end
end
end
document_root = ARGV[0] || '.'
port = ARGV[1] || 10080
server = WEBrick::HTTPServer.new({ :DocumentRoot => document_root,
#:BindAddress => '127.0.0.1',
:BindAddress => '0.0.0.0',
:Port => port})
Signal.trap("INT"){ server.shutdown }
app = Ssb::App.new
server.mount '/ssb/api/', app
server.start
def usage
STDERR.puts <<EOD
usage:
$0 document_roor port
EOD
end