Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save moladukes/c45dae45a5a43a38270979d83a1907e5 to your computer and use it in GitHub Desktop.

Select an option

Save moladukes/c45dae45a5a43a38270979d83a1907e5 to your computer and use it in GitHub Desktop.

Revisions

  1. moladukes created this gist May 10, 2023.
    52 changes: 52 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    # db/schema.rb
    create_table "webhooks", force: :cascade do |t|
    t.integer "webhook_type", null: false
    t.string "event_type", null: false
    t.string "resource_type", null: false
    t.jsonb "event_data", default: {}, null: false
    t.boolean "processed", default: false, null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    end


    # app/controllers/webhooks_controller.rb
    class WebhooksController < ApplicationController
    before_action :authenticate_provider, only: :provider

    def provider
    @webhook = Webhook.new(provider_webhook_params)
    @webhook.save!
    head :ok
    end

    private

    def authenticate_provider
    # secret, decoding, etc, verification that the
    # payload comes from the provider
    end

    def provider_webhook_params
    # mod of the provider to match my data structure
    end
    end


    # app/models/webhook.rb
    class Webhook < ApplicationRecord
    enum webhook_type: { provider: 0, another_provider: 1 }
    after_commit :enqueue_process_worker, on: :create

    private

    def enqueue_process_worker
    case webhook_type
    when 'provider'
    ProcessProviderWebhookWorker.perform_async(id)
    when 'another_provider'
    ProcessAnotherProviderWebhookWorker.perform_async(id)
    end
    end
    end