2
2
require 'pact_broker/webhooks/render'
3
3
require 'cgi'
4
4
require 'pact_broker/domain/webhook_request'
5
+ require 'pact_broker/string_refinements'
5
6
6
7
module PactBroker
7
8
module Webhooks
8
9
class WebhookRequestTemplate
9
10
10
11
include PactBroker ::Logging
11
12
include PactBroker ::Messages
13
+ using PactBroker ::StringRefinements
14
+
12
15
HEADERS_TO_REDACT = [ /authorization/i , /token/i ]
13
16
14
17
attr_accessor :method , :url , :headers , :body , :username , :password , :uuid
@@ -31,24 +34,15 @@ def build(template_params)
31
34
attributes = {
32
35
method : http_method ,
33
36
url : build_url ( template_params ) ,
34
- headers : headers ,
35
- username : username ,
36
- password : password ,
37
+ headers : build_headers ( template_params ) ,
38
+ username : build_string ( username , template_params ) ,
39
+ password : build_string ( password , template_params ) ,
37
40
uuid : uuid ,
38
41
body : build_body ( template_params )
39
42
}
40
43
PactBroker ::Domain ::WebhookRequest . new ( attributes )
41
44
end
42
45
43
- def build_url ( template_params )
44
- URI ( PactBroker ::Webhooks ::Render . call ( url , template_params ) { | value | CGI ::escape ( value ) if !value . nil? } ) . to_s
45
- end
46
-
47
- def build_body ( template_params )
48
- body_string = String === body ? body : body . to_json
49
- PactBroker ::Webhooks ::Render . call ( body_string , template_params )
50
- end
51
-
52
46
def description
53
47
"#{ http_method . upcase } #{ URI ( url . gsub ( PactBroker ::Webhooks ::Render ::TEMPLATE_PARAMETER_REGEXP , 'placeholder' ) ) . host } "
54
48
end
@@ -68,11 +62,32 @@ def headers= headers
68
62
@headers = Rack ::Utils ::HeaderHash . new ( headers )
69
63
end
70
64
71
- private
72
65
73
66
def to_s
74
67
"#{ method . upcase } #{ url } , username=#{ username } , password=#{ display_password } , headers=#{ redacted_headers } , body=#{ body } "
75
68
end
69
+
70
+ private
71
+
72
+ def build_url ( template_params )
73
+ URI ( PactBroker ::Webhooks ::Render . call ( url , template_params ) { | value | CGI ::escape ( value ) if !value . nil? } ) . to_s
74
+ end
75
+
76
+ def build_body ( template_params )
77
+ body_string = String === body ? body : body . to_json
78
+ build_string ( body_string , template_params )
79
+ end
80
+
81
+ def build_headers ( template_params )
82
+ headers . each_with_object ( Rack ::Utils ::HeaderHash . new ) do | ( key , value ) , new_headers |
83
+ new_headers [ key ] = build_string ( value , template_params )
84
+ end
85
+ end
86
+
87
+ def build_string ( string , template_params )
88
+ return string if string . nil? || string . blank?
89
+ PactBroker ::Webhooks ::Render . call ( string , template_params )
90
+ end
76
91
end
77
92
end
78
93
end
0 commit comments