Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moladukes/c45dae45a5a43a38270979d83a1907e5 to your computer and use it in GitHub Desktop.
Save moladukes/c45dae45a5a43a38270979d83a1907e5 to your computer and use it in GitHub Desktop.
Simple Webhooks in rails
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment