class Lita::Handlers::LockerLabels

Label-related handlers

Public Instance Methods

add(response) click to toggle source
# File lib/lita/handlers/locker_labels.rb, line 132
def add(response)
  results = []
  resource_names = response.match_data['resources'].split(/,\s*/)
  label_name = response.match_data['label']
  return response.reply(failed(t('label.does_not_exist', name: label_name))) unless Label.exists?(label_name)

  resource_names.each do |resource_name|
    if Resource.exists?(resource_name)
      l = Label.new(label_name)
      r = Resource.new(resource_name)
      l.add_resource(r)
      results <<= t('label.resource_added', label: label_name, resource: resource_name)
    else
      results <<= t('resource.does_not_exist', name: resource_name)
    end
  end

  response.reply(results.join(', '))
end
create(response) click to toggle source
# File lib/lita/handlers/locker_labels.rb, line 90
def create(response)
  names = response.match_data['labels'].split(/,\s*/)
  results = []

  names.each do |name|
    results <<= if !Label.exists?(name) && Label.create(name)
                  t('label.created', name: name)
                else
                  t('label.exists', name: name)
                end
  end

  response.reply(results.join(', '))
end
delete(response) click to toggle source
# File lib/lita/handlers/locker_labels.rb, line 105
def delete(response)
  names = response.match_data['labels'].split(/,\s*/)
  results = []

  names.each do |name|
    results <<= if Label.exists?(name) && Label.delete(name)
                  t('label.deleted', name: name)
                else
                  failed(t('label.does_not_exist', name: name))
                end
  end

  response.reply(results.join(', '))
end
list(response) click to toggle source
# File lib/lita/handlers/locker_labels.rb, line 59
def list(response)
  begin
    list = ::Locker::List.new(Label, config.per_page, response.extensions[:kwargs][:page])
  rescue ArgumentError
    return response.reply(t('list.invalid_page_type'))
  end

  return response.reply(t('list.page_outside_range', pages: list.pages)) unless list.valid_page?

  message = list.requested_page.map do |key|
    label = Label.new(key)

    state = label.state.value.to_s

    case state
    when 'unlocked'
      unlocked(t('label.desc', name: key, state: state))
    when 'locked'
      locked(t('label.desc', name: key, state: state))
    else
      # This case shouldn't happen, but it will if a label
      # gets saved with some other value for `state`.
      t('label.desc', name: key, state: state)
    end
  end.join("\n")

  message += "\n#{t('list.paginate', page: list.page, pages: list.pages)}" if list.multiple_pages?

  response.reply(message)
end
remove(response) click to toggle source
# File lib/lita/handlers/locker_labels.rb, line 152
def remove(response)
  results = []
  resource_names = response.match_data['resources'].split(/,\s*/)
  label_name = response.match_data['label']
  return response.reply(failed(t('label.does_not_exist', name: label_name))) unless Label.exists?(label_name)

  resource_names.each do |resource_name|
    if Resource.exists?(resource_name)
      l = Label.new(label_name)
      if l.membership.include?(resource_name)
        r = Resource.new(resource_name)
        l.remove_resource(r)
        results <<= t('label.resource_removed', label: label_name, resource: resource_name)
      else
        results <<= t('label.does_not_have_resource', label: label_name, resource: resource_name)
      end
    else
      results <<= t('resource.does_not_exist', name: resource_name)
    end
  end

  response.reply(results.join(', '))
end
show(response) click to toggle source
# File lib/lita/handlers/locker_labels.rb, line 120
def show(response)
  name = response.match_data['label']
  return response.reply(failed(t('label.does_not_exist', name: name))) unless Label.exists?(name)
  l = Label.new(name)
  return response.reply(t('label.has_no_resources', name: name)) unless l.membership.count > 0
  res = []
  l.membership.each do |member|
    res.push(member)
  end
  response.reply(t('label.resources', name: name, resources: res.join(', ')))
end