2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-06 16:15:18 +00:00
|
|
|
|
2016-03-12 23:36:38 +00:00
|
|
|
class WebhookService
|
|
|
|
def self.publish!(webhook:, event:)
|
|
|
|
return false unless webhook.event_types.include? event.kind
|
|
|
|
HTTParty.post webhook.uri, body: payload_for(webhook, event), headers: webhook.headers
|
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
class << self
|
|
|
|
def payload_for(webhook, event)
|
|
|
|
WebhookSerializer.new(webhook_object_for(webhook, event), root: false).to_json
|
|
|
|
end
|
2016-03-12 23:36:38 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
def webhook_object_for(webhook, event)
|
2016-11-05 16:34:50 +00:00
|
|
|
"Webhooks::#{webhook.kind.classify}::#{event.kind.classify}".constantize.new(webhook, event)
|
2016-07-26 00:14:23 +00:00
|
|
|
end
|
2016-03-12 23:36:38 +00:00
|
|
|
end
|
|
|
|
end
|