lib/stripe_event.rb in stripe_event-0.5.0 vs lib/stripe_event.rb in stripe_event-0.6.0
- old
+ new
@@ -1,28 +1,37 @@
require "set"
require "stripe"
require "stripe_event/engine"
-require "stripe_event/subscriber"
-require "stripe_event/publisher"
module StripeEvent
- mattr_accessor :event_retriever
- self.event_retriever = Proc.new { |params| Stripe::Event.retrieve(params[:id]) }
+ class << self
+ attr_accessor :backend
+ attr_accessor :event_retriever
- def self.setup(&block)
- instance_eval(&block)
- end
+ def setup(&block)
+ instance_eval(&block)
+ end
- def self.publish(event)
- Publisher.new(event).instrument
- end
+ def instrument(params)
+ publish event_retriever.call(params)
+ end
- def self.subscribe(*names, &block)
- Subscriber.new(*names).register(&block)
+ def publish(event)
+ backend.publish event[:type], event
+ end
+
+ def subscribe(*names, &block)
+ pattern = Regexp.union(names.empty? ? TYPE_LIST.to_a : names)
+
+ backend.subscribe(pattern) do |*args|
+ payload = args.last
+ block.call payload
+ end
+ end
end
- class StripeEventError < StandardError; end
- class InvalidEventTypeError < StripeEventError; end
+ self.backend = ActiveSupport::Notifications
+ self.event_retriever = lambda { |params| Stripe::Event.retrieve(params[:id]) }
TYPE_LIST = Set[
'account.updated',
'account.application.deauthorized',
'charge.succeeded',