Class: PactBroker::Webhooks::WebhookRequestTemplate

Inherits:
Object
  • Object
show all
Includes:
Logging, Messages
Defined in:
lib/pact_broker/webhooks/webhook_request_template.rb

Constant Summary collapse

HEADERS_TO_REDACT =
[/authorization/i, /token/i]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Messages

#message, #pluralize, #validation_message, #validation_message_at_index

Methods included from Logging

included, #log_error, #log_with_tag, #measure_info

Constructor Details

#initialize(attributes = {}) ⇒ WebhookRequestTemplate

Returns a new instance of WebhookRequestTemplate.



23
24
25
26
27
28
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 23

def initialize attributes = {}
  attributes.each do | (name, value) |
    instance_variable_set("@#{name}", value) if respond_to?(name)
  end
  @headers = Rack::Utils::HeaderHash.new(attributes[:headers] || {})
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



16
17
18
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 16

def body
  @body
end

#headersObject

Returns the value of attribute headers.



17
18
19
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 17

def headers
  @headers
end

#methodObject Also known as: http_method

Returns the value of attribute method.



16
17
18
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 16

def method
  @method
end

#passwordObject

Returns the value of attribute password.



16
17
18
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 16

def password
  @password
end

#urlObject

Returns the value of attribute url.



16
17
18
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 16

def url
  @url
end

#usernameObject

Returns the value of attribute username.



16
17
18
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 16

def username
  @username
end

#uuidObject

Returns the value of attribute uuid.



16
17
18
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 16

def uuid
  @uuid
end

Instance Method Details

#body_stringObject



69
70
71
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 69

def body_string
  String === body ? body : body&.to_json
end

#body_template_parameters(scope = nil) ⇒ Object



81
82
83
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 81

def body_template_parameters(scope = nil)
  body_string.scan(parameter_pattern(scope)).flatten.uniq
end

#build(template_params, user_agent: nil, disable_ssl_verification: false, cert_store: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 30

def build(template_params, user_agent: nil, disable_ssl_verification: false, cert_store: nil)
  attributes = {
    method: http_method,
    url: build_url(template_params),
    headers: build_headers(template_params),
    username: build_string(username, template_params),
    password: build_string(password, template_params),
    uuid: uuid,
    body: build_body(template_params),
    user_agent: user_agent,
    disable_ssl_verification: disable_ssl_verification,
    cert_store: cert_store
  }
  PactBroker::Domain::WebhookRequest.new(attributes)
end

#credentials_template_parameters(scope = nil) ⇒ Object



94
95
96
97
98
99
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 94

def credentials_template_parameters(scope = nil)
  pattern = parameter_pattern(scope)
  [username, password].compact.collect do | credential |
    credential.scan(pattern)
  end.flatten.uniq
end

#descriptionObject



46
47
48
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 46

def description
  "#{http_method.upcase} #{URI(PactBroker::Webhooks::Render.render_with_placeholder(url)).host}"
end

#display_passwordObject



50
51
52
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 50

def display_password
  password.nil? ? nil : (PactBroker::Webhooks::Render.includes_parameter?(password) ? password : "**********")
end

#header_template_parameters(scope = nil) ⇒ Object



85
86
87
88
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 85

def header_template_parameters(scope = nil)
  pattern = parameter_pattern(scope)
  headers.values.collect { |value| value.scan(pattern) }.flatten.uniq
end

#redacted_headersObject



54
55
56
57
58
59
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 54

def redacted_headers
  headers.each_with_object({}) do | (name, value), new_headers |
    redact = HEADERS_TO_REDACT.any?{ | pattern | name =~ pattern }  && !PactBroker::Webhooks::Render.includes_parameter?(value)
    new_headers[name] = redact ? "**********" : value
  end
end

#template_parameters(scope = nil) ⇒ Object



77
78
79
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 77

def template_parameters(scope = nil)
  body_template_parameters(scope) + url_template_parameters(scope) + header_template_parameters(scope) + credentials_template_parameters(scope)
end

#to_sObject



73
74
75
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 73

def to_s
  "#{method.upcase} #{url}, username=#{username}, password=#{display_password}, headers=#{redacted_headers}, body=#{body}"
end

#url_template_parameters(scope = nil) ⇒ Object



90
91
92
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 90

def url_template_parameters(scope = nil)
  url.scan(parameter_pattern(scope)).flatten.uniq
end

#uses_parameter?(parameter_name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 65

def uses_parameter?(parameter_name)
  !!body_string&.include?("${" + parameter_name + "}")
end