custom_payload.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module TriggerWebhookJob::CustomPayload
  3. # This module generates the final custom payload by replacing the
  4. # replacement variables with the actual values.
  5. extend TriggerWebhookJob::CustomPayload::Parser
  6. extend TriggerWebhookJob::CustomPayload::Validator
  7. # The API provides an endpoint GET /api/v1/webhook/replacements returning a
  8. # list of all available replacement variables lined by this method.
  9. def self.replacements(pre_defined_webhook_type:)
  10. hash = {}
  11. tracks.select(&:root?).each do |track|
  12. hash.merge!(track.replacements(pre_defined_webhook_type: pre_defined_webhook_type))
  13. end
  14. hash
  15. end
  16. # This method is called by the webhook job to generate the custom payload.
  17. def self.generate(record, tracks)
  18. return {} if record.blank?
  19. variables = scan(record)
  20. return JSON.parse(record) if variables.blank?
  21. tracks.transform_keys!(&:to_sym)
  22. mappings = parse(variables, tracks)
  23. # NeverShouldHappen(TM)
  24. return JSON.parse(record) if mappings.blank?
  25. replace(record, mappings)
  26. begin
  27. valid!(record)
  28. rescue => e
  29. return { error: e.message }
  30. end
  31. JSON.parse(record)
  32. end
  33. # The allowed classes and methods are defined within so called track classes,
  34. # see files in app/job/trigger_webhook_job/custom_payload/track.
  35. def self.tracks
  36. TriggerWebhookJob::CustomPayload::Track.descendants
  37. end
  38. end