Class: ConstantContact::WebhooksUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/constantcontact/webhooks/webhooks_util.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_secret = nil) ⇒ Object

Class constructor

Parameters:

  • api_secret (String) (defaults to: nil)
    • the Constant Contact secret key



14
15
16
17
18
19
# 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

Instance Attribute Details

#client_secretObject

Returns the value of attribute client_secret



9
10
11
# File 'lib/constantcontact/webhooks/webhooks_util.rb', line 9

def client_secret
  @client_secret
end

Instance Method Details

#get_billing_change_notification(hmac, data) ⇒ BillingChangeNotification

Get the BillingChangeNotification model object (has url and event_type as properties) Validates and parses the received data into a BillingChangeNotification model

Parameters:

  • hmac (String)

    The value in the x-ctct-hmac-sha256 header.

  • data (String)

    The body message from the POST received from ConstantContact in Webhook callback.

Returns:

  • (BillingChangeNotification)

    object corresponding to data in case of success



27
28
29
30
31
32
33
# 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) ⇒ Object

Validates a Webhook encrypted message

Parameters:

  • hmac (String)

    The value in the x-ctct-hmac-sha256 header.

  • data (String)

    The body message from the POST received from ConstantContact in Webhook callback.

Returns:

  • true in case of success; false if the Webhook is invalid.



40
41
42
# File 'lib/constantcontact/webhooks/webhooks_util.rb', line 40

def is_valid_webhook(hmac, data)
  Webhooks::Helpers::Validator.validate(@client_secret, hmac, data)
end