Class: PactBroker::Models::WebhookRequest

Inherits:
Object
  • Object
show all
Includes:
Logging, PactBroker::Messages
Defined in:
lib/pact_broker/models/webhook_request.rb

Constant Summary

Constants included from Logging

Logging::LOG_DIR, Logging::LOG_FILE_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PactBroker::Messages

#message, #potential_duplicate_pacticipant_message, #validation_message

Methods included from Logging

included, #logger, #logger=

Constructor Details

#initialize(attributes = {}) ⇒ WebhookRequest

Returns a new instance of WebhookRequest.



31
32
33
34
35
36
37
38
# File 'lib/pact_broker/models/webhook_request.rb', line 31

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



25
26
27
# File 'lib/pact_broker/models/webhook_request.rb', line 25

def body
  @body
end

#headersObject

Returns the value of attribute headers.



25
26
27
# File 'lib/pact_broker/models/webhook_request.rb', line 25

def headers
  @headers
end

#methodObject Also known as: http_method

Returns the value of attribute method.



25
26
27
# File 'lib/pact_broker/models/webhook_request.rb', line 25

def method
  @method
end

#passwordObject

Returns the value of attribute password.



25
26
27
# File 'lib/pact_broker/models/webhook_request.rb', line 25

def password
  @password
end

#urlObject

Returns the value of attribute url.



25
26
27
# File 'lib/pact_broker/models/webhook_request.rb', line 25

def url
  @url
end

#usernameObject

Returns the value of attribute username.



25
26
27
# File 'lib/pact_broker/models/webhook_request.rb', line 25

def username
  @username
end

Instance Method Details

#descriptionObject



40
41
42
# File 'lib/pact_broker/models/webhook_request.rb', line 40

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

#display_passwordObject



44
45
46
# File 'lib/pact_broker/models/webhook_request.rb', line 44

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

#executeObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pact_broker/models/webhook_request.rb', line 48

def execute

  begin
    #TODO make it work with https
    req = http_request

    headers.each_pair do | name, value |
      req[name] = value
    end

    req.basic_auth(username, password) if username

    req.body = body

    logger.info "Making webhook request #{to_s}"
    response = Net::HTTP.start(uri.hostname, uri.port) do |http|
      http.request req
    end

    logger.info "Received response status=#{response.code} body=#{response.body}"
    WebhookExecutionResult.new(response)

  rescue StandardError => e
    logger.error "Error executing webhook #{e.class.name} - #{e.message}"
    logger.error e.backtrace.join("\n")
    WebhookExecutionResult.new(nil, e)
  end

end