seam 1.1.3 → 2.0.0a2

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.
Files changed (92) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +4 -2
  3. data/Gemfile.lock +137 -0
  4. data/LICENSE.txt +16 -18
  5. data/README.md +118 -115
  6. data/Rakefile +23 -0
  7. data/lib/seam/client.rb +129 -0
  8. data/lib/seam/clients/access_codes.rb +95 -0
  9. data/lib/seam/clients/access_codes_simulate.rb +17 -0
  10. data/lib/seam/clients/access_codes_unmanaged.rb +57 -0
  11. data/lib/seam/clients/acs.rb +35 -0
  12. data/lib/seam/clients/acs_access_groups.rb +57 -0
  13. data/lib/seam/clients/acs_credential_pools.rb +17 -0
  14. data/lib/seam/clients/acs_credential_provisioning_automations.rb +17 -0
  15. data/lib/seam/clients/acs_credentials.rb +77 -0
  16. data/lib/seam/clients/acs_entrances.rb +47 -0
  17. data/lib/seam/clients/acs_systems.rb +27 -0
  18. data/lib/seam/clients/acs_users.rb +117 -0
  19. data/lib/seam/clients/action_attempts.rb +30 -0
  20. data/lib/seam/clients/base_client.rb +21 -0
  21. data/lib/seam/clients/client_sessions.rb +77 -0
  22. data/lib/seam/clients/connect_webviews.rb +47 -0
  23. data/lib/seam/clients/connected_accounts.rb +47 -0
  24. data/lib/seam/clients/devices.rb +65 -0
  25. data/lib/seam/clients/devices_simulate.rb +17 -0
  26. data/lib/seam/clients/devices_unmanaged.rb +37 -0
  27. data/lib/seam/clients/events.rb +27 -0
  28. data/lib/seam/clients/locks.rb +53 -0
  29. data/lib/seam/clients/networks.rb +27 -0
  30. data/lib/seam/clients/noise_sensors.rb +15 -0
  31. data/lib/seam/clients/noise_sensors_noise_thresholds.rb +57 -0
  32. data/lib/seam/clients/noise_sensors_simulate.rb +17 -0
  33. data/lib/seam/clients/phones.rb +31 -0
  34. data/lib/seam/clients/phones_simulate.rb +17 -0
  35. data/lib/seam/clients/thermostats.rb +106 -0
  36. data/lib/seam/clients/thermostats_climate_setting_schedules.rb +57 -0
  37. data/lib/seam/clients/user_identities.rb +131 -0
  38. data/lib/seam/clients/user_identities_enrollment_automations.rb +47 -0
  39. data/lib/seam/clients/webhooks.rb +57 -0
  40. data/lib/seam/clients/workspaces.rb +50 -0
  41. data/lib/seam/logger.rb +12 -0
  42. data/lib/seam/lts_version.rb +5 -0
  43. data/lib/seam/request.rb +73 -0
  44. data/lib/seam/resources/access_code.rb +12 -0
  45. data/lib/seam/resources/acs_access_group.rb +9 -0
  46. data/lib/seam/resources/acs_credential.rb +12 -0
  47. data/lib/seam/resources/acs_credential_pool.rb +9 -0
  48. data/lib/seam/resources/acs_credential_provisioning_automation.rb +9 -0
  49. data/lib/seam/resources/acs_entrance.rb +9 -0
  50. data/lib/seam/resources/acs_system.rb +9 -0
  51. data/lib/seam/resources/acs_user.rb +9 -0
  52. data/lib/seam/resources/action_attempt.rb +46 -0
  53. data/lib/seam/resources/base_resource.rb +58 -0
  54. data/lib/seam/resources/client_session.rb +9 -0
  55. data/lib/seam/resources/climate_setting_schedule.rb +11 -0
  56. data/lib/seam/resources/connect_webview.rb +9 -0
  57. data/lib/seam/resources/connected_account.rb +12 -0
  58. data/lib/seam/resources/device.rb +12 -0
  59. data/lib/seam/resources/device_provider.rb +7 -0
  60. data/lib/seam/resources/enrollment_automation.rb +9 -0
  61. data/lib/seam/resources/event.rb +9 -0
  62. data/lib/seam/resources/network.rb +9 -0
  63. data/lib/seam/resources/noise_threshold.rb +7 -0
  64. data/lib/seam/resources/phone.rb +12 -0
  65. data/lib/seam/resources/resource_error.rb +9 -0
  66. data/lib/seam/resources/resource_errors_support.rb +9 -0
  67. data/lib/seam/resources/resource_warning.rb +9 -0
  68. data/lib/seam/resources/resource_warnings_support.rb +9 -0
  69. data/lib/seam/resources/service_health.rb +7 -0
  70. data/lib/seam/resources/unmanaged_access_code.rb +12 -0
  71. data/lib/seam/resources/unmanaged_device.rb +12 -0
  72. data/lib/seam/resources/user_identity.rb +9 -0
  73. data/lib/seam/resources/webhook.rb +7 -0
  74. data/lib/seam/resources/workspace.rb +7 -0
  75. data/lib/seam/version.rb +3 -1
  76. data/lib/seam.rb +71 -13
  77. metadata +147 -82
  78. data/.gitignore +0 -18
  79. data/lib/seam/effort.rb +0 -86
  80. data/lib/seam/flow.rb +0 -53
  81. data/lib/seam/in_memory.rb +0 -13
  82. data/lib/seam/persistence.rb +0 -43
  83. data/lib/seam/step.rb +0 -33
  84. data/lib/seam/wait_worker.rb +0 -30
  85. data/lib/seam/worker.rb +0 -166
  86. data/seam.gemspec +0 -30
  87. data/spec/seam/effort_spec.rb +0 -43
  88. data/spec/seam/flow_spec.rb +0 -173
  89. data/spec/seam/step_spec.rb +0 -34
  90. data/spec/seam/wait_worker_spec.rb +0 -179
  91. data/spec/seam/worker_spec.rb +0 -973
  92. data/spec/spec_helper.rb +0 -8
@@ -1,973 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- describe "worker" do
4
-
5
- before do
6
- Seam::Persistence.destroy
7
- @stamp_data_history = true
8
- end
9
-
10
- after do
11
- Timecop.return
12
- end
13
-
14
- describe "step" do
15
- it "should match the name" do
16
- worker = Seam::Worker.new
17
- worker.handles(:darren)
18
- worker.step.must_equal "darren"
19
- end
20
-
21
- it "should drop namespaces" do
22
- worker = ThisIsANamespace::ThisIsAnotherNamespace::IAmAWorker.new
23
- worker.step.must_equal "i_am_a"
24
- end
25
- end
26
-
27
- describe "inherited & all" do
28
-
29
- before do
30
- Seam::Worker.instance_eval { @handlers = nil }
31
- end
32
-
33
- it "should default to an empty set" do
34
- Seam::Worker.all.count.must_equal 0
35
- end
36
-
37
- it "should return new instances of all handlers" do
38
- instance = Object.new
39
- klass = Struct.new(:new).new instance
40
- Seam::Worker.inherited klass
41
- Seam::Worker.all.count.must_equal 1
42
- Seam::Worker.all[0].must_be_same_as instance
43
- end
44
-
45
- end
46
-
47
- describe "handler for" do
48
-
49
- let(:handler_1) { Struct.new(:step).new SecureRandom.uuid }
50
- let(:handler_2) { Struct.new(:step).new SecureRandom.uuid }
51
- let(:handler_3) { Struct.new(:step).new SecureRandom.uuid }
52
-
53
- before do
54
- handler_1_class, handler_2_class, handler_3_class = Object.new, Object.new, Object.new
55
-
56
- handler_1_class = Struct.new(:new).new handler_1
57
- handler_2_class = Struct.new(:new).new handler_2
58
- handler_3_class = Struct.new(:new).new handler_3
59
-
60
- Seam::Worker.instance_eval do
61
- @handlers = [handler_1_class, handler_2_class, handler_3_class]
62
- end
63
- end
64
-
65
- it "should return the handler for the type" do
66
- Seam::Worker.handler_for(handler_2.step).must_be_same_as handler_2
67
- Seam::Worker.handler_for(handler_1.step).must_be_same_as handler_1
68
- Seam::Worker.handler_for(handler_3.step).must_be_same_as handler_3
69
- end
70
-
71
- it "should return nil if none exist" do
72
- Seam::Worker.handler_for('test').nil?.must_equal true
73
- end
74
- end
75
-
76
- describe "move_to_next_step" do
77
-
78
- [:date].to_objects {[
79
- ['1/1/2011'],
80
- ['3/4/2015']
81
- ]}.each do |test|
82
-
83
- describe "move immediately" do
84
-
85
- before { Timecop.freeze Time.parse(test.date) }
86
- after { Timecop.return }
87
-
88
- it "should move to the next step and set the date to now" do
89
- flow = Seam::Flow.new
90
- flow.apple
91
- flow.orange
92
-
93
- effort = flow.start( { first_name: 'John' } )
94
- effort = Seam::Effort.find(effort.id)
95
-
96
- effort.next_step.must_equal "apple"
97
-
98
- apple_worker = Seam::Worker.new
99
- apple_worker.handles(:apple)
100
- def apple_worker.process
101
- move_to_next_step
102
- end
103
-
104
- apple_worker.execute effort
105
-
106
- effort = Seam::Effort.find(effort.id)
107
- effort.next_step.must_equal "orange"
108
- effort.next_execute_at.must_equal Time.parse(test.date)
109
- end
110
-
111
- end
112
-
113
- end
114
-
115
- [:date, :next_date].to_objects {[
116
- ['1/1/2011', '2/1/2011'],
117
- ['1/1/2011', '2/2/2011'],
118
- ['3/4/2015', '4/5/2016']
119
- ]}.each do |test|
120
-
121
- describe "move to some point in the future" do
122
-
123
- before { Timecop.freeze Time.parse(test.date) }
124
- after { Timecop.return }
125
-
126
- it "should move to the next step and set the date to now" do
127
- flow = Seam::Flow.new
128
- flow.apple
129
- flow.orange
130
-
131
- effort = flow.start( { first_name: 'John' } )
132
- effort = Seam::Effort.find(effort.id)
133
-
134
- effort.next_step.must_equal "apple"
135
-
136
- apple_worker = Seam::Worker.new
137
- apple_worker.handles(:apple)
138
- eval("
139
- def apple_worker.process
140
- move_to_next_step( { on: Time.parse('#{test.next_date}') } )
141
- end
142
- ")
143
-
144
- apple_worker.execute effort
145
-
146
- effort = Seam::Effort.find(effort.id)
147
- effort.next_step.must_equal "orange"
148
- effort.next_execute_at.must_equal Time.parse(test.next_date)
149
- end
150
-
151
- end
152
-
153
- end
154
- end
155
-
156
- describe "move_to_next_step as a default" do
157
- it "should go to move_to_next_step by default" do
158
- flow = Seam::Flow.new
159
- flow.apple
160
- flow.orange
161
-
162
- effort = flow.start( { first_name: 'John' } )
163
- effort = Seam::Effort.find(effort.id)
164
-
165
- effort.next_step.must_equal "apple"
166
-
167
- apple_worker = Seam::Worker.new
168
- apple_worker.handles(:apple)
169
- def apple_worker.process
170
- end
171
-
172
- apple_worker.execute effort
173
-
174
- effort = Seam::Effort.find(effort.id)
175
- effort.next_step.must_equal "orange"
176
- end
177
- end
178
-
179
- describe "try_again_in" do
180
-
181
- let(:effort) do
182
- flow = Seam::Flow.new
183
- flow.apple
184
- flow.orange
185
-
186
- e = flow.start( { first_name: 'John' } )
187
- Seam::Effort.find(e.id)
188
- end
189
-
190
- before do
191
- Timecop.freeze Time.parse('3/4/2013')
192
- effort.next_step.must_equal "apple"
193
-
194
- apple_worker = Seam::Worker.new
195
- apple_worker.handles(:apple)
196
- def apple_worker.process
197
- try_again_in 1.day
198
- end
199
-
200
- apple_worker.execute effort
201
- end
202
-
203
- it "should not update the next step" do
204
- fresh_effort = Seam::Effort.find(effort.id)
205
- fresh_effort.next_step.must_equal "apple"
206
- end
207
-
208
- it "should update the next execute date" do
209
- fresh_effort = Seam::Effort.find(effort.id)
210
- fresh_effort.next_execute_at.must_equal Time.parse('4/4/2013')
211
- end
212
- end
213
-
214
- describe "try_again_on" do
215
-
216
- describe "putting it off for one day" do
217
- let(:effort) do
218
- flow = Seam::Flow.new
219
- flow.apple
220
- flow.orange
221
-
222
- e = flow.start( { first_name: 'John' } )
223
- Seam::Effort.find(e.id)
224
- end
225
-
226
- before do
227
- Timecop.freeze Time.parse('3/4/2013')
228
- effort.next_step.must_equal "apple"
229
-
230
- apple_worker = Seam::Worker.new
231
- apple_worker.handles(:apple)
232
- def apple_worker.process
233
- try_again_on Time.parse('4/4/2013')
234
- end
235
-
236
- apple_worker.execute effort
237
- end
238
-
239
- it "should not update the next step" do
240
- fresh_effort = Seam::Effort.find(effort.id)
241
- fresh_effort.next_step.must_equal "apple"
242
- end
243
-
244
- it "should update the next execute date" do
245
- fresh_effort = Seam::Effort.find(effort.id)
246
- fresh_effort.next_execute_at.must_equal Time.parse('4/4/2013')
247
- end
248
-
249
- it "should update the history" do
250
- fresh_effort = Seam::Effort.find(effort.id)
251
- fresh_effort.history[0]['try_again_on'].must_equal Time.parse('4/4/2013')
252
- end
253
- end
254
- end
255
-
256
- describe "more copmlex example" do
257
-
258
- let(:effort1) do
259
- flow = Seam::Flow.new
260
- flow.grape
261
- flow.mango
262
-
263
- e = flow.start( { status: 'Good' } )
264
- Seam::Effort.find(e.id)
265
- end
266
-
267
- let(:effort2) do
268
- flow = Seam::Flow.new
269
- flow.grape
270
- flow.mango
271
-
272
- e = flow.start( { status: 'Bad' } )
273
- Seam::Effort.find(e.id)
274
- end
275
-
276
- before do
277
- Timecop.freeze Time.parse('1/6/2013')
278
-
279
- apple_worker = Seam::Worker.new
280
- apple_worker.handles(:apple)
281
- def apple_worker.process
282
- if @current_effort.data[:status] == 'Good'
283
- move_to_next_step
284
- else
285
- try_again_in 1.day
286
- end
287
- end
288
-
289
- apple_worker.execute effort1
290
- apple_worker.execute effort2
291
- end
292
-
293
- it "should move the first effort forward" do
294
- fresh_effort = Seam::Effort.find(effort1.id)
295
- fresh_effort.next_step.must_equal "mango"
296
- end
297
-
298
- it "should keep the second effort at the same step" do
299
- fresh_effort = Seam::Effort.find(effort2.id)
300
- fresh_effort.next_step.must_equal "grape"
301
- fresh_effort.next_execute_at.must_equal Time.parse('2/6/2013')
302
- end
303
- end
304
-
305
- describe "processing all pending steps for one effort" do
306
- let(:effort1_creator) do
307
- ->() do
308
- flow = Seam::Flow.new
309
- flow.banana
310
- flow.mango
311
-
312
- e = flow.start
313
- Seam::Effort.find(e.id)
314
- end
315
- end
316
-
317
- let(:effort2_creator) do
318
- ->() do
319
- flow = Seam::Flow.new
320
- flow.apple
321
- flow.orange
322
-
323
- e = flow.start
324
- Seam::Effort.find(e.id)
325
- end
326
- end
327
-
328
- let(:apple_worker) do
329
- apple_worker = Seam::Worker.new
330
- apple_worker.handles(:apple)
331
-
332
- apple_worker.class_eval do
333
- attr_accessor :count
334
- end
335
-
336
- def apple_worker.process
337
- self.count += 1
338
- end
339
-
340
- apple_worker.count = 0
341
- apple_worker
342
- end
343
-
344
- before do
345
- Timecop.freeze Time.parse('1/6/2013')
346
-
347
- effort1_creator.call
348
- effort1_creator.call
349
- effort1_creator.call
350
- effort2_creator.call
351
- effort2_creator.call
352
-
353
- apple_worker.execute_all
354
- end
355
-
356
- it "should call the apple worker for the record in question" do
357
- apple_worker.count.must_equal 2
358
- end
359
- end
360
-
361
- describe "a more realistic example" do
362
-
363
- let(:flow) do
364
- flow = Seam::Flow.new
365
- flow.wait_for_attempting_contact_stage
366
- flow.determine_if_postcard_should_be_sent
367
- flow.send_postcard_if_necessary
368
- flow
369
- end
370
-
371
- let(:effort_creator) do
372
- ->() do
373
- e = flow.start
374
- flow.stamp_data_history = @stamp_data_history
375
- Seam::Effort.find(e.id)
376
- end
377
- end
378
-
379
- let(:wait_for_attempting_contact_stage_worker) do
380
- worker = Seam::Worker.new
381
- worker.handles(:wait_for_attempting_contact_stage)
382
-
383
- def worker.process
384
- @current_effort.data['hit 1'] ||= 0
385
- @current_effort.data['hit 1'] += 1
386
- move_to_next_step
387
- end
388
-
389
- worker
390
- end
391
-
392
- let(:determine_if_postcard_should_be_sent_worker) do
393
- worker = Seam::Worker.new
394
- worker.handles(:determine_if_postcard_should_be_sent)
395
-
396
- def worker.process
397
- @current_effort.data['hit 2'] ||= 0
398
- @current_effort.data['hit 2'] += 1
399
- move_to_next_step
400
- end
401
-
402
- worker
403
- end
404
-
405
- let(:send_postcard_if_necessary_worker) do
406
- worker = Seam::Worker.new
407
- worker.handles(:send_postcard_if_necessary)
408
-
409
- def worker.process
410
- @current_effort.data['hit 3'] ||= 0
411
- @current_effort.data['hit 3'] += 1
412
- move_to_next_step
413
- end
414
-
415
- worker
416
- end
417
-
418
- before do
419
- Timecop.freeze Time.parse('1/6/2013')
420
- end
421
-
422
- it "should progress through the story" do
423
-
424
- # SETUP
425
- effort = effort_creator.call
426
- effort.next_step.must_equal "wait_for_attempting_contact_stage"
427
-
428
- # FIRST WAVE
429
- send_postcard_if_necessary_worker.execute_all
430
- determine_if_postcard_should_be_sent_worker.execute_all
431
- wait_for_attempting_contact_stage_worker.execute_all
432
-
433
- effort = Seam::Effort.find effort.id
434
- effort.next_step.must_equal "determine_if_postcard_should_be_sent"
435
-
436
- effort.complete?.must_equal false
437
-
438
- # SECOND WAVE
439
- send_postcard_if_necessary_worker.execute_all
440
- determine_if_postcard_should_be_sent_worker.execute_all
441
- wait_for_attempting_contact_stage_worker.execute_all
442
-
443
- effort = Seam::Effort.find effort.id
444
- effort.next_step.must_equal "send_postcard_if_necessary"
445
-
446
- # THIRD WAVE
447
- send_postcard_if_necessary_worker.execute_all
448
- determine_if_postcard_should_be_sent_worker.execute_all
449
- wait_for_attempting_contact_stage_worker.execute_all
450
-
451
- effort = Seam::Effort.find effort.id
452
- effort.next_step.must_equal nil
453
-
454
- effort.data['hit 1'].must_equal 1
455
- effort.data['hit 2'].must_equal 1
456
- effort.data['hit 3'].must_equal 1
457
-
458
- effort.complete?.must_equal true
459
- effort.completed_at.must_equal Time.now
460
-
461
- # FUTURE WAVES
462
- send_postcard_if_necessary_worker.execute_all
463
- determine_if_postcard_should_be_sent_worker.execute_all
464
- wait_for_attempting_contact_stage_worker.execute_all
465
- send_postcard_if_necessary_worker.execute_all
466
- determine_if_postcard_should_be_sent_worker.execute_all
467
- wait_for_attempting_contact_stage_worker.execute_all
468
- send_postcard_if_necessary_worker.execute_all
469
- determine_if_postcard_should_be_sent_worker.execute_all
470
- wait_for_attempting_contact_stage_worker.execute_all
471
-
472
- effort = Seam::Effort.find effort.id
473
- effort.next_step.must_equal nil
474
-
475
- effort.data['hit 1'].must_equal 1
476
- effort.data['hit 2'].must_equal 1
477
- effort.data['hit 3'].must_equal 1
478
-
479
- end
480
- end
481
-
482
- describe "a more realistic example with waiting" do
483
-
484
- let(:flow) do
485
- flow = Seam::Flow.new
486
- flow.wait_for_attempting_contact_stage
487
- flow.determine_if_postcard_should_be_sent
488
- flow.send_postcard_if_necessary
489
- flow
490
- end
491
-
492
- let(:effort_creator) do
493
- ->() do
494
- e = flow.start
495
- Seam::Effort.find(e.id)
496
- end
497
- end
498
-
499
- let(:wait_for_attempting_contact_stage_worker) do
500
- worker = Seam::Worker.new
501
- worker.handles(:wait_for_attempting_contact_stage)
502
-
503
- def worker.process
504
- @current_effort.data['hit 1'] ||= 0
505
- @current_effort.data['hit 1'] += 1
506
- if Time.now >= Time.parse('28/12/2013')
507
- move_to_next_step
508
- else
509
- try_again_in 1.day
510
- end
511
- end
512
-
513
- worker
514
- end
515
-
516
- let(:determine_if_postcard_should_be_sent_worker) do
517
- worker = Seam::Worker.new
518
- worker.handles(:determine_if_postcard_should_be_sent)
519
-
520
- def worker.process
521
- @current_effort.data['hit 2'] ||= 0
522
- @current_effort.data['hit 2'] += 1
523
- move_to_next_step
524
- end
525
-
526
- worker
527
- end
528
-
529
- let(:send_postcard_if_necessary_worker) do
530
- worker = Seam::Worker.new
531
- worker.handles(:send_postcard_if_necessary)
532
-
533
- def worker.process
534
- @current_effort.data['hit 3'] ||= 0
535
- @current_effort.data['hit 3'] += 1
536
- move_to_next_step
537
- end
538
-
539
- worker
540
- end
541
-
542
- before do
543
- Timecop.freeze Time.parse('25/12/2013')
544
- end
545
-
546
- it "should progress through the story" do
547
-
548
- # SETUP
549
- effort = effort_creator.call
550
- effort.next_step.must_equal "wait_for_attempting_contact_stage"
551
-
552
- # FIRST DAY
553
- send_postcard_if_necessary_worker.execute_all
554
- determine_if_postcard_should_be_sent_worker.execute_all
555
- wait_for_attempting_contact_stage_worker.execute_all
556
-
557
- effort = Seam::Effort.find effort.id
558
- effort.next_step.must_equal "wait_for_attempting_contact_stage"
559
-
560
- send_postcard_if_necessary_worker.execute_all
561
- determine_if_postcard_should_be_sent_worker.execute_all
562
- wait_for_attempting_contact_stage_worker.execute_all
563
- send_postcard_if_necessary_worker.execute_all
564
- determine_if_postcard_should_be_sent_worker.execute_all
565
- wait_for_attempting_contact_stage_worker.execute_all
566
-
567
- effort = Seam::Effort.find effort.id
568
- effort.next_step.must_equal "wait_for_attempting_contact_stage"
569
-
570
- Timecop.freeze Time.parse('29/12/2013')
571
-
572
- send_postcard_if_necessary_worker.execute_all
573
- determine_if_postcard_should_be_sent_worker.execute_all
574
- wait_for_attempting_contact_stage_worker.execute_all
575
-
576
- effort = Seam::Effort.find effort.id
577
- effort.next_step.must_equal "determine_if_postcard_should_be_sent"
578
- effort.data['hit 1'].must_equal 2
579
- end
580
- end
581
-
582
- describe "tracking history" do
583
-
584
- let(:flow) do
585
- flow = Seam::Flow.new
586
- flow.wait_for_attempting_contact_stage
587
- flow.determine_if_postcard_should_be_sent
588
- flow.send_postcard_if_necessary
589
- flow
590
- end
591
-
592
- let(:effort_creator) do
593
- ->(values = {}) do
594
- e = flow.start values
595
- Seam::Effort.find(e.id)
596
- end
597
- end
598
-
599
- let(:wait_for_attempting_contact_stage_worker) do
600
- worker = Seam::Worker.new
601
- worker.handles(:wait_for_attempting_contact_stage)
602
-
603
- def worker.process
604
- @current_effort.data['hit 1'] ||= 0
605
- @current_effort.data['hit 1'] += 1
606
- if Time.now >= Time.parse('28/12/2013')
607
- move_to_next_step
608
- else
609
- try_again_in 1.day
610
- end
611
- end
612
-
613
- worker
614
- end
615
-
616
- let(:determine_if_postcard_should_be_sent_worker) do
617
- worker = Seam::Worker.new
618
- worker.handles(:determine_if_postcard_should_be_sent)
619
-
620
- def worker.process
621
- @current_effort.data['hit 2'] ||= 0
622
- @current_effort.data['hit 2'] += 1
623
- move_to_next_step
624
- end
625
-
626
- worker
627
- end
628
-
629
- let(:send_postcard_if_necessary_worker) do
630
- worker = Seam::Worker.new
631
- worker.handles(:send_postcard_if_necessary)
632
-
633
- def worker.process
634
- @current_effort.data['hit 3'] ||= 0
635
- @current_effort.data['hit 3'] += 1
636
- move_to_next_step
637
- end
638
-
639
- worker
640
- end
641
-
642
- before do
643
- Timecop.freeze Time.parse('26/12/2013')
644
- end
645
-
646
- it "should progress through the story" do
647
-
648
- # SETUP
649
- effort = effort_creator.call({ first_name: 'DARREN' })
650
- effort.next_step.must_equal "wait_for_attempting_contact_stage"
651
-
652
- # FIRST DAY
653
- send_postcard_if_necessary_worker.execute_all
654
- determine_if_postcard_should_be_sent_worker.execute_all
655
- wait_for_attempting_contact_stage_worker.execute_all
656
-
657
- effort = Seam::Effort.find effort.id
658
- effort.next_step.must_equal "wait_for_attempting_contact_stage"
659
-
660
- effort.history.count.must_equal 1
661
- effort.history[0].contrast_with!( {
662
- "step_id" => effort.flow['steps'][0]['id'],
663
- "started_at"=> Time.now,
664
- "step"=>"wait_for_attempting_contact_stage",
665
- "stopped_at" => Time.now,
666
- } )
667
-
668
- send_postcard_if_necessary_worker.execute_all
669
- determine_if_postcard_should_be_sent_worker.execute_all
670
- wait_for_attempting_contact_stage_worker.execute_all
671
- send_postcard_if_necessary_worker.execute_all
672
- determine_if_postcard_should_be_sent_worker.execute_all
673
- wait_for_attempting_contact_stage_worker.execute_all
674
-
675
- effort = Seam::Effort.find effort.id
676
- effort.next_step.must_equal "wait_for_attempting_contact_stage"
677
-
678
- effort.history.count.must_equal 1
679
- effort.history[0].contrast_with!( {
680
- "step_id" => effort.flow['steps'][0]['id'],
681
- "started_at"=> Time.now,
682
- "step"=>"wait_for_attempting_contact_stage",
683
- "stopped_at" => Time.now,
684
- "result" => "try_again_in",
685
- "try_again_on" => Time.now + 1.day
686
- } )
687
-
688
- # THE NEXT DAY
689
- Timecop.freeze Time.parse('27/12/2013')
690
-
691
- send_postcard_if_necessary_worker.execute_all
692
- determine_if_postcard_should_be_sent_worker.execute_all
693
- wait_for_attempting_contact_stage_worker.execute_all
694
-
695
- effort = Seam::Effort.find effort.id
696
- effort.next_step.must_equal "wait_for_attempting_contact_stage"
697
-
698
- effort.history.count.must_equal 2
699
- effort.history[1].contrast_with!( {
700
- "step_id" => effort.flow['steps'][0]['id'],
701
- "started_at"=> Time.now,
702
- "step"=>"wait_for_attempting_contact_stage",
703
- "stopped_at" => Time.now,
704
- "result" => "try_again_in"
705
- } )
706
-
707
- # THE NEXT DAY
708
- Timecop.freeze Time.parse('28/12/2013')
709
-
710
- send_postcard_if_necessary_worker.execute_all
711
- determine_if_postcard_should_be_sent_worker.execute_all
712
- wait_for_attempting_contact_stage_worker.execute_all
713
-
714
- effort = Seam::Effort.find effort.id
715
- effort.next_step.must_equal "determine_if_postcard_should_be_sent"
716
-
717
- effort.history.count.must_equal 3
718
- effort.history[2].contrast_with!( {
719
- "step_id" => effort.flow['steps'][0]['id'],
720
- "started_at"=> Time.now,
721
- "step"=>"wait_for_attempting_contact_stage",
722
- "stopped_at" => Time.now,
723
- "result" => "move_to_next_step"
724
- } )
725
-
726
- # KEEP GOING
727
- send_postcard_if_necessary_worker.execute_all
728
- determine_if_postcard_should_be_sent_worker.execute_all
729
- wait_for_attempting_contact_stage_worker.execute_all
730
- effort = Seam::Effort.find effort.id
731
- effort.next_step.must_equal "send_postcard_if_necessary"
732
-
733
- effort.history.count.must_equal 4
734
- effort.history[3].contrast_with!( {
735
- "step_id" => effort.flow['steps'][1]['id'],
736
- "started_at"=> Time.now,
737
- "step"=>"determine_if_postcard_should_be_sent",
738
- "stopped_at" => Time.now,
739
- "result" => "move_to_next_step"
740
- } )
741
-
742
- # KEEP GOING
743
- send_postcard_if_necessary_worker.execute_all
744
- determine_if_postcard_should_be_sent_worker.execute_all
745
- wait_for_attempting_contact_stage_worker.execute_all
746
- effort = Seam::Effort.find effort.id
747
- effort.next_step.must_equal nil
748
-
749
- effort.history.count.must_equal 5
750
- effort.history[4].contrast_with!( {
751
- "step_id" => effort.flow['steps'][2]['id'],
752
- "started_at"=> Time.now,
753
- "step"=>"send_postcard_if_necessary",
754
- "stopped_at" => Time.now,
755
- "result" => "move_to_next_step"
756
- } )
757
- end
758
- end
759
-
760
- describe "eject" do
761
-
762
- let(:effort) do
763
- flow = Seam::Flow.new
764
- flow.apple
765
- flow.orange
766
-
767
- e = flow.start( { first_name: 'John' } )
768
- Seam::Effort.find(e.id)
769
- end
770
-
771
- before do
772
- Timecop.freeze Time.parse('5/11/2013')
773
- effort.next_step.must_equal "apple"
774
-
775
- apple_worker = Seam::Worker.new
776
- apple_worker.handles(:apple)
777
- def apple_worker.process
778
- eject
779
- end
780
-
781
- apple_worker.execute effort
782
- end
783
-
784
- it "should mark the step as completed" do
785
- fresh_effort = Seam::Effort.find(effort.id)
786
- fresh_effort.complete?.must_equal true
787
- end
788
-
789
- it "should mark the next step to nil" do
790
- fresh_effort = Seam::Effort.find(effort.id)
791
- fresh_effort.next_step.nil?.must_equal true
792
- end
793
-
794
- it "should mark the completed_at date" do
795
- fresh_effort = Seam::Effort.find(effort.id)
796
- fresh_effort.completed_at.must_equal Time.now
797
- end
798
-
799
- it "should mark the history" do
800
- effort.history[0].contrast_with!({"step"=>"apple", "result" => "eject" } )
801
- end
802
-
803
- end
804
-
805
- describe "use the name of the worker to tie to a step" do
806
-
807
- let(:effort) do
808
- flow = Seam::Flow.new
809
- flow.i_will_not_call_handles
810
-
811
- e = flow.start
812
- Seam::Effort.find(e.id)
813
- end
814
-
815
- before do
816
- effort
817
- worker = IWillNotCallHandlesWorker.new
818
- IWillNotCallHandlesWorker.new.execute_all
819
- end
820
-
821
- it "should complete the effort" do
822
- fresh_effort = Seam::Effort.find effort.id
823
- fresh_effort.complete?.must_equal true
824
- end
825
-
826
- end
827
-
828
- describe "making the current step available" do
829
- it "should return the first step if on the first step" do
830
- flow = Seam::Flow.new
831
- flow.apple("test")
832
- flow.orange
833
-
834
- effort = flow.start( { first_name: 'John' } )
835
- effort = Seam::Effort.find(effort.id)
836
-
837
- effort.next_step.must_equal "apple"
838
-
839
- apple_worker = Seam::Worker.new
840
- apple_worker.handles(:apple)
841
- def apple_worker.process
842
- current_step.nil?.must_equal false
843
- current_step["name"].must_equal "apple"
844
- current_step["arguments"].must_equal ["test"]
845
- end
846
-
847
- apple_worker.execute effort
848
- end
849
-
850
- it "should return the second step if on the second step" do
851
- flow = Seam::Flow.new
852
- flow.apple("test")
853
- flow.orange("another test")
854
-
855
- effort = flow.start( { first_name: 'John' } )
856
- effort = Seam::Effort.find(effort.id)
857
-
858
- effort.next_step.must_equal "apple"
859
-
860
- apple_worker = Seam::Worker.new
861
- apple_worker.handles(:apple)
862
- def apple_worker.process
863
- current_step.nil?.must_equal false
864
- current_step["name"].must_equal "apple"
865
- current_step["arguments"].must_equal ["test"]
866
- end
867
-
868
- orange_worker = Seam::Worker.new
869
- orange_worker.handles(:orange)
870
- def orange_worker.process
871
- current_step.nil?.must_equal false
872
- current_step["name"].must_equal "orange"
873
- current_step["arguments"].must_equal ["another test"]
874
- end
875
-
876
- apple_worker.execute_all
877
- orange_worker.execute_all
878
- end
879
- end
880
-
881
- describe "data history" do
882
- describe "stamping the history" do
883
- let(:effort) do
884
- flow = Seam::Flow.new
885
- flow.stamp_data_history = true
886
- flow.apple
887
-
888
- e = flow.start( { first_name: 'John' } )
889
- Seam::Effort.find(e.id)
890
- end
891
-
892
- before do
893
- Timecop.freeze Time.parse('3/4/2013')
894
- effort.next_step.must_equal "apple"
895
-
896
- apple_worker = Seam::Worker.new
897
- apple_worker.handles(:apple)
898
- def apple_worker.process
899
- effort.data['something'] = 'else'
900
- end
901
-
902
- apple_worker.execute effort
903
- end
904
-
905
- it "should not update the next step" do
906
- fresh_effort = Seam::Effort.find(effort.id)
907
- fresh_effort.history.count.must_equal 1
908
- end
909
-
910
- it "should set the data_before history" do
911
- fresh_effort = Seam::Effort.find(effort.id)
912
- fresh_effort.history.first["data_before"].must_equal( { "first_name" => 'John' } )
913
- end
914
-
915
- it "should set the data_after history" do
916
- fresh_effort = Seam::Effort.find(effort.id)
917
- fresh_effort.history.first["data_after"].must_equal( { "first_name" => 'John', "something" => 'else' } )
918
- end
919
- end
920
-
921
- describe "not stamping the history" do
922
- let(:effort) do
923
- flow = Seam::Flow.new
924
- flow.stamp_data_history = false
925
- flow.apple
926
-
927
- e = flow.start( { first_name: 'John' } )
928
- Seam::Effort.find(e.id)
929
- end
930
-
931
- before do
932
- Timecop.freeze Time.parse('3/4/2013')
933
-
934
- apple_worker = Seam::Worker.new
935
- apple_worker.handles(:apple)
936
- def apple_worker.process
937
- effort.data['something'] = 'else'
938
- end
939
-
940
- apple_worker.execute effort
941
- end
942
-
943
- it "should not update the next step" do
944
- fresh_effort = Seam::Effort.find(effort.id)
945
- fresh_effort.history.count.must_equal 1
946
- end
947
-
948
- it "should set the data_before history" do
949
- fresh_effort = Seam::Effort.find(effort.id)
950
- fresh_effort.history.first["data_before"].nil?.must_equal true
951
- end
952
-
953
- it "should set the data_after history" do
954
- fresh_effort = Seam::Effort.find(effort.id)
955
- fresh_effort.history.first["data_after"].nil?.must_equal true
956
- end
957
- end
958
- end
959
- end
960
-
961
- class IWillNotCallHandlesWorker < Seam::Worker
962
- # no calling handles here
963
- def process; end
964
- end
965
-
966
- module ThisIsANamespace
967
- module ThisIsAnotherNamespace
968
- class IAmAWorker < Seam::Worker
969
- def process; end
970
- end
971
- end
972
- end
973
-