Class: Ig3tool::WebHandler

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/web.rb

Instance Method Summary collapse

Instance Method Details

#decode_body(request) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/web.rb', line 8

def decode_body(request)
	if request.params[Mongrel::Const::REQUEST_METHOD] == "POST" then
		type = request.params['CONTENT_TYPE'].sub(/;.*/,'')
		res = case type
			when "application/x-yaml","text/yaml", "text/x-yaml"
				YAML.load request.body
			when "application/x-www-form-urlencoded"
				Mongrel::HttpRequest.query_parse request.body.read
			else
				warn "Warning: don't know how to handle Content-Type: #{type}"
				{}
			end
		res.stringify_keys!
	else
		{}
	end
end

#process(request, response) ⇒ Object



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]

	# strip leading /, convert all remaining / to _
	(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]
			extra = decode_body request
			extra.update( :from_uri => params, :given_uri => params.size, :type => type)
			puts "Extra params: #{extra.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 += " ; " +  extra.to_a.map{|e| e.join(" => ")}.join(", ")
			r.save!

			answer = Ig3tool::Actions.send(command, extra)
			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 # text dump
			head["Content-Type"] = "text/plain"
			out << answer.inspect
		end
	end
end