class ConstantContact::WebhooksUtil

Attributes

client_secret[RW]

Public Class Methods

new(api_secret = nil) click to toggle source

Class constructor @param [String] api_secret - the Constant Contact secret key @return

# File lib/constantcontact/webhooks/webhooks_util.rb, line 14
def initialize(api_secret = nil)
  @client_secret = api_secret || Util::Config.get('auth.api_secret')
  if @client_secret.nil? || @client_secret == ''
    raise ArgumentError.new(Util::Config.get('errors.api_secret_missing'))
  end
end

Public Instance Methods

get_billing_change_notification(hmac, data) click to toggle source

Get the BillingChangeNotification model object (has url and event_type as properties) Validates and parses the received data into a BillingChangeNotification model @param [String] hmac The value in the x-ctct-hmac-sha256 header. @param [String] data The body message from the POST received from ConstantContact in Webhook callback. @return [BillingChangeNotification] object corresponding to data in case of success

# File lib/constantcontact/webhooks/webhooks_util.rb, line 27
def get_billing_change_notification(hmac, data)
  if is_valid_webhook(hmac, data)
    Webhooks::Models::BillingChangeNotification.create(JSON.parse(data))
  else
    raise Exceptions::WebhooksException, Util::Config.get('errors.invalid_webhook')
  end
end
is_valid_webhook(hmac, data) click to toggle source

Validates a Webhook encrypted message @param [String] hmac The value in the x-ctct-hmac-sha256 header. @param [String] data The body message from the POST received from ConstantContact in Webhook callback. @return true in case of success; false if the Webhook is invalid.

# File lib/constantcontact/webhooks/webhooks_util.rb, line 40
def is_valid_webhook(hmac, data)
  Webhooks::Helpers::Validator.validate(@client_secret, hmac, data)
end