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.



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

def initialize attributes = {}
  @method = attributes[:method]
  @url = attributes[:url]
  @username = attributes[:username]
  @password = attributes[:password]
  @headers = attributes[:headers] || {}
  @body = attributes[:body]
  @uuid = attributes[:uuid]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



14
15
16
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 14

def body
  @body
end

#headersObject

Returns the value of attribute headers.



14
15
16
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 14

def headers
  @headers
end

#methodObject Also known as: http_method

Returns the value of attribute method.



14
15
16
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 14

def method
  @method
end

#passwordObject

Returns the value of attribute password.



14
15
16
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 14

def password
  @password
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 14

def url
  @url
end

#usernameObject

Returns the value of attribute username.



14
15
16
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 14

def username
  @username
end

#uuidObject

Returns the value of attribute uuid.



14
15
16
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 14

def uuid
  @uuid
end

Instance Method Details

#build(context) ⇒ Object



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

def build(context)
  attributes = {
    method: http_method,
    url: build_url(context[:pact], context[:verification]),
    headers: headers,
    username: username,
    password: password,
    uuid: uuid,
    body: build_body(context[:pact], context[:verification])
  }
  PactBroker::Domain::WebhookRequest.new(attributes)
end

#build_body(pact, verification) ⇒ Object



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

def build_body(pact, verification)
  PactBroker::Webhooks::Render.call(String === body ? body : body.to_json, pact, verification)
end

#build_url(pact, verification) ⇒ Object



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

def build_url(pact, verification)
  URI(PactBroker::Webhooks::Render.call(url, pact, verification){ | value | CGI::escape(value) if !value.nil? } ).to_s
end

#descriptionObject



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

def description
  "#{http_method.upcase} #{URI(url.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, 'placeholder')).host}"
end

#display_passwordObject



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

def display_password
  password.nil? ? nil : "**********"
end

#redacted_headersObject



59
60
61
62
63
64
# File 'lib/pact_broker/webhooks/webhook_request_template.rb', line 59

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