lib/lita/handlers/locker_resources.rb in lita-locker-0.7.0 vs lib/lita/handlers/locker_resources.rb in lita-locker-1.0.0

- old
+ new

@@ -15,21 +15,21 @@ command: true, help: { t('help.resource.list.syntax') => t('help.resource.list.desc') } ) route( - /^locker\sresource\screate\s#{RESOURCE_REGEX}$/, + /^locker\sresource\screate\s#{RESOURCES_REGEX}$/, :create, command: true, restrict_to: [:locker_admins], help: { t('help.resource.create.syntax') => t('help.resource.create.desc') } ) route( - /^locker\sresource\sdelete\s#{RESOURCE_REGEX}$/, + /^locker\sresource\sdelete\s#{RESOURCES_REGEX}$/, :delete, command: true, restrict_to: [:locker_admins], help: { t('help.resource.delete.syntax') => t('help.resource.delete.desc') @@ -42,39 +42,69 @@ command: true, help: { t('help.resource.show.syntax') => t('help.resource.show.desc') } ) def list(response) - output = '' - resources.each do |r| - r_name = r.sub('resource_', '') - res = resource(r_name) - output += t('resource.desc', name: r_name, state: res['state']) + should_rate_limit = false + + Resource.list.each_slice(10) do |slice| + if should_rate_limit + sleep 1 + else + should_rate_limit = true + end + + slice.each do |r| + res = Resource.new(r) + response.reply(t('resource.desc', name: r, state: res.state.value)) + end end - response.reply(output) end def create(response) - name = response.matches[0][0] - if create_resource(name) - response.reply(t('resource.created', name: name)) - else - response.reply(t('resource.exists', name: name)) + names = response.match_data['resources'].split(/,\s*/) + results = [] + + names.each do |name| + if Resource.exists?(name) + results <<= t('resource.exists', name: name) + else + Resource.create(name) + results <<= t('resource.created', name: name) + end end + + response.reply(results.join(', ')) end def delete(response) - name = response.matches[0][0] - return response.reply(t('resource.does_not_exist', name: name)) unless resource_exists?(name) - delete_resource(name) - response.reply(t('resource.deleted', name: name)) + names = response.match_data['resources'].split(/,\s*/) + results = [] + + names.each do |name| + if Resource.exists?(name) + Resource.delete(name) + results <<= t('resource.deleted', name: name) + else + results <<= t('resource.does_not_exist', name: name) + end + end + + response.reply(results.join(', ')) end def show(response) - name = response.matches[0][0] - return response.reply(t('resource.does_not_exist', name: name)) unless resource_exists?(name) - r = resource(name) - response.reply(t('resource.desc', name: name, state: r['state'])) + name = response.match_data['resource'] + return response.reply(t('resource.does_not_exist', name: name)) unless Resource.exists?(name) + r = Resource.new(name) + resp = t('resource.desc', name: name, state: r.state.value) + if r.labels.count > 0 + resp += ', used by: ' + r.labels.each do |label| + resp += Label.new(label).id + end + end + response.reply(resp) end Lita.register_handler(LockerResources) end end