Commit 17cacf42 authored by John E. Vincent's avatar John E. Vincent

tag DRYup

parent 6eda1500
module Noah
module SinatraTagHelpers
#TODO DRY this sumbitch up
def tag(primitive, name, tags)
case primitive
when "services"
servicename, hostname = name.split('/')
obj = host_service(hostname, servicename)
when "hosts"
obj = Noah::Host.find(:name=>name).first
when "configurations"
obj = Noah::Configuration.find(:name=>name).first
when "applications"
obj = Noah::Application.find(:name=>name).first
when "ephemerals"
obj = Noah::Ephemeral.find(:path=>"/#{name}").first
else
halt 404
end
obj.nil? ? (halt 404) : (obj.tag!(tags))
obj.to_json
end
def untag(primitive, name, tags)
case primitive
when "services"
servicename, hostname = name.split('/')
obj = host_service(hostname, servicename)
when "hosts"
obj = Noah::Host.find(:name=>name).first
when "configurations"
obj = Noah::Configuration.find(:name=>name).first
when "applications"
obj = Noah::Application.find(:name=>name).first
when "ephemerals"
obj = Noah::Ephemeral.find(:path=>"/#{name}").first
else
halt 404
end
obj.nil? ? (halt 404) : (obj.untag!(tags))
obj.to_json
end
end
end
class Noah::App class Noah::App
put '/:primitive/*/tag' do |primitive, name|
required_params = ["tags"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (tag(primitive, name, data["tags"])) : (raise "Missing Parameters")
end
delete '/:primitive/*/tag' do |primitive, name|
required_params = ["tags"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (untag(primitive, name, data["tags"])) : (raise "Missing Parameters")
end
get '/tags/:tagname/?' do |tagname| get '/tags/:tagname/?' do |tagname|
tags = Noah::Tags.all(:name => tagname).to_hash tags = Noah::Tags.all(:name => tagname).to_hash
(halt 404) if tags.size == 0 (halt 404) if tags.size == 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment