Class: PactBroker::Api::Contracts::WebhookRequestContract

Inherits:
BaseContract
  • Object
show all
Includes:
ValidationHelpers
Defined in:
lib/pact_broker/api/contracts/webhook_request_contract.rb

Instance Method Summary collapse

Methods included from ValidationHelpers

#blank?, #environment_with_name_exists?, #includes_space?, #multiple_lines?, #not_provided?, #pacticipant_with_name_exists?, #provided?, #valid_http_method?, #valid_url?, #valid_version_number?

Methods inherited from BaseContract

call

Methods included from DryValidationErrorsFormatter

#add_error, #add_indexed_error, #format_errors

Methods included from DryValidationMethods

#not_provided?, #provided?, #validate_environment_with_name_exists, #validate_no_spaces_if_present, #validate_not_blank_if_present, #validate_not_multiple_lines, #validate_pacticipant_with_name_exists, #validate_url, #validate_valid_url, #validate_version_number, #validation_message

Instance Method Details

#allowed_webhook_host?(url) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 90

def allowed_webhook_host?(url)
  if host_whitelist.any?
    PactBroker::Webhooks::CheckHostWhitelist.call(parse_uri(url).host, host_whitelist).any?
  else
    true
  end
end

#allowed_webhook_method?(http_method) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 61

def allowed_webhook_method?(http_method)
  PactBroker.configuration.webhook_http_method_whitelist.any? do | allowed_method |
    http_method.downcase == allowed_method.downcase
  end
end

#allowed_webhook_scheme?(url) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 75

def allowed_webhook_scheme?(url)
  scheme = parse_uri(url).scheme
  PactBroker.configuration.webhook_scheme_whitelist.any? do | allowed_scheme |
    scheme.downcase == allowed_scheme.downcase
  end
end

#host_error_messageObject



102
103
104
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 102

def host_error_message
  "host must be in the whitelist #{PactBroker.configuration.webhook_host_whitelist.collect(&:inspect).join(", ")}. See /doc/webhooks#whitelist for more information."
end

#host_whitelistObject



98
99
100
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 98

def host_whitelist
  PactBroker.configuration.webhook_host_whitelist
end

#http_method_error_messageObject



67
68
69
70
71
72
73
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 67

def http_method_error_message
  if PactBroker.configuration.webhook_http_method_whitelist.size == 1
    "must be #{PactBroker.configuration.webhook_http_method_whitelist.first}. See /doc/webhooks#whitelist for more information."
  else
    "must be one of #{PactBroker.configuration.webhook_http_method_whitelist.join(", ")}. See /doc/webhooks#whitelist for more information."
  end
end

#if_still_valid(context) ⇒ Object



117
118
119
120
121
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 117

def if_still_valid(context)
  if !context.rule_error?(context.path.keys)
    yield
  end
end

#parse_uri(uri_string, placeholder = "placeholder") ⇒ Object



86
87
88
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 86

def parse_uri(uri_string, placeholder = "placeholder")
  URI(PactBroker::Webhooks::Render.render_with_placeholder(uri_string, placeholder))
end

#scheme_error_messageObject



82
83
84
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 82

def scheme_error_message
  "scheme must be #{PactBroker.configuration.webhook_scheme_whitelist.join(", ")}. See /doc/webhooks#whitelist for more information."
end

#templated_host?(url) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 113

def templated_host?(url)
  parse_uri(url).host != parse_uri(url, "differentplaceholder").host
end

#valid_webhook_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/pact_broker/api/contracts/webhook_request_contract.rb', line 106

def valid_webhook_url?(url)
  uri = parse_uri(url)
  uri.scheme && uri.host
rescue URI::InvalidURIError, ArgumentError
  nil
end