ig3tool 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/web.rb DELETED
@@ -1,98 +0,0 @@
1
- module Ig3tool
2
-
3
- require 'mongrel'
4
- require 'yaml'
5
- require 'facets'
6
-
7
- class WebHandler < Mongrel::HttpHandler
8
- def decode_body(request)
9
- if request.params[Mongrel::Const::REQUEST_METHOD] == "POST" then
10
- type = request.params['CONTENT_TYPE'].sub(/;.*/,'')
11
- res = case type
12
- when "application/x-yaml","text/yaml", "text/x-yaml"
13
- YAML.load request.body
14
- when "application/x-www-form-urlencoded"
15
- Mongrel::HttpRequest.query_parse request.body.read
16
- else
17
- warn "Warning: don't know how to handle Content-Type: #{type}"
18
- {}
19
- end
20
- res.stringify_keys!
21
- else
22
- {}
23
- end
24
- end
25
-
26
- def process(request, response)
27
- path = request.params[Mongrel::Const::PATH_INFO]
28
-
29
- # strip leading /, convert all remaining / to _
30
- (path, params) = path.split('//')
31
- params = (params || "").split("/")
32
- command = path.tr('/', '_').sub(/_+$/,'').sub(/^_+/,"")
33
-
34
- puts "Handling request for #{path}; params: #{params.inspect}"
35
-
36
- begin
37
-
38
- if command.empty? then
39
- response.start(503) do |head,out|
40
- messages = Ig3tool::Actions.public_methods.reject {|x| Object.respond_to? x or x.starts_with? "_" }
41
- out << "Please enter a query. Possible commands: #{messages.sort.join(', ')}"
42
- end
43
- return
44
- else
45
- type = request.params[Mongrel::Const::REQUEST_METHOD]
46
- extra = decode_body request
47
- extra.update( :from_uri => params, :given_uri => params.size, :type => type)
48
- puts "Extra params: #{extra.inspect}"
49
-
50
- command = command + '!' if type == 'POST'
51
-
52
- if command != "wannabe!" and command != "register!"
53
- raise Token, "you need a key" if params.size < 1
54
- token = params[0]
55
- raise Token, "invalid key" if not Bitch.can_request?(params[0])
56
- end
57
-
58
- r = Ig3tool::Request.new
59
- r.bitch = Bitch.bitch(token)
60
- r.request = "REQ: " + command
61
- r.request += " ; " + params.join(", ")
62
- r.request += " ; " + extra.to_a.map{|e| e.join(" => ")}.join(", ")
63
- r.save!
64
-
65
- answer = Ig3tool::Actions.send(command, extra)
66
- raised = false
67
- end
68
-
69
- rescue IG3Error, NoMethodError => boom
70
- answer = { "type" => boom.class.to_s, "message" => boom.message }
71
- raised = true
72
- rescue Exception => boom
73
- warn "==== EXCEPTION ===="
74
- warn "ERROR OCCURED: #{boom.to_s}\n"
75
- warn "Backtrace: \n"
76
- warn boom.backtrace.collect {|x| "\t"+x}.join("\n")
77
- warn "==== EXCEPTION ===="
78
- answer = { "type" => "InternalError", "message" => boom.to_s, "backtrace" => boom.backtrace }
79
- raised = true
80
- end
81
-
82
- code = raised ? 500 : 200
83
- response.start(code) do |head,out|
84
- case request.params['HTTP_ACCEPT']
85
- when /text\/x-yaml/, /text\/yaml/, /application\/yaml/
86
- head["Content-Type"] = "application/yaml"
87
- out << answer.to_yaml
88
-
89
- else # text dump
90
- head["Content-Type"] = "text/plain"
91
- out << answer.inspect
92
- end
93
- end
94
- end
95
-
96
- end
97
-
98
- end