Class: PactBroker::Domain::WebhookRequest

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/pact_broker/domain/webhook_request.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #log_error

Constructor Details

#initialize(attributes = {}) ⇒ WebhookRequest

Returns a new instance of WebhookRequest.



28
29
30
31
32
33
34
35
36
# File 'lib/pact_broker/domain/webhook_request.rb', line 28

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.



22
23
24
# File 'lib/pact_broker/domain/webhook_request.rb', line 22

def body
  @body
end

#headersObject

Returns the value of attribute headers.



22
23
24
# File 'lib/pact_broker/domain/webhook_request.rb', line 22

def headers
  @headers
end

#methodObject Also known as: http_method

Returns the value of attribute method.



22
23
24
# File 'lib/pact_broker/domain/webhook_request.rb', line 22

def method
  @method
end

#passwordObject

Returns the value of attribute password.



22
23
24
# File 'lib/pact_broker/domain/webhook_request.rb', line 22

def password
  @password
end

#urlObject

Returns the value of attribute url.



22
23
24
# File 'lib/pact_broker/domain/webhook_request.rb', line 22

def url
  @url
end

#usernameObject

Returns the value of attribute username.



22
23
24
# File 'lib/pact_broker/domain/webhook_request.rb', line 22

def username
  @username
end

#uuidObject

Returns the value of attribute uuid.



22
23
24
# File 'lib/pact_broker/domain/webhook_request.rb', line 22

def uuid
  @uuid
end

Instance Method Details

#descriptionObject



38
39
40
# File 'lib/pact_broker/domain/webhook_request.rb', line 38

def description
  "#{method.upcase} #{URI(url).host}"
end

#display_passwordObject



42
43
44
# File 'lib/pact_broker/domain/webhook_request.rb', line 42

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

#executeObject



53
54
55
56
57
58
59
# File 'lib/pact_broker/domain/webhook_request.rb', line 53

def execute
  options = PactBroker::BuildHttpOptions.call(uri).merge(read_timeout: 15, open_timeout: 15)
  req = http_request
  Net::HTTP.start(uri.hostname, uri.port, :ENV, options) do |http|
    http.request req
  end
end

#http_requestObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pact_broker/domain/webhook_request.rb', line 61

def http_request
  @http_request ||= begin
    req = Net::HTTP.const_get(method.capitalize).new(uri.request_uri)
    req.delete("accept-encoding")
    req["user-agent"] = PactBroker.configuration.user_agent
    headers.each_pair { | name, value | req[name] = value }
    req.basic_auth(username, password) if username && username.size > 0
    req.body = body unless body.nil?
    req
  end
end

#redacted_headersObject



46
47
48
49
50
51
# File 'lib/pact_broker/domain/webhook_request.rb', line 46

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