-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpoller.rb
More file actions
117 lines (99 loc) · 3.34 KB
/
poller.rb
File metadata and controls
117 lines (99 loc) · 3.34 KB
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
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true
module Smithy
module Client
module Waiters
class Poller
def initialize(options = {})
@operation_name = options[:operation_name]
@acceptors = options[:acceptors]
end
def call(client, params)
@input = params
begin
resp = client.send(@operation_name, params)
rescue StandardError => e
error = e
end
resp_or_error = resp || error
status = evaluate_acceptors(resp, error)
[resp_or_error, status]
end
private
def evaluate_acceptors(resp, error)
@acceptors.each do |acceptor|
return acceptor['state'] if acceptor_matches?(acceptor['matcher'], resp, error)
end
# If none of the acceptors match and an error was encountered,
# transition to failure state. Otherwise, transition to retry state.
if error
'error'
else
'retry'
end
end
def acceptor_matches?(matcher, resp, error)
matcher_type = matcher.keys[0]
send("matches_#{matcher_type}?", matcher[matcher_type], resp, error)
end
def matches_output?(path_matcher, resp, error)
return false unless error.nil?
actual = JMESPath.search(underscore_jmespath(path_matcher['path']), resp)
is_equal?(actual, path_matcher['expected'], path_matcher['comparator'])
end
def matches_inputOutput?(path_matcher, resp, error)
return false unless error.nil? && @input
data = {
input: @input, ### Where do we get this?
output: resp
}
actual = JMESPath.search(underscore_jmespath(path_matcher['path']), data)
is_equal?(actual, path_matcher['expected'], path_matcher['comparator'])
end
def matches_success?(path_matcher, resp, error)
if path_matcher == true
!resp.nil?
else
!error.nil?
end
end
def matches_errorType?(path_matcher, resp, error)
return false unless resp.nil?
err = path_matcher.split('#').last.split('#').first
error.class.to_s.include?(err)
end
def is_equal?(actual, expected, comparator)
case comparator
when 'stringEquals'
return actual == expected
when 'booleanEquals'
return actual.to_s == expected
when 'allStringEquals'
return false if actual.nil? || actual.empty?
actual.each do |value|
return false if value != expected
end
return true
when 'anyStringEquals'
return false if actual.nil? || actual.empty?
actual.each do |value|
return true if value == expected
end
return false
end
end
def underscore(string)
string.gsub(/::/, '/')
.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
.tr("-", "_")
.downcase
end
def underscore_jmespath(expression)
expression
.gsub(' or ', '||')
.gsub(/(?<![`'])\b\w+\b(?![`'])/) { |str| underscore(str) }
end
end
end
end
end