integrallis / stripe_event

Stripe webhook integration for Rails applications.
https://rubygems.org/gems/stripe_event
MIT License
841 stars 107 forks source link

Add an event finalizer #54

Closed peterkeen closed 9 years ago

peterkeen commented 9 years ago

This adds an event_finalizer hook that will be called after all subscribers have been notified of an event. This lets us record a completely successful event, while allowing Stripe to retry any failures.

Fixes #53

coveralls commented 9 years ago

Coverage Status

Changes Unknown when pulling 934c12d47893dd37e628a18a2ca58129492a30e4 on peterkeen:event-finalizer-53 into \ on integrallis:master**.

coveralls commented 9 years ago

Coverage Status

Changes Unknown when pulling 934c12d47893dd37e628a18a2ca58129492a30e4 on peterkeen:event-finalizer-53 into \ on integrallis:master**.

coveralls commented 9 years ago

Coverage Status

Changes Unknown when pulling 934c12d47893dd37e628a18a2ca58129492a30e4 on peterkeen:event-finalizer-53 into \ on integrallis:master**.

coveralls commented 9 years ago

Coverage Status

Changes Unknown when pulling 934c12d47893dd37e628a18a2ca58129492a30e4 on peterkeen:event-finalizer-53 into \ on integrallis:master**.

rmm5t commented 9 years ago

I think I would prefer to see something like an optional event_wrapper (for lack of a better name right now) or maybe just an around configuration that takes a Proc (that in turn calls the normal backend.instrument routine). This would allow people to effectively run both before and after hooks instead of just an after hook.

Example usage (psuedo, untested so far):

StripeEvent.around = lambda do |event, subscribers|
  # stuff before (e.g. log a start time)
  subscribers.call
  # stuff after (e.g. log total processing time or track fully processed event
  # to help prevent replay attacks)
end

Example instrument implementation and default around config implementation (also untested):

module StripeEvent
  class << self
    # ...
    def instrument(params)
      # ...
      if event
        subscribers = lambda { backend.instrument(namespace.call(event[:type]), event) }
        around.call(event, subscribers)
      end
    end
    # ...
  end
  #...
  self.around = lambda { |event, subscribers| subscribers.call }
# ...

Just brainstorming for now. There's probably room to clean this up, but I can turn this into a more formal pull-request if that would help.