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, #potential_duplicate_pacticipant_message, #validation_message

Methods included from Logging

included, #log_error, #log_with_tag

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



67
68
69
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 67

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

#body_template_parameters(scope = nil) ⇒ Object



79
80
81
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 79

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

#build(template_params, user_agent) ⇒ Object



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

def build(template_params, user_agent)
  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
  }
  PactBroker::Domain::WebhookRequest.new(attributes)
end

#credentials_template_parameters(scope = nil) ⇒ Object



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

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

#descriptionObject



44
45
46
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 44

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

#display_passwordObject



48
49
50
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 48

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

#header_template_parameters(scope = nil) ⇒ Object



83
84
85
86
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 83

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

#redacted_headersObject



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

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



75
76
77
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 75

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

#to_sObject



71
72
73
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 71

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

#url_template_parameters(scope = nil) ⇒ Object



88
89
90
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 88

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

#uses_parameter?(parameter_name) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 63

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