Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
noah.rb | 325 | 283 | 48.31%
|
42.40%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 #!/usr/bin/env ruby |
2 require 'sinatra/base' |
3 require 'sinatra/namespace' |
4 require 'ohm' |
5 begin |
6 require 'yajl' |
7 rescue LoadError |
8 require 'json' |
9 end |
10 require 'haml' |
11 require 'yaml' |
12 |
13 require File.join(File.dirname(__FILE__), 'lib/models') |
14 require File.join(File.dirname(__FILE__), 'lib/helpers') |
15 |
16 @db_settings = YAML::load File.new(File.join(File.dirname(__FILE__),'config','db.yml')).read |
17 |
18 class NoahApp < Sinatra::Base |
19 register Sinatra::Namespace |
20 helpers Sinatra::NoahHelpers |
21 config_file = YAML::load File.new(File.join(File.dirname(__FILE__),'config','db.yml')).read |
22 db = config_file["#{environment}"] |
23 Ohm.connect(:url => "redis://#{db["host"]}:#{db["port"]}/#{db["db"]}") |
24 |
25 configure do |
26 set :app_file, __FILE__ |
27 set :root, File.dirname(__FILE__) |
28 set :server, %w[thin mongrel webrick kirk] |
29 set :port, 9291 |
30 set :logging, true |
31 set :raise_errors, false |
32 set :show_exceptions, false |
33 end |
34 configure(:development) do |
35 require 'sinatra/reloader' |
36 register Sinatra::Reloader |
37 also_reload "models.rb" |
38 also_reload "helpers.rb" |
39 set :port, 9292 |
40 end |
41 configure(:test) do |
42 set :port, 9294 |
43 end |
44 |
45 get '/' do |
46 content_type "text/html" |
47 |
48 haml :index, :format => :html5 |
49 end |
50 |
51 before do |
52 content_type "application/json" |
53 end |
54 |
55 not_found do |
56 content_type "application/json" |
57 erb :'404' |
58 end |
59 |
60 error do |
61 content_type "application/json" |
62 erb :'500' |
63 end |
64 |
65 namespace "/h" do |
66 |
67 get '/:hostname/:servicename/?' do |hostname, servicename| |
68 h = host_service(hostname, servicename) |
69 if h.nil? |
70 halt 404 |
71 else |
72 h.to_json |
73 end |
74 end |
75 |
76 get '/:hostname/?' do |hostname| |
77 h = host(:name => hostname) |
78 if h.nil? |
79 halt 404 |
80 else |
81 h.to_json |
82 end |
83 end |
84 |
85 get '/?' do |
86 hosts.map {|h| h.to_hash} |
87 if hosts.size == 0 |
88 halt 404 |
89 else |
90 hosts.to_json |
91 end |
92 end |
93 |
94 put '/:hostname/?' do |
95 required_params = ["name", "status"] |
96 data = JSON.parse(request.body.read) |
97 data.keys.sort == required_params.sort ? (host = Host.find_or_create(:name => data['name'], :status => data['status'])) : (raise "Missing Parameters") |
98 if host.valid? |
99 r = {"result" => "success","id" => "#{host.id}","status" => "#{host.status}", "name" => "#{host.name}", "new_record" => host.is_new?} |
100 r.to_json |
101 else |
102 raise "#{host.errors}" |
103 end |
104 end |
105 |
106 delete '/:hostname/?' do |hostname| |
107 host = Host.find(:name => hostname).first |
108 if host |
109 services = [] |
110 Service.find(:host_id => host.id).sort.each {|x| services << x; x.delete} if host.services.size > 0 |
111 host.delete |
112 r = {"result" => "success", "id" => "#{host.id}", "name" => "#{hostname}", "service_count" => "#{services.size}"} |
113 r.to_json |
114 else |
115 halt 404 |
116 end |
117 end |
118 |
119 end |
120 |
121 namespace "/s" do |
122 |
123 get '/:servicename/:hostname/?' do |servicename, hostname| |
124 hs = host_service(hostname, servicename) |
125 if hs.nil? |
126 halt 404 |
127 else |
128 hs.to_json |
129 end |
130 end |
131 |
132 get '/:servicename/?' do |servicename| |
133 s = services(:name => servicename) |
134 s.map {|x| x.to_hash} |
135 if s.empty? |
136 halt 404 |
137 else |
138 s.to_json |
139 end |
140 end |
141 |
142 get '/?' do |
143 if services.empty? |
144 halt 404 |
145 else |
146 services.map {|s| s.to_hash} |
147 services.to_json |
148 end |
149 end |
150 |
151 put '/:servicename/?' do |servicename| |
152 # message format: {"status":"initial_status", "host":"hostname"} |
153 required_params = ["status", "host", "name"] |
154 data = JSON.parse(request.body.read) |
155 if data.keys.sort == required_params.sort |
156 h = Host.find(:name => data['host']).first || (raise "Invalid Host") |
157 service = Service.create(:name => servicename, :status => data['status'], :host => h) |
158 if service.valid? |
159 service.save |
160 r = {"action" => "add", "result" => "success", "id" => service.id, "host" => h.name, "name" => service.name} |
161 r.to_json |
162 else |
163 raise "#{service.errors}" |
164 end |
165 else |
166 raise "Missing Parameters" |
167 end |
168 end |
169 |
170 delete '/:servicename/:hostname/?' do |servicename, hostname| |
171 host = Host.find(:name => hostname).first || (halt 404) |
172 service = Service.find(:name => servicename, :host_id => host.id).first || (halt 404) |
173 if host && service |
174 service.delete |
175 r = {"action" => "delete", "result" => "success", "id" => service.id, "host" => host.name, "service" => servicename} |
176 r.to_json |
177 else |
178 halt 404 |
179 end |
180 end |
181 |
182 end |
183 |
184 namespace "/a" do |
185 |
186 get '/:appname/:config/?' do |appname, config| |
187 app = Application.find(:name => appname).first |
188 if app.nil? |
189 halt 404 |
190 else |
191 c = Configuration.find(:name => config, :application_id => app.id).first |
192 c.to_json |
193 end |
194 end |
195 |
196 get '/:appname/?' do |appname| |
197 app = Application.find(:name => appname).first |
198 if app.nil? |
199 halt 404 |
200 else |
201 app.to_json |
202 end |
203 end |
204 |
205 put '/:appname/?' do |appname| |
206 required_params = ["name"] |
207 data = JSON.parse(request.body.read) |
208 if data.keys.sort == required_params.sort && data['name'] == appname |
209 app = Application.find_or_create(:name => appname) |
210 else |
211 raise "Missing or invalid parameters" |
212 end |
213 if app.valid? |
214 action = app.is_new? ? "create" : "update" |
215 app.save |
216 r = {"result" => "success","id" => app.id, "action" => action, "name" => app.name } |
217 r.to_json |
218 else |
219 raise "#{app.errors}" |
220 end |
221 end |
222 |
223 delete '/:appname/?' do |appname| |
224 app = Application.find(:name => appname).first |
225 if app.nil? |
226 halt 404 |
227 else |
228 configurations = [] |
229 Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0 |
230 app.delete |
231 r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{app.name}", "configurations" => "#{configurations.size}"} |
232 r.to_json |
233 end |
234 end |
235 |
236 get '/?' do |
237 apps = [] |
238 Application.all.sort.each {|a| apps << a.to_hash} |
239 if apps.empty? |
240 halt 404 |
241 else |
242 apps.to_json |
243 end |
244 end |
245 |
246 end |
247 |
248 namespace '/c' do |
249 |
250 # Need to move this out to configuration. |
251 # Maybe bootstrap them from itself? |
252 content_type_mapping = { |
253 :yaml => "text/x-yaml", |
254 :json => "application/json", |
255 :xml => "text/xml" |
256 } |
257 |
258 get '/:appname/:element/?' do |appname, element| |
259 a = Application.find(:name => appname).first |
260 if a.nil? |
261 halt 404 |
262 else |
263 c = Configuration.find(:name => element, :application_id => a.id).first |
264 content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym] |
265 c.body |
266 end |
267 end |
268 |
269 get '/:appname/?' do |appname| |
270 config = [] |
271 a = Application.find(:name => appname).first |
272 if a.nil? |
273 halt 404 |
274 else |
275 Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash} |
276 config.to_json |
277 end |
278 end |
279 |
280 get '/?' do |
281 configs = [] |
282 Configuration.all.sort.each {|c| configs << c.to_hash} |
283 if configs.empty? |
284 halt 404 |
285 else |
286 configs.to_json |
287 end |
288 end |
289 |
290 put '/:appname/:element?' do |appname, element| |
291 app = Application.find_or_create(:name => appname) |
292 config = Configuration.find_or_create(:name => element, :application_id => app.id) |
293 required_params = ["format", "body"] |
294 data = JSON.parse(request.body.read) |
295 data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters") |
296 if config.valid? |
297 action = config.is_new? ? "create" : "update" |
298 dependency_action = app.is_new? ? "created" : "updated" |
299 config.save |
300 r = {"result" => "success","id" => "#{config.id}", "action" => action, "dependencies" => dependency_action, "application" => app.name, "item" => config.name} |
301 r.to_json |
302 else |
303 raise "#{config.errors}" |
304 end |
305 end |
306 |
307 delete '/:appname/:element?' do |appname, element| |
308 app = Application.find(:name => appname).first |
309 if app |
310 config = Configuration.find(:name=> element, :application_id => app.id).first |
311 if config |
312 config.delete |
313 r = {"result" => "success", "id" => "#{config.id}", "action" => "delete", "application" => "#{app.name}", "item" => "#{element}"} |
314 r.to_json |
315 else |
316 halt 404 |
317 end |
318 else |
319 halt 404 |
320 end |
321 end |
322 |
323 end |
324 run! if app_file == $0 |
325 end |
Generated on 2011-01-27 21:32:12 -0500 with rcov 0.9.8