Module: StripeWebhooks::Callback::ClassMethods

Included in:
StripeWebhooks::Callback
Defined in:
app/models/stripe_webhooks/callback.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#event_typesObject (readonly)

Returns the value of attribute event_types.



5
6
7
# File 'app/models/stripe_webhooks/callback.rb', line 5

def event_types
  @event_types
end

Instance Method Details

#handles_events(*event_types) ⇒ Object

Declares which event types a callback will handle

Example

class MyCallback < StripeWebhooks::Callback
  handles_events 'customer.subscription.deleted', 'customer.subscription.testing'
  def run(event)
    puts "Do useful stuff"
  end
end


34
35
36
# File 'app/models/stripe_webhooks/callback.rb', line 34

def handles_events(*event_types)
  @event_types = event_types
end

#run_callbacks_for(event_type, event) ⇒ Object

Run callbacks for a given event type

Example

StripeWebhooks::Callback.run_callbacks_for('customer.subscription.deleted', stripe_event)


13
14
15
16
17
18
19
20
21
# File 'app/models/stripe_webhooks/callback.rb', line 13

def run_callbacks_for(event_type, event)
  StripeWebhooks.callbacks.each do |label|
    class_name = "#{label.classify}Callback"
    callback = class_name.constantize.new
    if callback.handles?(event_type)
      callback.run_once(event)
    end
  end
end