class ActiveWebhook::Formatting::BaseAdapter

Protected Class Methods

component_name() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 14
def self.component_name
  "formatting"
end

Public Instance Methods

call() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 8
def call
  Hook.new url, headers, body
end

Protected Instance Methods

body() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 26
def body
  encoded_data
end
content_type() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 34
def content_type
  raise NotImplementedError, "#content_type must be implemented."
end
custom_headers() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 47
def custom_headers
  h = signature_headers.merge(
    Time: time.to_s,
    Topic: topic.key,
    'Topic-Version': topic.version,
    'Webhook-Type': type.presence || "event"
  )
  h['Webhook-Id'] = job_id if job_id.present?
  h
end
data() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 58
def data
  context[:data].presence || resource&.as_json || default_data
end
default_data() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 62
def default_data
  result = { data: {} }

  if resource_id || resource_type
    result[:data] = {}
    result[:data][:id] = resource_id if resource_id
    result[:data][:type] = resource_type if resource_type
  end

  result
end
default_headers() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 38
def default_headers
  h = {
    "Content-Type": content_type,
    "User-Agent": component_configuration.user_agent
  }
  h['Origin'] = ActiveWebhook.origin.to_s if ActiveWebhook.origin.present?
  h
end
encoded_data() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 30
def encoded_data
  raise NotImplementedError, "#encoded_data must be implemented."
end
headers() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 22
def headers
  default_headers.stringify_keys.merge(custom_headers.transform_keys { |key| "#{prefix}-#{key}" })
end
prefix() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 92
def prefix
  @prefix ||= begin
    x = ["X"]
    x << component_configuration.custom_header_prefix
    x.compact.join("-")
  end
end
resource() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 82
def resource
  resource_type.constantize.find_by(id: resource_id) if type == "resource" && resource_id && resource_type
rescue StandardError
  nil
end
resource_id() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 74
def resource_id
  context[:resource_id]
end
resource_type() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 78
def resource_type
  context[:resource_type]
end
signature_headers() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 104
def signature_headers
  ActiveWebhook.verification_adapter.call secret: subscription.shared_secret.to_s, data: body.to_s
end
time() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 100
def time
  context[:time] || Time.current
end
topic() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 88
def topic
  subscription.topic
end
url() click to toggle source
# File lib/active_webhook/formatting/base_adapter.rb, line 18
def url
  subscription.callback_url
end