Sha256: 01bd9e43948e5c77591505c6685a1f0e26e1d71a5a7b16fc0b6b1aeccbbac4d3

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

require "active_support/notifications"
require "stripe"
require "stripe_event/engine" if defined?(Rails)

module StripeEvent
  class << self
    attr_accessor :adapter, :backend, :namespace, :event_filter
    attr_reader :signing_secrets

    def configure(&block)
      raise ArgumentError, "must provide a block" unless block_given?
      block.arity.zero? ? instance_eval(&block) : yield(self)
    end
    alias :setup :configure

    def instrument(event)
      event = event_filter.call(event)
      backend.instrument namespace.call(event.type), event if event
    end

    def subscribe(name, callable = Proc.new)
      backend.subscribe namespace.to_regexp(name), adapter.call(callable)
    end

    def all(callable = Proc.new)
      subscribe nil, callable
    end

    def listening?(name)
      namespaced_name = namespace.call(name)
      backend.notifier.listening?(namespaced_name)
    end

    def signing_secret=(value)
      @signing_secrets = Array(value)
    end
    alias signing_secrets= signing_secret=

    def signing_secret
      self.signing_secrets && self.signing_secrets.first
    end
  end

  class Namespace < Struct.new(:value, :delimiter)
    def call(name = nil)
      "#{value}#{delimiter}#{name}"
    end

    def to_regexp(name = nil)
      %r{^#{Regexp.escape call(name)}}
    end
  end

  class NotificationAdapter < Struct.new(:subscriber)
    def self.call(callable)
      new(callable)
    end

    def call(*args)
      payload = args.last
      subscriber.call(payload)
    end
  end

  class Error < StandardError; end
  class UnauthorizedError < Error; end

  self.adapter = NotificationAdapter
  self.backend = ActiveSupport::Notifications
  self.namespace = Namespace.new("stripe_event", ".")
  self.event_filter = lambda { |event| event }
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stripe_event-2.3.0 lib/stripe_event.rb
stripe_event-2.2.0 lib/stripe_event.rb
stripe_event-2.1.1 lib/stripe_event.rb
stripe_event-2.0.0 lib/stripe_event.rb