Module: StripeLocal::Webhook

Defined in:
lib/stripe_local/webhook.rb,
lib/stripe_local/webhook/types.rb,
lib/stripe_local/webhook/subscriber.rb

Overview

basically taken straight from the StripeEvents gem github.com/integrallis/stripe_event

Defined Under Namespace

Classes: Subscriber

Constant Summary collapse

InvalidEventType =
Class.new(StandardError)
TYPE_LIST =
[
'account.updated',
'account.application.deauthorized',
  'charge.succeeded',
  'charge.failed',
  'charge.refunded',
  'charge.dispute.created',
  'charge.dispute.updated',
  'charge.dispute.closed',
  'customer.created',
  'customer.updated',
  'customer.deleted',
  'customer.subscription.created',
  'customer.subscription.updated',
  'customer.subscription.deleted',
  'customer.subscription.trial_will_end',
  'customer.discount.created',
  'customer.discount.updated',
  'customer.discount.deleted',
  'invoice.created',
  'invoice.updated',
  'invoice.payment_succeeded',
  'invoice.payment_failed',
  'invoiceitem.created',
  'invoiceitem.updated',
  'invoiceitem.deleted',
  'plan.created',
  'plan.updated',
  'plan.deleted',
  'coupon.created',
  'coupon.updated',
  'coupon.deleted',
  'transfer.created',
  'transfer.paid',
'transfer.updated',
  'transfer.failed',
'balance.available',
  'ping'
]

Class Method Summary collapse

Class Method Details

.clear_subscribers!Object



28
29
30
31
32
# File 'lib/stripe_local/webhook.rb', line 28

def clear_subscribers!
  TYPE_LIST.each do |type|
    subscribers(type).each { |s| unsubscribe(s) }
  end
end

.dispatch(event) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/stripe_local/webhook.rb', line 43

def dispatch event
	object,action,sub_action = event.type.split('.')
	unless sub_action.nil?
		object = [object,action].join('_')
		action = sub_action
	end
	translate object, action, event
end

.publish(event_obj) ⇒ Object



16
17
18
# File 'lib/stripe_local/webhook.rb', line 16

def publish event_obj
  ActiveSupport::Notifications.instrument event_obj.type, event: event_obj
end

.subscribe(*names, &block) ⇒ Object



20
21
22
# File 'lib/stripe_local/webhook.rb', line 20

def subscribe *names, &block
  Subscriber.new(*names, &block).register
end

.subscribers(name) ⇒ Object



24
25
26
# File 'lib/stripe_local/webhook.rb', line 24

def subscribers name
  ActiveSupport::Notifications.notifier.listeners_for name
end

.translate(object, action, event) ⇒ Object



38
39
40
41
# File 'lib/stripe_local/webhook.rb', line 38

def translate object, action, event
	klass = object.camelize.constantize
	klass.send action, event
end

.unsubscribe(subscriber) ⇒ Object



34
35
36
# File 'lib/stripe_local/webhook.rb', line 34

def unsubscribe subscriber
  ActiveSupport::Notifications.notifier.unsubscribe subscriber
end