Class: Lita::Wizard
- Inherits:
-
Object
- Object
- Lita::Wizard
- Defined in:
- lib/lita/wizard.rb
Instance Attribute Summary collapse
-
#current_step_index ⇒ Object
Returns the value of attribute current_step_index.
-
#id ⇒ Object
Returns the value of attribute id.
-
#message ⇒ Object
Returns the value of attribute message.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#robot ⇒ Object
Returns the value of attribute robot.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
-
#values ⇒ Object
Returns the value of attribute values.
Class Method Summary collapse
- .cancel_wizard(user_id) ⇒ Object
- .handle_message(robot, message) ⇒ Object
- .pending_wizard?(user_id) ⇒ Boolean
- .restore(robot, message) ⇒ Object
- .start(robot, message, meta = {}) ⇒ Object
- .step(name, options = {}) ⇒ Object
- .steps ⇒ Object
Instance Method Summary collapse
- #abort_message ⇒ Object
- #abort_wizard ⇒ Object
- #advance ⇒ Object
- #as_json ⇒ Object
- #destroy ⇒ Object
- #final_message ⇒ Object
- #final_step? ⇒ Boolean
- #finish_wizard ⇒ Object
- #first_step? ⇒ Boolean
- #handle_message ⇒ Object
- #initial_message ⇒ Object
-
#initialize(robot, message, data = {}) ⇒ Wizard
constructor
A new instance of Wizard.
- #run_current_step? ⇒ Boolean
- #save ⇒ Object
- #send_message(body) ⇒ Object
- #start_wizard ⇒ Object
- #step ⇒ Object
- #step_index(step_name) ⇒ Object
- #steps ⇒ Object
- #to_json ⇒ Object
- #valid_response? ⇒ Boolean
- #value_for(step_name) ⇒ Object
Constructor Details
#initialize(robot, message, data = {}) ⇒ Wizard
Returns a new instance of Wizard.
7 8 9 10 11 12 13 14 15 |
# File 'lib/lita/wizard.rb', line 7 def initialize(robot, , data = {}) @id = data['id'] || SecureRandom.hex(3) @robot = robot = @user_id = .user.id @current_step_index = (data['current_step_index'] || -1).to_i @values = data['values'] || [] = data['meta'] end |
Instance Attribute Details
#current_step_index ⇒ Object
Returns the value of attribute current_step_index.
5 6 7 |
# File 'lib/lita/wizard.rb', line 5 def current_step_index @current_step_index end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/lita/wizard.rb', line 5 def id @id end |
#message ⇒ Object
Returns the value of attribute message.
5 6 7 |
# File 'lib/lita/wizard.rb', line 5 def end |
#meta ⇒ Object
Returns the value of attribute meta.
5 6 7 |
# File 'lib/lita/wizard.rb', line 5 def end |
#robot ⇒ Object
Returns the value of attribute robot.
5 6 7 |
# File 'lib/lita/wizard.rb', line 5 def robot @robot end |
#user_id ⇒ Object
Returns the value of attribute user_id.
5 6 7 |
# File 'lib/lita/wizard.rb', line 5 def user_id @user_id end |
#values ⇒ Object
Returns the value of attribute values.
5 6 7 |
# File 'lib/lita/wizard.rb', line 5 def values @values end |
Class Method Details
.cancel_wizard(user_id) ⇒ Object
187 188 189 |
# File 'lib/lita/wizard.rb', line 187 def cancel_wizard(user_id) Lita.redis.del "pending-wizard-#{user_id.downcase}" end |
.handle_message(robot, message) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/lita/wizard.rb', line 163 def (robot, ) return false unless pending_wizard?(.user.id) wizard = restore(robot, ) if wizard wizard. true else cancel_wizard(.user.id) false end end |
.pending_wizard?(user_id) ⇒ Boolean
183 184 185 |
# File 'lib/lita/wizard.rb', line 183 def pending_wizard?(user_id) Lita.redis["pending-wizard-#{user_id.downcase}"] end |
.restore(robot, message) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/lita/wizard.rb', line 175 def restore(robot, ) data = MultiJson.load(Lita.redis["pending-wizard-#{message.user.id.downcase}"]) klass = eval(data['class']) klass.new(robot, , data) rescue nil end |
.start(robot, message, meta = {}) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/lita/wizard.rb', line 156 def start(robot, , = {}) return false if pending_wizard?(.user.id) wizard = new(robot, , 'meta' => ) wizard.advance true end |
.step(name, options = {}) ⇒ Object
191 192 193 |
# File 'lib/lita/wizard.rb', line 191 def step(name, = {}) steps << OpenStruct.new(.merge(name: name)) end |
.steps ⇒ Object
195 196 197 |
# File 'lib/lita/wizard.rb', line 195 def steps @steps ||= [] end |
Instance Method Details
#abort_message ⇒ Object
133 134 135 |
# File 'lib/lita/wizard.rb', line 133 def "Aborting. Resume your normal operations" end |
#abort_wizard ⇒ Object
144 145 |
# File 'lib/lita/wizard.rb', line 144 def abort_wizard end |
#advance ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/lita/wizard.rb', line 17 def advance self.current_step_index += 1 save if final_step? finish_wizard destroy elsif run_current_step? if first_step? start_wizard end = step[:label] = "#{message} (Write done when finished)" if step[:multiline] else advance end end |
#as_json ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lita/wizard.rb', line 77 def as_json { 'class' => self.class.name, 'id' => id, 'user_id' => user_id, 'current_step_index' => current_step_index, 'values' => values, 'meta' => } end |
#destroy ⇒ Object
69 70 71 |
# File 'lib/lita/wizard.rb', line 69 def destroy Lita.redis.del "pending-wizard-#{user_id.downcase}" end |
#final_message ⇒ Object
137 138 139 |
# File 'lib/lita/wizard.rb', line 137 def "You're done!" end |
#final_step? ⇒ Boolean
100 101 102 |
# File 'lib/lita/wizard.rb', line 100 def final_step? current_step_index == steps.size end |
#finish_wizard ⇒ Object
147 148 |
# File 'lib/lita/wizard.rb', line 147 def finish_wizard end |
#first_step? ⇒ Boolean
104 105 106 |
# File 'lib/lita/wizard.rb', line 104 def first_step? current_step_index == 0 end |
#handle_message ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lita/wizard.rb', line 37 def if .body == "abort" abort_wizard destroy elsif step.nil? "Some error occured. Aborting." destroy elsif .body == "done" && step[:multiline] save advance elsif valid_response? if step[:multiline] values[current_step_index] ||= "" values[current_step_index] << "\n" values[current_step_index] << .body values[current_step_index].strip! save else values[current_step_index] = .body save advance end else end end |
#initial_message ⇒ Object
128 129 130 131 |
# File 'lib/lita/wizard.rb', line 128 def "Great! I'm going to ask you some questions. During this time I cannot take regular commands. " \ "You can abort at any time by writing abort" end |
#run_current_step? ⇒ Boolean
96 97 98 |
# File 'lib/lita/wizard.rb', line 96 def run_current_step? step[:if].nil? || instance_eval(&step[:if]) end |
#save ⇒ Object
65 66 67 |
# File 'lib/lita/wizard.rb', line 65 def save Lita.redis["pending-wizard-#{user_id.downcase}"] = to_json end |
#send_message(body) ⇒ Object
150 151 152 |
# File 'lib/lita/wizard.rb', line 150 def (body) .reply body end |
#start_wizard ⇒ Object
141 142 |
# File 'lib/lita/wizard.rb', line 141 def start_wizard end |
#step ⇒ Object
88 89 90 |
# File 'lib/lita/wizard.rb', line 88 def step steps[current_step_index] end |
#step_index(step_name) ⇒ Object
112 113 114 |
# File 'lib/lita/wizard.rb', line 112 def step_index(step_name) steps.index { |step| step.name == step_name } end |
#steps ⇒ Object
92 93 94 |
# File 'lib/lita/wizard.rb', line 92 def steps self.class.steps end |
#to_json ⇒ Object
73 74 75 |
# File 'lib/lita/wizard.rb', line 73 def to_json MultiJson.dump(as_json) end |
#valid_response? ⇒ Boolean
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/lita/wizard.rb', line 116 def valid_response? if step[:validate] && !step[:validate].match(.body) = 'Invalid format' false elsif step[:options] && !step[:options].include?(.body) = "Invalid response. Valid options: #{step[:options].join(', ')}" false else true end end |
#value_for(step_name) ⇒ Object
108 109 110 |
# File 'lib/lita/wizard.rb', line 108 def value_for(step_name) values[step_index(step_name)] end |