lita-coffee 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b49345f070b8d118bf1d960e0f07dcd7a56dffc
4
- data.tar.gz: 7ff14d8b53dbeb2cee7200933edcd8e164e96c2e
3
+ metadata.gz: c162431752710f02765329fd0069db1ec4ad97e6
4
+ data.tar.gz: 07c85dc2df9409970da32028f9ead325150cd20f
5
5
  SHA512:
6
- metadata.gz: 48816a6a46e4569e3b64a2e3393c9be8daa0855bcb5db2260b248be329e0ecb215c24f4337ec549caa47537953793b54933e34b3575fd7344ad78ae1779ca113
7
- data.tar.gz: b897fd6aa5f26089d1a0d57636ffe8163f33f9f509f7a2e3282856fc0b92f13a294de8e0b12acbe7aa4c91233a309aebbe617abf8c34ef533cce0f9ee2747dcc
6
+ metadata.gz: 0301877fcaf23733f15f37d2acbecd2cdf73c05d274fd3a569a62fe5b4a2a1f07179674556ae848d8eef1049cc99d50b8e7a59fd45774106e06c81853cba8499
7
+ data.tar.gz: fddb2a37595679ae62937bc91eabf0894079eaae42fbb8ec5f5068982dff8165deb4f00dafe4f1ac62d36667923fdd22d2f311c9385277f5d28479e89f353a79
data/README.md CHANGED
@@ -21,22 +21,8 @@ config.handlers.lita-coffee.default_coffee = "Single origin espresso"
21
21
 
22
22
  ## Usage
23
23
 
24
- ``` ruby
25
- route(
26
- /^\(coffee\)(\s+\-[bcdgist]?|\s+\+)?(.*)/i,
27
- :coffee,
28
- help: {
29
- '(coffee)' => "List the (coffee) orders for your group",
30
- '(coffee) -i' => "Display your (coffee) profile",
31
- '(coffee) -s Colombian Filter' => "Set your (coffee) preference",
32
- '(coffee) -g Cool Kids' => "Change your (coffee) group",
33
- '(coffee) +' => "Order a (coffee)",
34
- '(coffee) -c' => "Cancel your (coffee) order",
35
- '(coffee) -b You owe me one!' => "Buy (coffee) for your group, clear the orders and send a message",
36
- '(coffee) -t' => "Display (coffee) system settings",
37
- '(coffee) -d' => "Delete you from the (coffee) system",
38
- }
39
- )
24
+ ```
25
+ help coffee
40
26
  ```
41
27
 
42
28
  ## License
@@ -11,182 +11,297 @@ module Lita
11
11
  # default_coffee - the coffee we will order if users don't specify what they would like
12
12
  config :default_group, type: String, default: 'coffee-lovers'
13
13
  config :default_coffee, type: String, default: 'Single origin espresso'
14
+ config :default_timeout, type: Integer, default: 28800
14
15
  on :loaded, :set_constants
15
16
 
16
17
  def set_constants(payload)
17
18
  @@DEFAULT_GROUP = config.default_group
18
19
  @@DEFAULT_COFFEE = config.default_coffee
20
+ @@DEFAULT_GROUP_TIMEOUT = config.default_timeout
19
21
  end
20
22
 
23
+ # ---------------
24
+ # Nice new routes
25
+ # ---------------
26
+
27
+ # Welcome new users
28
+ route(
29
+ /coffee/i,
30
+ :init_user,
31
+ )
32
+
33
+ # Order a coffee
34
+ route(
35
+ /^\s*\(?coffee\)?\s+\+\s*(\S.*)?$/i,
36
+ :get_me_a_coffee,
37
+ help: {
38
+ 'coffee +' => "Order a coffee",
39
+ }
40
+ )
41
+
42
+ # Cancel your order
21
43
  route(
22
- /^\(?coffee\)?(\s+\-[bcdgist]?|\s+\+)?(.*)/i,
23
- :coffee,
44
+ /^\s*\(?coffee\)?\s+\-c\s*$/i,
45
+ :cancel_order,
46
+ help: {
47
+ 'coffee -c' => "Cancel your order",
48
+ }
49
+ )
50
+
51
+ # List orders
52
+ route(
53
+ /^\s*\(?coffee\)?\s*$/i,
54
+ :list_orders,
24
55
  help: {
25
56
  'coffee' => "List the orders for your group",
57
+ }
58
+ )
59
+
60
+ # Display profile informatino
61
+ route(
62
+ /^\s*\(?coffee\)?\s+\-i\s*$/i,
63
+ :display_profile,
64
+ help: {
26
65
  'coffee -i' => "Display your profile",
66
+ }
67
+ )
68
+
69
+ # Set preferences
70
+ route(
71
+ /^\s*\(?coffee\)?\s+\-s\s+(.*)$/i,
72
+ :set_prefs,
73
+ help: {
27
74
  'coffee -s Colombian Filter' => "Set your coffee preference",
28
- 'coffee -g Cool Kids' => "Change your group",
29
- 'coffee +' => "Order a coffee",
30
- 'coffee -c' => "Cancel your order",
75
+ }
76
+ )
77
+
78
+ # Set group
79
+ route(
80
+ /^\s*\(?coffee\)?\s+\-g\s+(.*)$/i,
81
+ :set_group,
82
+ help: {
83
+ 'coffee -g Cool Kids' => "Change your group",
84
+ }
85
+ )
86
+
87
+ # Buy coffees
88
+ route(
89
+ /^\s*\(?coffee\)?\s+\-b\s*(.*)$/i,
90
+ :buy_coffees,
91
+ help: {
31
92
  'coffee -b You owe me one!' => "Buy coffee for your group, clear the orders and send a message to each coffee drinker",
93
+ }
94
+ )
95
+
96
+ # Display system settings
97
+ route(
98
+ /^\s*\(?coffee\)?\s+\-t\s*$/i,
99
+ :system_settings,
100
+ help: {
32
101
  'coffee -t' => "Display system settings",
102
+ }
103
+ )
104
+
105
+ # Delete me
106
+ route(
107
+ /^\s*\(?coffee\)?\s+\-d\s*$/i,
108
+ :delete_me,
109
+ help: {
33
110
  'coffee -d' => "Delete you from the coffee system",
34
111
  }
35
112
  )
36
113
 
37
- def coffee(response)
38
- get_settings = response.matches[0][0].strip == "-i" rescue false
39
- set_coffee = response.matches[0][0].strip == "-s" rescue false
40
- change_group = response.matches[0][0].strip == "-g" rescue false
41
- order = response.matches[0][0].strip == "+" rescue false
42
- cancel = response.matches[0][0].strip == "-c" rescue false
43
- buy_coffee = response.matches[0][0].strip == "-b" rescue false
44
- system_settings = response.matches[0][0].strip == "-t" rescue false
45
- delete_me = response.matches[0][0].strip == "-d" rescue false
46
-
47
- preference = response.matches[0][1].strip rescue nil
48
-
49
- my_user = response.user.name
50
- group = get_group(my_user)
51
-
52
- # Setup new users
53
- response.reply("Welcome to coffee! You have been added to the #{@@DEFAULT_GROUP} group with an order of #{@@DEFAULT_COFFEE}. Type help coffee for help.") if initialize_user_redis(my_user) == :new_user
54
-
55
- # Retrieve my preference
56
- if get_settings
57
- settings = get_settings(my_user)
58
- response.reply("Your current coffee is #{settings['coffee']}. You are in the #{settings['group']} group.")
59
- # Set my coffee
60
- elsif set_coffee
61
- result = set_coffee(my_user,preference)
62
- if result == "OK"
63
- response.reply("Coffee set to #{preference}")
64
- else
65
- response.reply("(sadpanda) Failed to set your coffee for some reason: #{result.inspect}")
66
- end
67
- # Delete me altogether
68
- elsif delete_me
69
- result = delete_user(my_user)
70
- if result == 1
71
- response.reply("You have been deleted from coffee")
72
- else
73
- response.reply("(sadpanda) Failed to delete you from coffee for some reason: #{result.inspect}")
74
- end
75
- # Change my coffee group
76
- elsif change_group
77
- result = set_coffee_group(my_user,preference)
78
- if result == "OK"
79
- response.reply("Group set to #{preference}")
80
- else
81
- response.reply("(sadpanda) Failed to set your coffee group for some reason: #{result.inspect}")
82
- end
83
- # Order a coffee
84
- elsif order
85
- result = order_coffee(my_user)
86
- if result == "OK"
87
- response.reply("Ordered you a coffee")
88
- else
89
- response.reply("(sadpanda) Failed to order your coffee for some reason: #{result.inspect}")
90
- end
91
- # Cancel a coffee
92
- elsif cancel
93
- result = cancel_coffee(my_user)
94
- if result == "OK"
95
- response.reply("Cancelled your coffee")
96
- else
97
- response.reply("(sadpanda) Failed to cancel your coffee for some reason: #{result.inspect}")
98
- end
99
- # Buy the coffees and clear the orders
100
- elsif buy_coffee
101
- response.reply("Thanks for ordering the coffee for #{group}!\n--")
102
- get_orders(group).each do |order|
103
- response.reply("#{order}: #{get_coffee(order)}")
104
- send_coffee_message(order,my_user,preference) unless order == my_user
105
- end
106
- result = clear_orders(group)
107
- if result == "OK"
108
- response.reply("Cleared all orders for #{group}")
109
- else
110
- response.reply("(sadpanda) Failed to clear the orders for some reason: #{result.inspect}")
111
- end
112
- # tests
113
- elsif system_settings
114
- response.reply("Default coffee: #{@@DEFAULT_COFFEE}, Default group: #{@@DEFAULT_GROUP}")
115
- # List the orders
114
+ # List all groups
115
+ route(
116
+ /^\s*\(?coffee\)?\s+\-l\s*$/i,
117
+ :list_groups,
118
+ help: {
119
+ 'coffee -l' => "List the available coffee groups",
120
+ }
121
+ )
122
+
123
+ # Coffee stats a.k.a. who owes whom?
124
+ route(
125
+ /^\s*\(?coffee\)?\s+\-w\s*(.*)$/i,
126
+ :show_stats,
127
+ help: {
128
+ 'coffee -w' => "Show stats for a group",
129
+ }
130
+ )
131
+
132
+ # Setup new users
133
+ def init_user(response)
134
+ response.reply("Welcome to coffee! You have been added to the #{@@DEFAULT_GROUP} group with an order of #{@@DEFAULT_COFFEE}. Type help coffee for help.") if initialize_user_redis(response.user.name) == :new_user
135
+ end
136
+
137
+ # Order coffee
138
+ def get_me_a_coffee(response)
139
+ group = response.matches[0][0].strip rescue get_group(response.user.name)
140
+ orders = (get_orders(group) + [response.user.name]).uniq
141
+ result = redis.set("orders:#{group}",orders.to_json)
142
+ set_timeout(group)
143
+ if result == "OK"
144
+ response.reply("Ordered you a coffee from #{group}")
116
145
  else
117
- response.reply("Current orders for #{group}:-\n--")
118
- get_orders(group).each do |order|
119
- response.reply("#{order}: #{get_coffee(order)}")
120
- end
146
+ response.reply("(sadpanda) Failed to order your coffee for some reason: #{result.inspect}")
121
147
  end
148
+ end
122
149
 
150
+ # Cancel coffee order
151
+ def cancel_order(response)
152
+ group = get_group(response.user.name)
153
+ orders = get_orders(group)
154
+ orders.delete(response.user.name)
155
+ result = redis.set("orders:#{group}",orders.to_json)
156
+ set_timeout(group)
157
+ if result == "OK"
158
+ response.reply("Cancelled your coffee")
159
+ else
160
+ response.reply("(sadpanda) Failed to cancel your coffee for some reason: #{result.inspect}")
161
+ end
162
+ end
163
+
164
+ # List the coffee orders for your group
165
+ def list_orders(response)
166
+ group = get_group(response.user.name)
167
+ response.reply("Current orders for #{group}:-\n--")
168
+ get_orders(group).each do |order|
169
+ response.reply("#{order}: #{get_coffee(order)}")
170
+ end
171
+ end
172
+
173
+ # Display profile
174
+ def display_profile(response)
175
+ settings = get_settings(response.user.name)
176
+ response.reply("Your current coffee is #{settings['coffee']}. You are in the #{settings['group']} group.")
177
+ end
178
+
179
+ # Set coffee preference
180
+ # TODO: a single method to update user info
181
+ def set_prefs(response)
182
+ preference = response.matches[0][0].strip rescue nil
183
+ result = update_user_coffee(response.user.name,preference)
184
+ if result == "OK"
185
+ response.reply("Coffee set to #{preference}")
186
+ else
187
+ response.reply("(sadpanda) Failed to set your coffee for some reason: #{result.inspect}")
188
+ end
189
+ end
190
+
191
+ # Set coffee group
192
+ # TODO: merge with coffee preference
193
+ def set_group(response)
194
+ preference = response.matches[0][0].strip rescue nil
195
+ result = set_coffee_group(response.user.name,preference)
196
+ if result == "OK"
197
+ response.reply("Group set to #{preference}")
198
+ else
199
+ response.reply("(sadpanda) Failed to set your coffee group for some reason: #{result.inspect}")
200
+ end
201
+ end
202
+
203
+ # Buy all the coffee for your group
204
+ def buy_coffees(response)
205
+ group = get_group(response.user.name)
206
+ message = response.matches[0][0].strip rescue nil
207
+ response.reply("Thanks for ordering the coffee for #{group}!\n--")
208
+ stats = get_coffee_stats(group)
209
+ get_orders(group).each do |order|
210
+ response.reply("#{order}: #{get_coffee(order)}")
211
+ send_coffee_message(order,response.user.name,message) unless order == response.user.name
212
+ stats[order] -= 1 rescue stats[order] = -1
213
+ stats[response.user.name] += 1 rescue stats[response.user.name] = 1
214
+ end
215
+ set_coffee_stats(group,stats)
216
+ result = clear_orders(group)
217
+ if result == 1
218
+ response.reply("Cleared all orders for #{group}")
219
+ else
220
+ response.reply("(sadpanda) Failed to clear the orders for some reason: #{result.inspect}")
221
+ end
222
+ end
223
+
224
+ # Display the system settings
225
+ def system_settings(response)
226
+ response.reply("Default coffee: #{@@DEFAULT_COFFEE}, Default group: #{@@DEFAULT_GROUP}")
227
+ end
228
+
229
+ # Delete a user
230
+ def delete_me(response)
231
+ result = redis.del("settings:#{response.user.name}")
232
+ if result == 1
233
+ response.reply("You have been deleted from coffee")
234
+ else
235
+ response.reply("(sadpanda) Failed to delete you from coffee for some reason: #{result.inspect}")
236
+ end
237
+ end
238
+
239
+ # List groups
240
+ def list_groups(response)
241
+ groups = redis.keys('stats:*')
242
+ response.reply("The following groups are active:-\n--\n#{groups.map{|g| g.split(':')[1]}.join("\n")}")
243
+ end
244
+
245
+ # Display the stats
246
+ def show_stats(response)
247
+ group = response.matches[0][0].strip rescue get_group(response.user.name)
248
+ stats = get_coffee_stats(group)
249
+ response.reply("Who owes whom?\n--\n#{stats.map{|s| "#{s[0]}: #{s[1]}"}.join("\n")}")
123
250
  end
124
251
 
125
252
  #######
126
253
  private
127
254
  #######
128
255
 
256
+ def get_coffee_stats(group)
257
+ JSON.parse(redis.get("stats:#{group}")) rescue {}
258
+ end
259
+
260
+ def set_coffee_stats(group,stats)
261
+ redis.set("stats:#{group}",stats.to_json)
262
+ end
263
+
129
264
  def initialize_user_redis(user)
130
- if redis.get("settings-#{user}").nil?
131
- redis.set("settings-#{user}",{group: @@DEFAULT_GROUP, coffee: @@DEFAULT_COFFEE}.to_json)
265
+ if redis.get("settings:#{user}").nil?
266
+ redis.set("settings:#{user}",{group: @@DEFAULT_GROUP, coffee: @@DEFAULT_COFFEE}.to_json)
132
267
  return :new_user
133
268
  else
134
269
  return :existing_user
135
270
  end
136
271
  end
137
272
 
138
- def delete_user(user)
139
- redis.del("settings-#{user}")
140
- end
141
-
142
273
  def get_settings(user)
143
- JSON.parse(redis.get("settings-#{user}")) rescue {group: @@DEFAULT_GROUP, coffee: @@DEFAULT_COFFEE}
274
+ JSON.parse(redis.get("settings:#{user}")) rescue {group: @@DEFAULT_GROUP, coffee: @@DEFAULT_COFFEE}
144
275
  end
145
276
 
146
277
  def get_orders(group)
147
- JSON.parse(redis.get("#{group}-orders")) rescue []
278
+ set_timeout(group)
279
+ JSON.parse(redis.get("orders:#{group}")) rescue []
148
280
  end
149
281
 
150
282
  def get_coffee(user)
151
- JSON.parse(redis.get("settings-#{user}"))['coffee'] rescue @@DEFAULT_COFFEE
283
+ JSON.parse(redis.get("settings:#{user}"))['coffee'] rescue @@DEFAULT_COFFEE
152
284
  end
153
285
 
154
286
  def get_group(user)
155
- JSON.parse(redis.get("settings-#{user}"))['group'] rescue @@DEFAULT_GROUP
287
+ JSON.parse(redis.get("settings:#{user}"))['group'] rescue @@DEFAULT_GROUP
156
288
  end
157
289
 
158
- def set_coffee(user,coffee)
290
+ def update_user_coffee(user,coffee)
159
291
  my_settings = get_settings(user)
160
292
  my_settings[:coffee] = coffee
161
- redis.set("settings-#{user}",my_settings.to_json)
293
+ redis.set("settings:#{user}",my_settings.to_json)
162
294
  end
163
295
 
164
296
  def set_coffee_group(user,group)
165
297
  my_settings = get_settings(user)
166
298
  my_settings[:group] = group
167
- redis.set("settings-#{user}",my_settings.to_json)
168
- end
169
-
170
- def order_coffee(user)
171
- group = get_group(user)
172
- Lita.logger.debug("Group: #{group}")
173
- orders = get_orders(group)
174
- Lita.logger.debug("Orders: #{orders}")
175
- orders << user
176
- orders.uniq!
177
- Lita.logger.debug("New orders: #{orders}")
178
- redis.set("#{group}-orders",orders.to_json)
179
- end
180
-
181
- def cancel_coffee(user)
182
- group = get_group(user)
183
- orders = get_orders(group)
184
- orders.delete(user)
185
- redis.set("#{group}-orders",orders.to_json)
299
+ redis.set("settings:#{user}",my_settings.to_json)
186
300
  end
187
301
 
188
302
  def clear_orders(group)
189
- redis.set("#{group}-orders",[])
303
+ set_timeout(group)
304
+ redis.del("orders:#{group}")
190
305
  end
191
306
 
192
307
  def send_coffee_message(user,purchaser,message)
@@ -198,6 +313,10 @@ module Lita
198
313
  Lita.logger.error("Coffee#send_coffee_message raised #{e.class}: #{e.message}\n#{e.backtrace}")
199
314
  end
200
315
 
316
+ def set_timeout(group)
317
+ redis.expire("orders:#{group}",@@DEFAULT_GROUP_TIMEOUT)
318
+ end
319
+
201
320
 
202
321
  end
203
322
 
@@ -0,0 +1,6 @@
1
+ module LitaCoffee
2
+ # lita-coffee version
3
+ VERSION = '0.1.0'
4
+ # lita-github version split amongst different revisions
5
+ MAJOR_VERSION, MINOR_VERSION, REVISION = VERSION.split('.').map(&:to_i)
6
+ end
data/lib/lita-coffee.rb CHANGED
@@ -5,6 +5,7 @@ Lita.load_locales Dir[File.expand_path(
5
5
  )]
6
6
 
7
7
  require_relative "lita/handlers/coffee"
8
+ require 'lita-coffee/version'
8
9
 
9
10
  Lita::Handlers::Coffee.template_root File.expand_path(
10
11
  File.join("..", "..", "templates"),
data/lita-coffee.gemspec CHANGED
@@ -1,6 +1,9 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
2
+ require 'lita-coffee/version'
3
+
1
4
  Gem::Specification.new do |spec|
2
5
  spec.name = 'lita-coffee'
3
- spec.version = '0.0.2'
6
+ spec.version = LitaCoffee::VERSION
4
7
  spec.authors = ['Stuart Auld']
5
8
  spec.email = ['sja@marsupialmusic.net']
6
9
  spec.description = %q{Lita integration to manage office coffee orders}
@@ -14,12 +17,12 @@ Gem::Specification.new do |spec|
14
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
18
  spec.require_paths = ['lib']
16
19
 
17
- spec.add_runtime_dependency 'lita', '>= 4.2'
20
+ spec.add_runtime_dependency 'lita', '~> 4.2'
18
21
  spec.add_runtime_dependency 'json'
19
22
  spec.add_development_dependency 'bundler', '~> 1.3'
20
23
  spec.add_development_dependency 'rake'
21
24
  spec.add_development_dependency 'rack-test'
22
- spec.add_development_dependency 'rspec', '>= 3.0.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
23
26
  spec.add_development_dependency 'simplecov'
24
27
  spec.add_development_dependency 'coveralls'
25
28
  end
@@ -2,8 +2,31 @@ require "spec_helper"
2
2
 
3
3
  describe Lita::Handlers::Coffee, lita_handler: true do
4
4
 
5
- it { is_expected.to route('(coffee)').to(:coffee) }
6
- it { is_expected.to route('coffee').to(:coffee) }
5
+ # Sample office
6
+ SAMPLE_DATA = [
7
+ {name: 'stu', group: 'cool kids', coffee: 'Colombian filter'},
8
+ {name: 'joel', group: 'cool kids', coffee: 'Strong cap w/ 1/2 sugar'},
9
+ {name: 'arun', group: 'cool kids', coffee: 'Warragamba highball'},
10
+ {name: 'danielle', group: 'latte socialists', coffee: 'Latte'},
11
+ {name: 'jack', group: 'latte socialists', coffee: 'Latte'},
12
+ {name: 'john', group: 'latte socialists', coffee: 'Latte'},
13
+ {name: 'alex', group: 'latte socialists', coffee: 'Latte'},
14
+ {name: 'geoff', group: 'traditionalists', coffee: 'Flat white'},
15
+ {name: 'simon', group: 'traditionalists', coffee: 'Skim flat white'},
16
+ ]
17
+
18
+ it { is_expected.to route('coffee').to(:list_orders) }
19
+ it { is_expected.to route('I really like coffee!').to(:init_user)}
20
+ it { is_expected.to route('coffee +').to(:get_me_a_coffee)}
21
+ it { is_expected.to route('coffee + cool dudes').to(:get_me_a_coffee)}
22
+ it { is_expected.to route('coffee -c').to(:cancel_order)}
23
+ it { is_expected.to route('coffee -i').to(:display_profile)}
24
+ it { is_expected.to route('coffee -s Latte').to(:set_prefs)}
25
+ it { is_expected.to route('coffee -g Testers').to(:set_group)}
26
+ it { is_expected.to route('coffee -b This one is on me :)').to(:buy_coffees)}
27
+ it { is_expected.to route('coffee -t').to(:system_settings)}
28
+ it { is_expected.to route('coffee -d').to(:delete_me)}
29
+ it { is_expected.to route('coffee -l').to(:list_groups)}
7
30
 
8
31
  describe '#coffee' do
9
32
  before{robot.trigger(:loaded)}
@@ -18,13 +41,18 @@ describe Lita::Handlers::Coffee, lita_handler: true do
18
41
  expect(replies.last).to start_with("Current orders")
19
42
  end
20
43
 
21
- it 'orders me a coffee if I ask it!' do
44
+ it 'orders you a coffee if you ask it!' do
22
45
  send_message("coffee +")
23
- expect(replies.last).to eq("Ordered you a coffee")
46
+ expect(replies.last).to eq("Ordered you a coffee from coffee-lovers")
24
47
  send_message("coffee")
25
48
  expect(replies.last).to start_with("Test User:")
26
49
  end
27
50
 
51
+ it 'orders me a coffee from a different group too' do
52
+ send_message("coffee + testers")
53
+ expect(replies.last).to eq("Ordered you a coffee from testers")
54
+ end
55
+
28
56
  it 'displays my profile settings' do
29
57
  send_message("coffee -i")
30
58
  expect(replies.last).to start_with("Your current coffee is")
@@ -38,20 +66,18 @@ describe Lita::Handlers::Coffee, lita_handler: true do
38
66
  end
39
67
 
40
68
  it 'changes my coffee group' do
41
- send_message("coffee -g testing team")
69
+ set_prefs('stu',{group: 'testing team'})
42
70
  expect(replies.last).to eq("Group set to testing team")
43
- send_message("coffee -i")
71
+ check_prefs('stu')
44
72
  expect(replies.last).to end_with("You are in the testing team group.")
45
73
  end
46
74
 
47
75
  it 'cancels my order' do
48
- send_message("coffee +")
49
- order_a_coffee('stu')
50
- order_a_coffee('joel')
51
- expect(coffees_in_the_queue).to eq(3)
52
- send_message("coffee -c")
76
+ populate_the_database
77
+ expect(coffees_in_the_queue('cool kids')).to eq(3)
78
+ cancel_order('stu')
53
79
  expect(replies.last).to eq("Cancelled your coffee") #TODO: check that my name has been removed from the queue
54
- expect(coffees_in_the_queue).to eq(2)
80
+ expect(coffees_in_the_queue('cool kids')).to eq(2)
55
81
  end
56
82
 
57
83
  it 'deletes me from the system' do
@@ -65,25 +91,71 @@ describe Lita::Handlers::Coffee, lita_handler: true do
65
91
  end
66
92
 
67
93
  it 'buys the coffees and clears the queue' do
68
- order_a_coffee('stu')
69
- order_a_coffee('joel')
70
- send_message("coffee -b")
94
+ populate_the_database
95
+ buy_coffees_for('cool kids')
71
96
  expect(replies.last).to start_with("Cleared all orders")
72
97
  send_message("coffee")
73
98
  expect(replies.last).to end_with("--")
74
- expect(replies.select{|x| x == "Test User has bought you a coffee!"}.count).to eq(2)
99
+ expect(replies.select{|x| x == "Test User has bought you a coffee!"}.count).to eq(3)
100
+ end
101
+
102
+ it 'lists the available groups' do
103
+ populate_the_database
104
+ send_message("coffee -l")
105
+ expect(replies.last).to start_with("The following groups are active:-")
106
+ end
107
+
108
+ it 'gets some stats' do
109
+ populate_the_database
110
+ buy_coffees_for('cool kids')
111
+ send_message("coffee -w cool kids")
112
+ expect(replies.last).to start_with("Who owes whom?")
113
+ end
114
+
115
+ def set_prefs(name,opts={})
116
+ user = init_user(name)
117
+ send_message("coffee -g #{opts[:group]}", as: user) unless opts[:group].nil?
118
+ send_message("coffee -s #{opts[:coffee]}", as: user) unless opts[:coffee].nil?
119
+ end
120
+
121
+ def check_prefs(name)
122
+ user = init_user(name)
123
+ send_message("coffee -i", as: user)
75
124
  end
76
125
 
77
- def order_a_coffee(user)
78
- user = Lita::User.create(1,name: user)
126
+ def order_a_coffee(name)
127
+ user = init_user(name)
79
128
  send_message('coffee +', as: user)
80
129
  end
81
130
 
82
- def coffees_in_the_queue
83
- send_message('coffee')
131
+ def cancel_order(name)
132
+ user = init_user(name)
133
+ send_message('coffee -c', as: user)
134
+ end
135
+
136
+ def coffees_in_the_queue(group)
137
+ send_message("coffee -g #{group}")
138
+ send_message("coffee")
84
139
  replies.reverse.slice(0,replies.reverse.index{|x| ( x =~ /^Current orders/ ) == 0}).count
85
140
  end
86
141
 
142
+ def populate_the_database
143
+ SAMPLE_DATA.each do |x|
144
+ set_prefs(x[:name],{coffee: x[:coffee],group: x[:group]})
145
+ order_a_coffee(x[:name])
146
+ end
147
+ end
148
+
149
+ def init_user(name)
150
+ Lita::User.create(1,name: name)
151
+ end
152
+
153
+ def buy_coffees_for(group)
154
+ send_message("coffee -g #{group}")
155
+ send_message("coffee -b")
156
+ end
157
+
158
+
87
159
 
88
160
 
89
161
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-coffee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Auld
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-23 00:00:00.000000000 Z
11
+ date: 2015-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.2'
27
27
  - !ruby/object:Gem::Dependency
@@ -84,16 +84,16 @@ dependencies:
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 3.0.0
89
+ version: '3.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 3.0.0
96
+ version: '3.0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: simplecov
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -135,6 +135,7 @@ files:
135
135
  - README.md
136
136
  - Rakefile
137
137
  - lib/lita-coffee.rb
138
+ - lib/lita-coffee/version.rb
138
139
  - lib/lita/handlers/coffee.rb
139
140
  - lita-coffee.gemspec
140
141
  - locales/en.yml