Class: Lita::Handlers::LockerLabels

Inherits:
Handler
  • Object
show all
Includes:
Locker::Label, Locker::Misc, Locker::Regex, Locker::Resource
Defined in:
lib/lita/handlers/locker_labels.rb

Overview

Label-related handlers

Constant Summary

Constants included from Locker::Regex

Locker::Regex::COMMENT_REGEX, Locker::Regex::LABELS_REGEX, Locker::Regex::LABEL_REGEX, Locker::Regex::LOCK_REGEX, Locker::Regex::RESOURCES_REGEX, Locker::Regex::RESOURCE_REGEX, Locker::Regex::UNLOCK_REGEX, Locker::Regex::USER_REGEX

Instance Method Summary collapse

Methods included from Locker::Misc

#failed, #locked, #success, #unlocked, #user_locks

Methods included from Locker::Label

#label_dependencies, #label_ownership

Instance Method Details

#add(response) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lita/handlers/locker_labels.rb', line 115

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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lita/handlers/locker_labels.rb', line 73

def create(response)
  names = response.match_data['labels'].split(/,\s*/)
  results = []

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

  response.reply(results.join(', '))
end

#delete(response) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lita/handlers/locker_labels.rb', line 88

def delete(response)
  names = response.match_data['labels'].split(/,\s*/)
  results = []

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

  response.reply(results.join(', '))
end

#list(response) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lita/handlers/locker_labels.rb', line 54

def list(response)
  after 0 do
    should_rate_limit = false

    Label.list.each_slice(5) do |slice|
      if should_rate_limit
        sleep 3
      else
        should_rate_limit = true
      end

      slice.each do |n|
        l = Label.new(n)
        response.reply(unlocked(t('label.desc', name: n, state: l.state.value)))
      end
    end
  end
end

#remove(response) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/lita/handlers/locker_labels.rb', line 135

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) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/lita/handlers/locker_labels.rb', line 103

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