Class: Webhook
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Webhook
- Extended by:
- FriendlyId
- Includes:
- Authorizable, Encryptable, Parameterizable::ByIdName
- Defined in:
- app/models/webhook.rb
Constant Summary collapse
- EVENT_POSTFIX =
".#{Foreman::Observable::DEFAULT_NAMESPACE}"
- DEFAULT_PAYLOAD_TEMPLATE =
'Webhook Template - Payload Default'
- ALLOWED_HTTP_METHODS =
%w[POST GET PUT DELETE PATCH].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #ca_certs_store ⇒ Object
- #deliver(event_name:, payload:) ⇒ Object
- #event ⇒ Object
- #event=(event) ⇒ Object
- #events=(events) ⇒ Object
- #test(payload: nil) ⇒ Object
Class Method Details
.available_events ⇒ Object
42 43 44 |
# File 'app/models/webhook.rb', line 42 def self.available_events ::Foreman::EventSubscribers.all_observable_events end |
.deliver(event_name:, payload:) ⇒ Object
46 47 48 49 50 |
# File 'app/models/webhook.rb', line 46 def self.deliver(event_name:, payload:) for_event(event_name).includes([:webhook_template]).each do |target| target.deliver(event_name: event_name, payload: payload) if target.enabled? end end |
Instance Method Details
#ca_certs_store ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/webhook.rb', line 92 def ca_certs_store store = OpenSSL::X509::Store.new if ssl_ca_certs.blank? store.set_default_paths return store end ssl_ca_certs.split(/(?=-----BEGIN)/).each do |cert| store.add_cert(OpenSSL::X509::Certificate.new(cert)) end store rescue StandardError => e raise _(format('Failed to build X509 certificate store for HTTPS client, error: %s', e.)) end |
#deliver(event_name:, payload:) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/models/webhook.rb', line 69 def deliver(event_name:, payload:) payload_to_deliver = rendered_payload(event_name, payload) headers_to_deliver = rendered_headers(event_name, payload) url_to_deliver = rendered_targed_url(event_name, payload) ::ForemanWebhooks::DeliverWebhookJob.perform_later( event_name: event_name, payload: payload_to_deliver, headers: headers_to_deliver, url: url_to_deliver, webhook_id: id ) end |
#event ⇒ Object
65 66 67 |
# File 'app/models/webhook.rb', line 65 def event events.first end |
#event=(event) ⇒ Object
61 62 63 |
# File 'app/models/webhook.rb', line 61 def event=(event) self.events = event end |
#events=(events) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'app/models/webhook.rb', line 52 def events=(events) events = Array(events).map do |event| next event if event.end_with?(EVENT_POSTFIX) event.to_s.underscore + EVENT_POSTFIX end super(self.class.available_events & events) end |
#test(payload: nil) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'app/models/webhook.rb', line 82 def test(payload: nil) ForemanWebhooks::WebhookService.new( webhook: self, headers: http_headers, url: target_url, event_name: event, payload: test_payload(payload || '') ).execute end |