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
|
# File 'lib/web.rb', line 26
def process(request, response)
path = request.params[Mongrel::Const::PATH_INFO]
(path, params) = path.split('//')
params = (params || "").split("/")
command = path.tr('/', '_').sub(/_+$/,'').sub(/^_+/,"")
puts "Handling request for #{path}; params: #{params.inspect}"
begin
if command.empty? then
response.start(503) do |head,out|
messages = Ig3tool::Actions.public_methods.reject {|x| Object.respond_to? x or x.starts_with? "_" }
out << "Please enter a query. Possible commands: #{messages.sort.join(', ')}"
end
return
else
type = request.params[Mongrel::Const::REQUEST_METHOD]
= decode_body request
.update( :from_uri => params, :given_uri => params.size, :type => type)
puts "Extra params: #{.inspect}"
command = command + '!' if type == 'POST'
if command != "wannabe!" and command != "register!"
raise Token, "you need a key" if params.size < 1
token = params[0]
raise Token, "invalid key" if not Bitch.can_request?(params[0])
end
r = Ig3tool::Request.new
r.bitch = Bitch.bitch(token)
r.request = "REQ: " + command
r.request += " ; " + params.join(", ")
r.request += " ; " + .to_a.map{|e| e.join(" => ")}.join(", ")
r.save!
answer = Ig3tool::Actions.send(command, )
raised = false
end
rescue IG3Error, NoMethodError => boom
answer = { "type" => boom.class.to_s, "message" => boom.message }
raised = true
rescue Exception => boom
warn "==== EXCEPTION ===="
warn "ERROR OCCURED: #{boom.to_s}\n"
warn "Backtrace: \n"
warn boom.backtrace.collect {|x| "\t"+x}.join("\n")
warn "==== EXCEPTION ===="
answer = { "type" => "InternalError", "message" => boom.to_s, "backtrace" => boom.backtrace }
raised = true
end
code = raised ? 500 : 200
response.start(code) do |head,out|
case request.params['HTTP_ACCEPT']
when /text\/x-yaml/, /text\/yaml/, /application\/yaml/
head["Content-Type"] = "application/yaml"
out << answer.to_yaml
else head["Content-Type"] = "text/plain"
out << answer.inspect
end
end
end
|