Sha256: ff76bcb41333583be4e47bee89d97c65ef9ab9c7499f6aeb914f8eac44ffa1c9

Contents?: true

Size: 1.26 KB

Versions: 13

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require_relative 'webhook_event'

# ref: https://developer.intuit.com/app/developer/qbo/docs/develop/webhooks/managing-webhooks-notifications#validating-the-notification
module LedgerSync
  module QuickBooksOnline
    class WebhookNotification
      attr_reader :events,
                  :original_payload,
                  :payload,
                  :realm_id,
                  :webhook

      def initialize(args = {})
        @original_payload = args.fetch(:payload)
        @webhook          = args.fetch(:webhook, nil)
        @payload          = original_payload.is_a?(String) ? JSON.parse(original_payload) : original_payload

        @realm_id = @payload['realmId']
        raise 'Invalid payload: Could not find realmId' if @realm_id.blank?

        events_payload = @payload.dig('dataChangeEvent', 'entities')
        raise 'Invalid payload: Could not find dataChangeEvent -> entities' unless events_payload.is_a?(Array)

        @events = []

        events_payload.each do |event_payload|
          @events << WebhookEvent.new(
            payload: event_payload,
            webhook_notification: self
          )
        end
      end

      def resources
        @resources ||= events.map(&:resource).compact
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ledger_sync-quickbooks_online-2.0.0 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-1.0.1 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-1.0.0 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.4.0 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.3.1 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.3.0 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.2.6 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.2.5 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.2.4 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.2.2 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.2.1 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.2.0 lib/ledger_sync/quickbooks_online/webhook_notification.rb
ledger_sync-quickbooks_online-0.1.1 lib/ledger_sync/quickbooks_online/webhook_notification.rb