Class: VcrStripeWebhook::EventReceiver
- Inherits:
-
Object
- Object
- VcrStripeWebhook::EventReceiver
- Defined in:
- lib/vcr_stripe_webhook/event_receiver.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ EventReceiver
constructor
A new instance of EventReceiver.
- #start ⇒ Object
- #stop ⇒ Object
- #use_cassette(cassette) ⇒ Object
-
#wait_for_rest_webhooks ⇒ Object
Create and delete dummy test clock to wait for the rest webhooks to be received.
Constructor Details
#initialize ⇒ EventReceiver
Returns a new instance of EventReceiver.
22 23 24 |
# File 'lib/vcr_stripe_webhook/event_receiver.rb', line 22 def initialize @started = false end |
Class Method Details
.instance ⇒ Object
10 11 12 |
# File 'lib/vcr_stripe_webhook/event_receiver.rb', line 10 def instance @instance ||= EventReceiver.new end |
.terminate ⇒ Object
14 15 16 17 18 19 |
# File 'lib/vcr_stripe_webhook/event_receiver.rb', line 14 def terminate return unless @instance @instance.stop @instance = nil end |
Instance Method Details
#start ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vcr_stripe_webhook/event_receiver.rb', line 26 def start return if @started api_key = VcrStripeWebhook.configuration.stripe_api_key @fence_mutex = Mutex.new @fence_cond_var = ConditionVariable.new @fence_test_clock_id = nil @fence_signaled = false @last_fence_event_created = nil @cassette = nil @server = VcrStripeWebhook::Server.new do |payload| receive_webhook(payload) end @cli = VcrStripeWebhook::StripeCLI.new(@server.port, api_key) @started = true end |
#stop ⇒ Object
45 46 47 48 49 |
# File 'lib/vcr_stripe_webhook/event_receiver.rb', line 45 def stop @cli.terminate @server.close @started = false end |
#use_cassette(cassette) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/vcr_stripe_webhook/event_receiver.rb', line 51 def use_cassette(cassette) if cassette.recording? start logger.info "Using cassette: #{cassette.name}" @cassette = cassette yield cassette.vcr_cassette logger.info "Eject cassette: #{cassette.name}" @cassette = nil wait_for_rest_webhooks else yield cassette.vcr_cassette end end |
#wait_for_rest_webhooks ⇒ Object
Create and delete dummy test clock to wait for the rest webhooks to be received. Stripe doesn’t guarantee delivery of events in the order in which they’re generated. But waiting dummy test clock event lower the probability of webhook ordering problem.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/vcr_stripe_webhook/event_receiver.rb', line 68 def wait_for_rest_webhooks time = Time.now name = "vcr_sw_fence_#{time.to_i}_#{SecureRandom.alphanumeric}" test_clock = nil @fence_mutex.synchronize do raise "Fence already used!" if @fence_test_clock_id @fence_signaled = false test_clock = Stripe::TestHelpers::TestClock.create(frozen_time: time.to_i, name: name) @fence_test_clock_id = test_clock.id end test_clock.delete @fence_mutex.synchronize do loop do break if @fence_signaled @fence_cond_var.wait(@fence_mutex, VcrStripeWebhook.configuration.timeout) end @fence_test_clock_id = nil end end |