Sha256: c2afaa0ac10ed78f1d9afcfcd931c67add954a2b02aace7d2f645fa5da81c10b

Contents?: true

Size: 1.66 KB

Versions: 10

Compression:

Stored size: 1.66 KB

Contents

#
# webhooks_util.rb
# ConstantContact
#
# Copyright (c) 2013 Constant Contact. All rights reserved.

module ConstantContact
  class WebhooksUtil
    attr_accessor :client_secret

    # Class constructor
    # @param [String] api_secret - the Constant Contact secret key
    # @return
    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


    # 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
    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


    # 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.
    def is_valid_webhook(hmac, data)
      Webhooks::Helpers::Validator.validate(@client_secret, hmac, data)
    end

  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
constantcontact-4.0.0 lib/constantcontact/webhooks/webhooks_util.rb
constantcontact-3.0.0 lib/constantcontact/webhooks/webhooks_util.rb
constantcontact-2.2.1 lib/constantcontact/webhooks/webhooks_util.rb
constantcontact-ruby-2.2.1 lib/constantcontact/webhooks/webhooks_util.rb
constantcontact-ruby-2.2.0 lib/constantcontact/webhooks/webhooks_util.rb
constantcontact-2.2.0 lib/constantcontact/webhooks/webhooks_util.rb
constantcontact-2.1.0 lib/constantcontact/webhooks/webhooks_util.rb
constantcontact-2.0.1 lib/constantcontact/webhooks/webhooks_util.rb
constantcontact-2.0.0 lib/constantcontact/webhooks/webhooks_util.rb
constantcontact-1.3.2 lib/constantcontact/webhooks/webhooks_util.rb