Skip to content

Commit 1f394eb

Browse files
committed
add basic inverted handler
1 parent 2661170 commit 1f394eb

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Layout/LineLength:
1818
Exclude:
1919
- "lib/stripe/object_types.rb"
2020
- "lib/stripe/stripe_client.rb"
21+
- "lib/stripe/stripe_event_handler.rb"
2122
- "lib/stripe/resources/**/*.rb"
2223
- "lib/stripe/services/**/*.rb"
2324
- "lib/stripe/events/**/*.rb"
@@ -86,6 +87,10 @@ Metrics/ParameterLists:
8687
- "lib/stripe/stripe_client.rb"
8788
- "lib/stripe/params/**/*.rb"
8889

90+
Naming/MethodName:
91+
Exclude:
92+
- "lib/stripe/stripe_event_handler.rb"
93+
8994
Naming/MethodParameterName:
9095
# We have many parameters that are less than 3 characters for tax codes
9196
Exclude:

lib/stripe/stripe_event_handler.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
module Stripe
4+
class StripeEventHandler
5+
def initialize(client, webhook_secret)
6+
@client = client
7+
@registered_handlers = {}
8+
@webhook_secret = webhook_secret
9+
end
10+
11+
def handle(webhook_body, sig_header)
12+
@client.parse_event_notification(
13+
webhook_body,
14+
sig_header,
15+
@webhook_secret
16+
)
17+
18+
# TODO: rebind stripe-context temporarily
19+
case event_notification.type
20+
# event-method-routing: The beginning of the section generated from our OpenAPI spec
21+
when
22+
"v1.billing.meter.error_report_triggered"
23+
on_V1BillingMeterErrorReportTriggeredEventNotification(event_notification, @client)
24+
when
25+
"v1.billing.meter.no_meter_found"
26+
on_V1BillingMeterNoMeterFoundEventNotification(event_notification, @client)
27+
when
28+
"v2.core.event_destination.ping"
29+
on_V2CoreEventDestinationPingEventNotification(event_notification, @client)
30+
# event-method-routing: The end of the section generated from our OpenAPI spec
31+
else
32+
# only used for types that the SDK has no class for
33+
on_UnknownEventNotification(
34+
event_notification,
35+
@client
36+
)
37+
end
38+
end
39+
40+
# Overwrite this method to handle events that the SDK has types for, but you've chosen not to handle with their dedicated method.
41+
def on_unhandled_event_notification(event_notification, _client)
42+
raise "Received an event that the SDK has a corresponding class for, but for which you haven't written a corresponding handler: \"#{event_notification.type}\""
43+
end
44+
45+
def on_UnknownEventNotification(event_notification, _client)
46+
raise "Received event type that the SDK doesn't have a corresponding class for: \"#{event_notification.type}\". Consider upgrading your SDK to handle this more gracefully."
47+
end
48+
49+
# event-handler-methods: The beginning of the section generated from our OpenAPI spec
50+
def on_V1BillingMeterErrorReportTriggeredEventNotification(event_notification, client)
51+
on_unhandled_event_notification(event_notification, client)
52+
end
53+
54+
def on_V1BillingMeterNoMeterFoundEventNotification(event_notification, client)
55+
on_unhandled_event_notification(event_notification, client)
56+
end
57+
58+
def on_V2CoreEventDestinationPingEventNotification(event_notification, client)
59+
on_unhandled_event_notification(event_notification, client)
60+
end
61+
# event-handler-methods: The end of the section generated from our OpenAPI spec
62+
end
63+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
# typed: true
3+
4+
module Stripe
5+
class StripeEventHandler
6+
sig do
7+
params(client: ::Stripe::StripeClient, webhook_secret: String)
8+
.void
9+
end
10+
def initialize(client, webhook_secret); end
11+
end
12+
13+
sig do
14+
params(
15+
webhook_body: String,
16+
sig_header: String
17+
)
18+
.void
19+
end
20+
def handle(webhook_body, sig_header); end
21+
22+
sig do
23+
overridable.params(event_notification: ::Stripe::V2::Core::EventNotification, client: ::Stripe::StripeClient).void
24+
end
25+
def on_unhandled_event_notification(event_notification, client); end
26+
27+
sig do
28+
overridable.params(event_notification: ::Stripe::Events::UnknownEventNotification, client: ::Stripe::StripeClient).void
29+
end
30+
def on_UnknownEventNotification(event_notification, client); end
31+
32+
# event-handler-methods: The beginning of the section generated from our OpenAPI spec
33+
sig do
34+
overridable.params(event_notification: ::Stripe::Events::V1BillingMeterErrorReportTriggeredEventNotification, client: ::Stripe::StripeClient).void
35+
end
36+
def on_V1BillingMeterErrorReportTriggeredEventNotification(event_notification, client);
37+
end
38+
39+
sig do
40+
overridable.params(event_notification: ::Stripe::Events::V1BillingMeterNoMeterFoundEventNotification, client: ::Stripe::StripeClient).void
41+
end
42+
def on_V1BillingMeterNoMeterFoundEventNotification(event_notification, client);
43+
end
44+
45+
sig do
46+
overridable.params(event_notification: ::Stripe::Events::V2CoreEventDestinationPingEventNotification, client: ::Stripe::StripeClient).void
47+
end
48+
def on_V2CoreEventDestinationPingEventNotification(event_notification, client);
49+
end
50+
51+
52+
# event-handler-methods: The end of the section generated from our OpenAPI spec
53+
end

0 commit comments

Comments
 (0)