-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.rb
108 lines (95 loc) · 2.51 KB
/
task.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# All objects are scoped to this module
module Stepper
require_relative 'mock_endpoints'
# A small unit of work.
class Task
attr_accessor :name,
:input,
:output,
:status,
:result,
:parent,
:process,
:start_time,
:end_time,
:duration
def initialize(
name:,
step:,
input_params_hash:
)
@name = name
@step = step
@input = input_params_hash
puts "Init task: #{@name}"
end
def perform
@start_time = Time.now.getutc
Utils.write_h2 "Performing '#{name}' (task #{@step} of #{@process.tasks.size})"
end
def finish
puts "Finished '#{name}'"
puts ''
@end_time = Time.now.getutc
@duration = @end_time - @start_time
end
end
class CallApiTask < Task
end
# Gets a name from an external API
class GetNameTask < CallApiTask
def perform
super()
url = @input['url']
slug = @input['slug']
name = eval("MockApi.#{slug}")
puts "Getting name from API: '#{url}/#{slug}'...'#{name}'"
# Create output
@output = {
"name": name
}
end
end
# Reads the contents of a file
class GetFileContentsTask < Task
require 'yaml'
def perform
super()
puts "Getting file contents of '#{@input['path']}'"
# Create output
@output = {
'value': YAML.load_file(
Utils.get_abs_path_from_file(
@process.config_file_path,
@input['path']
)
)['value'].to_i
}
end
end
# Simple example task that outputs some text to the console.
class TalkToUserTask < Task
def perform
super()
prepend = @input['prepend']
name = @parent.output['name'.to_sym]
append = @input['append']
puts "#{prepend} #{name}, #{append}."
end
end
# Compares two numeric values arithmetically and geometrically
class CompareValuesTask < Task
def perform
super()
start_val = @process.get_task_by_name(@input['starting_value']).output['value'.to_sym]
end_val = @process.get_task_by_name(@input['ending_value']).output['value'.to_sym]
@output = {
'starting_value': start_val,
'ending_value': end_val,
'diff_arithmetic': end_val - start_val,
'diff_geometric': 100.0 * (end_val.to_f / start_val.to_f - 1.0)
}
Utils.print_hash_as_text_table @output
end
end
end