Class: Yookassa::Webhook::Notification
- Inherits:
-
Object
- Object
- Yookassa::Webhook::Notification
- Defined in:
- lib/yookassa/webhook/notification.rb
Overview
Parses incoming webhook notification payloads from YooKassa.
Automatically wraps the notification object into the appropriate entity class (Payment, Refund, Payout, or Deal) based on the event type.
Constant Summary collapse
- ENTITY_MAP =
Maps event type prefixes to entity classes.
{ "payment" => Entities::Payment, "refund" => Entities::Refund, "payout" => Entities::Payout, "deal" => Entities::Deal }.freeze
Instance Attribute Summary collapse
-
#event ⇒ String
readonly
The event type (e.g. "payment.succeeded").
-
#object ⇒ Entities::Base
readonly
The wrapped entity (Payment, Refund, Payout, or Deal).
-
#type ⇒ String
readonly
The notification type (always "notification").
Class Method Summary collapse
-
.parse(json_or_hash) ⇒ Notification
Parses a raw webhook payload (JSON string or Hash) into a Notification.
Instance Method Summary collapse
-
#initialize(event:, type:, object:) ⇒ Notification
constructor
A new instance of Notification.
Constructor Details
#initialize(event:, type:, object:) ⇒ Notification
38 39 40 41 42 |
# File 'lib/yookassa/webhook/notification.rb', line 38 def initialize(event:, type:, object:) @event = event @type = type @object = object end |
Instance Attribute Details
#event ⇒ String (readonly)
19 20 21 |
# File 'lib/yookassa/webhook/notification.rb', line 19 def event @event end |
#object ⇒ Entities::Base (readonly)
25 26 27 |
# File 'lib/yookassa/webhook/notification.rb', line 25 def object @object end |
#type ⇒ String (readonly)
22 23 24 |
# File 'lib/yookassa/webhook/notification.rb', line 22 def type @type end |
Class Method Details
.parse(json_or_hash) ⇒ Notification
Parses a raw webhook payload (JSON string or Hash) into a Yookassa::Webhook::Notification.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/yookassa/webhook/notification.rb', line 49 def self.parse(json_or_hash) data = json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash event = data["event"] entity_type = event.to_s.split(".").first entity_class = ENTITY_MAP[entity_type] || Entities::Base new( event: event, type: data["type"], object: entity_class.new(data["object"] || {}) ) end |