Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
noah.rb | 326 | 285 | 82.52%
|
81.05%
|
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 |hostname| |
95 required_params = ["name", "status"] |
96 data = JSON.parse(request.body.read) |
97 (data.keys.sort == required_params.sort && data['name'] == hostname) ? (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 required_params = ["status", "host", "name"] |
153 data = JSON.parse(request.body.read) |
154 if data.keys.sort == required_params.sort |
155 h = Host.find(:name => data['host']).first || (raise "Invalid Host") |
156 service = Service.find_or_create(:name => servicename, :status => data['status'], :host => h) |
157 if service.valid? |
158 action = service.is_new? ? "create" : "update" |
159 service.save |
160 r = {"action" => action, "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 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" => "#{appname}", "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 :string => "text/plain" |
257 } |
258 |
259 get '/:appname/:element/?' do |appname, element| |
260 a = Application.find(:name => appname).first |
261 if a.nil? |
262 halt 404 |
263 else |
264 c = Configuration.find(:name => element, :application_id => a.id).first |
265 content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym] |
266 c.body |
267 end |
268 end |
269 |
270 get '/:appname/?' do |appname| |
271 config = [] |
272 a = Application.find(:name => appname).first |
273 if a.nil? |
274 halt 404 |
275 else |
276 Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash} |
277 config.to_json |
278 end |
279 end |
280 |
281 get '/?' do |
282 configs = [] |
283 Configuration.all.sort.each {|c| configs << c.to_hash} |
284 if configs.empty? |
285 halt 404 |
286 else |
287 configs.to_json |
288 end |
289 end |
290 |
291 put '/:appname/:element?' do |appname, element| |
292 app = Application.find_or_create(:name => appname) |
293 config = Configuration.find_or_create(:name => element, :application_id => app.id) |
294 required_params = ["format", "body"] |
295 data = JSON.parse(request.body.read) |
296 data.keys.sort == required_params.sort ? (config.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters") |
297 if config.valid? |
298 config.save |
299 action = config.is_new? ? "create" : "update" |
300 dependency_action = app.is_new? ? "created" : "updated" |
301 r = {"result" => "success","id" => "#{config.id}", "action" => action, "dependencies" => dependency_action, "application" => app.name, "item" => config.name} |
302 r.to_json |
303 else |
304 raise "#{config.errors}" |
305 end |
306 end |
307 |
308 delete '/:appname/:element?' do |appname, element| |
309 app = Application.find(:name => appname).first |
310 if app |
311 config = Configuration.find(:name=> element, :application_id => app.id).first |
312 if config |
313 config.delete |
314 r = {"result" => "success", "id" => "#{config.id}", "action" => "delete", "application" => "#{app.name}", "item" => "#{element}"} |
315 r.to_json |
316 else |
317 halt 404 |
318 end |
319 else |
320 halt 404 |
321 end |
322 end |
323 |
324 end |
325 run! if app_file == $0 |
326 end |
Generated on 2011-01-31 05:03:32 -0500 with rcov 0.9.8