lib/lita/handlers/claims.rb in lita-claims-0.0.2 vs lib/lita/handlers/claims.rb in lita-claims-0.0.3

- old
+ new

@@ -16,10 +16,37 @@ route(/^unclaim\s(#{PROPERTY_OR_ENVIRONMENT.source})(?:\s(#{PROPERTY_OR_ENVIRONMENT.source}))?/i, :destroy, command: true, help: { "unclaim PROPERTY" => "To remove your claim from a property by the name PROPERTY" }) + route(/^force\sunclaim\s(#{PROPERTY_OR_ENVIRONMENT.source})(?:\s(#{PROPERTY_OR_ENVIRONMENT.source}))?/i, :force_destroy, command: true, help: { + "force unclaim PROPERTY" => "To remove a claim by someone else from a property by the name PROPERTY" + }) + + http.get "/available/:property/:requester(/:environment)", :available + + def available(request, response) + property = request.env['router.params'][:property] + environment = request.env['router.params'][:environment] + environment_string = " (#{environment})" if environment + requester = request.env['router.params'][:requester] + claimer = Claim.read(property, environment) + if Claim.exists?(property, environment) && claimer != requester + # forbidden + response.status = 403 + response.headers["Content-Type"] = "application/json" + response.body = {claimer: claimer} + target = Source.new(room: '#devs') + robot.send_message(target, "#{requester} tried deploying #{property}#{environment_string} but it has been claimed by #{claimer}.") + else + # OK + response.status = 200 + response.headers["Content-Type"] = "application/json" + response.body = {} + end + end + def create(response) claimer = response.message.source.user.name property, environment = response.matches.first environment ||= 'default' environment_string = " (#{environment})" if environment != 'default' @@ -54,11 +81,24 @@ claimer = Claim.read(property, environment) if claimer == unclaimer Claim.destroy(property, environment) reply = "#{property}#{environment_string} is no longer claimed." else - reply = "#{property}#{environment_string} is currently claimed by #{claimer}. Use the --force flag when you are sure to unclaim the property." + reply = "#{property}#{environment_string} is currently claimed by #{claimer}. Use the `force unclaim` command when you are sure to unclaim the property." end + else + reply = "#{property}#{environment_string} has not yet been claimed." + end + response.reply(reply) + end + + def force_destroy(response) + property, environment = response.matches.first + environment ||= 'default' + environment_string = " (#{environment})" if environment != 'default' + if property && Claim.exists?(property, environment) + Claim.destroy(property, environment) + reply = "#{property}#{environment_string} is no longer claimed." else reply = "#{property}#{environment_string} has not yet been claimed." end response.reply(reply) end