Class: LedgerSync::Ledgers::TestLedger::Webhook
- Inherits:
-
Object
- Object
- LedgerSync::Ledgers::TestLedger::Webhook
- Defined in:
- lib/ledger_sync/test/support/test_ledger/webhook.rb
Instance Attribute Summary collapse
-
#notifications ⇒ Object
readonly
Returns the value of attribute notifications.
-
#original_payload ⇒ Object
readonly
Returns the value of attribute original_payload.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
Class Method Summary collapse
Instance Method Summary collapse
- #events ⇒ Object
-
#initialize(payload:) ⇒ Webhook
constructor
A new instance of Webhook.
- #resources ⇒ Object
- #valid?(signature:, verification_token:) ⇒ Boolean
Constructor Details
#initialize(payload:) ⇒ Webhook
Returns a new instance of Webhook.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ledger_sync/test/support/test_ledger/webhook.rb', line 14 def initialize(payload:) @original_payload = payload @payload = payload.is_a?(String) ? JSON.parse(payload) : payload event_notifications_payload = @payload['eventNotifications'] raise 'Invalid payload: Could not find eventNotifications' unless event_notifications_payload.is_a?(Array) @notifications = [] event_notifications_payload.each do |event_notification_payload| @notifications << WebhookNotification.new( payload: event_notification_payload, webhook: self ) end end |
Instance Attribute Details
#notifications ⇒ Object (readonly)
Returns the value of attribute notifications.
10 11 12 |
# File 'lib/ledger_sync/test/support/test_ledger/webhook.rb', line 10 def notifications @notifications end |
#original_payload ⇒ Object (readonly)
Returns the value of attribute original_payload.
10 11 12 |
# File 'lib/ledger_sync/test/support/test_ledger/webhook.rb', line 10 def original_payload @original_payload end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
10 11 12 |
# File 'lib/ledger_sync/test/support/test_ledger/webhook.rb', line 10 def payload @payload end |
Class Method Details
.valid?(payload:, signature:, verification_token:) ⇒ Boolean
47 48 49 50 51 52 53 54 |
# File 'lib/ledger_sync/test/support/test_ledger/webhook.rb', line 47 def self.valid?(payload:, signature:, verification_token:) raise 'Cannot verify non-String payload' unless payload.is_a?(String) digest = OpenSSL::Digest.new('sha256') hmac = OpenSSL::HMAC.digest(digest, verification_token, payload) base64 = Base64.encode64(hmac).strip base64 == signature end |
Instance Method Details
#events ⇒ Object
31 32 33 |
# File 'lib/ledger_sync/test/support/test_ledger/webhook.rb', line 31 def events notifications.map(&:events) end |
#resources ⇒ Object
35 36 37 |
# File 'lib/ledger_sync/test/support/test_ledger/webhook.rb', line 35 def resources @resources ||= notifications.map(&:resources).flatten.compact end |
#valid?(signature:, verification_token:) ⇒ Boolean
39 40 41 42 43 44 45 |
# File 'lib/ledger_sync/test/support/test_ledger/webhook.rb', line 39 def valid?(signature:, verification_token:) self.class.valid?( payload: payload.to_json, signature: signature, verification_token: verification_token ) end |