Skip to content

Commit cae0dad

Browse files
committed
feat: adapt to how form handler reports success and error
1 parent ae0e406 commit cae0dad

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

app/services/subscriptions/pardot_form_handler_submitter.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class PardotFormHandlerSubmitter
66

77
REQUEST_TIMEOUT_SECONDS = 10
88
OPEN_TIMEOUT_SECONDS = 5
9-
SUCCESS_STATUS_CODES = [200, 302].freeze
10-
SUCCESS_LOCATION_PATTERNS = ['/success'].freeze
11-
ERROR_LOCATION_PATTERNS = ['/error'].freeze
9+
SUCCESS_STATUS_CODE = 200
10+
ERROR_BODY_PATTERNS = ['error page'].freeze
11+
SUCCESS_BODY_PATTERNS = ['success page'].freeze
1212

1313
def initialize(endpoint_url:)
1414
@endpoint_url = endpoint_url
@@ -64,10 +64,11 @@ def provider_payload(form_payload)
6464
end
6565

6666
def classify_response(response)
67-
return reject_result unless SUCCESS_STATUS_CODES.include?(response.status)
68-
return Result.new(success?: true) if response.status == 200
69-
return reject_result if redirect_to_error_location?(response)
70-
return Result.new(success?: true) if redirect_to_success_location?(response)
67+
body = response_body(response)
68+
69+
return reject_result if error_body?(body)
70+
return reject_result unless response.status == SUCCESS_STATUS_CODE
71+
return Result.new(success?: true) if success_body?(body)
7172

7273
ambiguous_result
7374
end
@@ -90,27 +91,30 @@ def ambiguous_result
9091
)
9192
end
9293

93-
def redirect_to_success_location?(response)
94-
location = redirect_location(response)
95-
SUCCESS_LOCATION_PATTERNS.any? { |pattern| location.include?(pattern) }
94+
def error_body?(body)
95+
ERROR_BODY_PATTERNS.any? { |pattern| body.include?(pattern) }
96+
end
97+
98+
def success_body?(body)
99+
SUCCESS_BODY_PATTERNS.any? { |pattern| body.include?(pattern) }
96100
end
97101

98-
def redirect_to_error_location?(response)
99-
location = redirect_location(response)
100-
ERROR_LOCATION_PATTERNS.any? { |pattern| location.include?(pattern) }
102+
def response_body(response)
103+
response.body.to_s.downcase
101104
end
102105

103106
def redirect_location(response)
104107
response.headers.fetch('location', '').to_s.downcase
105108
end
106109

107110
def classification_for(response)
108-
return 'rejected_status' unless SUCCESS_STATUS_CODES.include?(response.status)
109-
return 'accepted_200' if response.status == 200
110-
return 'rejected_error_redirect' if redirect_to_error_location?(response)
111-
return 'accepted_success_redirect' if redirect_to_success_location?(response)
111+
body = response_body(response)
112+
113+
return 'rejected_error_body' if error_body?(body)
114+
return 'rejected_status' unless response.status == SUCCESS_STATUS_CODE
115+
return 'accepted_success_body' if success_body?(body)
112116

113-
'ambiguous_redirect'
117+
'ambiguous_response'
114118
end
115119
end
116120
end

0 commit comments

Comments
 (0)