Sha256: 64882827444757ef52c3c8ecda945a3e4dd6d37b4ee4e79ab77e8f43317c6d2e

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 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 Ledgers
    module QuickBooksOnline
      class WebhookNotification
        attr_reader :events,
                    :original_payload,
                    :payload,
                    :realm_id,
                    :webhook

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

          @realm_id = @payload.dig('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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ledger_sync-1.4.2 lib/ledger_sync/ledgers/quickbooks_online/webhook_notification.rb
ledger_sync-1.4.1 lib/ledger_sync/ledgers/quickbooks_online/webhook_notification.rb
ledger_sync-1.4.0 lib/ledger_sync/ledgers/quickbooks_online/webhook_notification.rb