lita-wizard 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88dc590dd6d02e8573709bc1036263435ae94e3e
4
- data.tar.gz: 1d67f3027b98a6306684bf240a18a61bc2bbc674
3
+ metadata.gz: ab9acf73075c026c14e907ded9fd64e07c9cdd4d
4
+ data.tar.gz: 134919eb9e5f687814242df8f0bc000b99ea2ca2
5
5
  SHA512:
6
- metadata.gz: 3ebb360390d5e265a5ad3cab2c0e31e56c79360fc46571b1da14138d96f33270e50268e66c2b5cad30a13d9845c0370203e817223e9754dd2d7855cf9923a6fd
7
- data.tar.gz: 12b092bb719569d7a7df2f63c4def931f549e4e41e77561a002053abae2dc3a12d61b471a4b3f6e0b1b9b36c8bf835e061138face5cc2dfb742d789c5aa2476a
6
+ metadata.gz: 640902ef6a1478b404041d1542ed22b62da9368f58883ac8a2a847c90afd2a504b972ca9648a545d3eaaac4710583a14ed701bcecbe5d0b1e7b672f46056d4d7
7
+ data.tar.gz: 3342bc3bc7e9f7d8e60cd67790917a9dad3c7b0a3ec01cb29cf9c63821985dab25bbd7eac0e5a5782674cd550c433a7188ae1379b7c317ae1a54b31ddf4aa624
@@ -1,23 +1,59 @@
1
1
  module Lita
2
2
  module Extensions
3
3
  class Wizard
4
+ attr_reader :message, :route, :robot
5
+
4
6
  def self.call(payload)
5
- message = payload[:message]
6
- route = payload[:route]
7
- robot = payload[:robot]
7
+ new(payload).call
8
+ end
8
9
 
9
- # mark message as processed and return next time
10
- return true if message.extensions[:processed_by_wizard]
10
+ def initialize(payload)
11
+ @message = payload[:message]
12
+ @route = payload[:route]
13
+ @robot = payload[:robot]
14
+ Lita.logger.debug "Initializing Lita::Extensions::Wizard with:\nMessage: #{message.body}\nRoute: #{route.inspect}"
15
+ end
16
+
17
+ def call
18
+ process_message if should_process_message?
19
+ Lita.logger.debug "Handled by wizard: #{!!message.extensions[:handled_by_wizard]}"
20
+ Lita.logger.debug "Dummy route: #{route.extensions[:dummy] == true}"
21
+ ret = compute_return_value
22
+ Lita.logger.debug "Returning #{ret.inspect}"
23
+ ret
24
+ end
25
+
26
+ protected
27
+
28
+ def process_message
29
+ Lita.logger.debug "Processing message"
11
30
  message.extensions[:processed_by_wizard] = true
31
+ message.extensions[:handled_by_wizard] = Lita::Wizard.handle_message(robot, message)
32
+ Lita.logger.debug "Message processed: #{message.extensions.inspect}"
33
+ end
12
34
 
13
- # if private messages and user has a pending wizard handle the message
14
- if message.private_message? && Lita::Wizard.pending_wizard?(message.user.id)
15
- handled = Lita::Wizard.handle_message(robot, message)
16
- return true if handled
17
- end
35
+ def should_process_message?
36
+ !already_processed? && private_message? && user_has_pending_wizard?
37
+ end
38
+
39
+ def already_processed?
40
+ message.extensions[:processed_by_wizard]
41
+ end
42
+
43
+ def private_message?
44
+ message.private_message?
45
+ end
46
+
47
+ def user_has_pending_wizard?
48
+ Lita::Wizard.pending_wizard?(message.user.id)
49
+ end
18
50
 
19
- # return
20
- !(route.extensions[:dummy] == true)
51
+ def compute_return_value
52
+ if message.extensions[:handled_by_wizard]
53
+ route.extensions[:dummy] == true
54
+ else
55
+ !route.extensions[:dummy]
56
+ end
21
57
  end
22
58
 
23
59
  Lita.register_hook(:validate_route, self)
@@ -1,10 +1,12 @@
1
1
  module Lita
2
2
  module Handlers
3
3
  class Wizard < Handler
4
+
4
5
  route(/.*/, nil, dummy: true) do |response|
5
6
  end
6
7
 
7
8
  Lita.register_handler(self)
9
+
8
10
  end
9
11
  end
10
12
  end
@@ -165,9 +165,11 @@ class Lita::Wizard
165
165
  wizard = restore(robot, message)
166
166
  if wizard
167
167
  wizard.handle_message
168
- return true
168
+ true
169
+ else
170
+ cancel_wizard(message.user.id)
171
+ false
169
172
  end
170
- false
171
173
  end
172
174
 
173
175
  def restore(robot, message)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-wizard"
3
- spec.version = "1.0.1"
3
+ spec.version = "1.0.2"
4
4
  spec.authors = ["Cristian Bica"]
5
5
  spec.email = ["cristian.bica@gmail.com"]
6
6
  spec.description = "A lita extension to build wizards"
@@ -17,48 +17,112 @@ describe Lita::Extensions::Wizard, lita: true do
17
17
  route: route
18
18
  }
19
19
  end
20
+ subject { described_class.new(payload) }
20
21
 
21
- it "should stop if message has already been processed" do
22
- message.extensions[:processed_by_wizard] = true
23
- expect(message.extensions).to receive(:[]=).never
24
- described_class.call(payload)
25
- end
22
+ context "deciding if it should process the message" do
23
+ it "shouldn't process the message if it was already processed" do
24
+ allow(subject).to receive(:private_message?).and_return(true)
25
+ allow(subject).to receive(:user_has_pending_wizard?).and_return(true)
26
+ message.extensions[:processed_by_wizard] = true
27
+ expect(subject).to receive(:process_message).never
28
+ subject.call
29
+ end
26
30
 
27
- it "should mark the message as processed" do
28
- expect(message.extensions).to receive(:[]=).with(:processed_by_wizard, true)
29
- described_class.call(payload)
30
- end
31
+ it "shouldn't process the message if it's a public message" do
32
+ allow(subject).to receive(:already_processed?).and_return(false)
33
+ allow(subject).to receive(:user_has_pending_wizard?).and_return(true)
34
+ expect(subject).to receive(:process_message).never
35
+ subject.call
36
+ end
31
37
 
32
- it "should check if the message is private" do
33
- expect(message).to receive(:private_message?)
34
- described_class.call(payload)
35
- end
38
+ it "shouldn't process the message if the user doesn't have a pending wizard" do
39
+ allow(subject).to receive(:already_processed?).and_return(false)
40
+ allow(subject).to receive(:private_message?).and_return(true)
41
+ Lita::Wizard.cancel_wizard("42")
42
+ expect(subject).to receive(:process_message).never
43
+ subject.call
44
+ end
36
45
 
37
- it "shouldn't check if the user has a pending message if the message is public" do
38
- allow(message).to receive(:private_message?).and_return(false)
39
- expect(Lita::Wizard).to receive(:pending_wizard?).never
40
- described_class.call(payload)
41
- end
46
+ it "should process the message only once" do
47
+ allow(subject).to receive(:private_message?).and_return(true)
48
+ allow(subject).to receive(:user_has_pending_wizard?).and_return(true)
49
+ expect(Lita::Wizard).to receive(:handle_message).once
50
+ subject.call
51
+ subject.call
52
+ end
42
53
 
43
- it "should check if the user has a pending message" do
44
- allow(message).to receive(:private_message?).and_return(true)
45
- expect(Lita::Wizard).to receive(:pending_wizard?)
46
- described_class.call(payload)
54
+ it "should process the message if not processed, private message and user has pending wizard" do
55
+ message.extensions.delete :processed_by_wizard
56
+ message.source.private_message!
57
+ allow(Lita::Wizard).to receive(:pending_wizard?).and_return(true)
58
+ expect(subject).to receive(:process_message).once
59
+ subject.call
60
+ end
47
61
  end
48
62
 
49
- it "try to handle the message if private message and has pending wizard" do
50
- allow(message).to receive(:private_message?).and_return(true)
51
- allow(Lita::Wizard).to receive(:pending_wizard?).and_return(true)
52
- expect(Lita::Wizard).to receive(:handle_message)
53
- described_class.call(payload)
54
- end
63
+ context "processing the message" do
64
+ before { allow(subject).to receive(:should_process_message?).and_return(true) }
55
65
 
56
- it "should return false if dummy route matched" do
57
- route.extensions[:dummy] = true
58
- expect(described_class.call(payload)).to be_falsey
66
+ it "should mark the message as processed" do
67
+ allow(Lita::Wizard).to receive(:handle_message)
68
+ subject.call
69
+ expect(message.extensions[:processed_by_wizard]).to be_truthy
70
+ end
71
+
72
+ it "should set on the message the return value from handle_message" do
73
+ allow(Lita::Wizard).to receive(:handle_message).and_return("42")
74
+ subject.call
75
+ expect(message.extensions[:handled_by_wizard]).to eq("42")
76
+ end
59
77
  end
60
78
 
61
- it "should return true if dummy route didn't match" do
62
- expect(described_class.call(payload)).to be_truthy
79
+ context "return value" do
80
+ it "should return true if the message is not processable for a non dummy route" do
81
+ allow(subject).to receive(:should_process_message?).and_return(false)
82
+ expect(subject.call).to be_truthy
83
+ end
84
+
85
+ it "should return false if the message is not processable for a dummy route" do
86
+ allow(subject).to receive(:should_process_message?).and_return(false)
87
+ route.extensions[:dummy] = true
88
+ expect(subject.call).to be_falsey
89
+ end
90
+
91
+ it "should return false if called on a processable and handled message for a non dummy route" do
92
+ message.extensions.delete :processed_by_wizard
93
+ message.source.private_message!
94
+ allow(Lita::Wizard).to receive(:pending_wizard?).and_return(true)
95
+ expect(Lita::Wizard).to receive(:handle_message).once.and_return(true)
96
+ expect(subject.call).to be_falsey
97
+ expect(subject.call).to be_falsey
98
+ end
99
+
100
+ it "should return true if called on a processable and handled message for a dummy route" do
101
+ message.extensions.delete :processed_by_wizard
102
+ message.source.private_message!
103
+ allow(Lita::Wizard).to receive(:pending_wizard?).and_return(true)
104
+ expect(Lita::Wizard).to receive(:handle_message).once.and_return(true)
105
+ route.extensions[:dummy] = true
106
+ expect(subject.call).to be_truthy
107
+ end
108
+
109
+ it "should return true if the message couldn't be handled by the wizard for a non dummy route" do
110
+ message.extensions.delete :processed_by_wizard
111
+ message.source.private_message!
112
+ allow(Lita::Wizard).to receive(:pending_wizard?).and_return(true)
113
+ expect(Lita::Wizard).to receive(:handle_message).once.and_return(false)
114
+ expect(subject.call).to be_truthy
115
+ end
116
+
117
+ it "should return false if the message couldn't be handled by the wizard for a dummy route" do
118
+ message.extensions.delete :processed_by_wizard
119
+ message.source.private_message!
120
+ allow(Lita::Wizard).to receive(:pending_wizard?).and_return(true)
121
+ expect(Lita::Wizard).to receive(:handle_message).once.and_return(false)
122
+ route.extensions[:dummy] = true
123
+ expect(subject.call).to be_falsey
124
+ end
63
125
  end
126
+
127
+
64
128
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-wizard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cristian Bica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-23 00:00:00.000000000 Z
11
+ date: 2016-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita