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

Methods included from Logging

included, #log_error

Constructor Details

#initialize(attributes = {}) ⇒ WebhookRequestTemplate

Returns a new instance of WebhookRequestTemplate.



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

def initialize attributes = {}
  @method = attributes[:method]
  @url = attributes[:url]
  @username = attributes[:username]
  @password = attributes[:password]
  @headers = Rack::Utils::HeaderHash.new(attributes[:headers] || {})
  @body = attributes[:body]
  @uuid = attributes[:uuid]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

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.



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

def method
  @method
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

#uuidObject

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

Instance Method Details

#build(template_params) ⇒ Object



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

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

#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

#to_sObject



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

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