Class: LedgerSync::Ledgers::TestLedger::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/ledger_sync/test/support/test_ledger/webhook.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#notificationsObject (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_payloadObject (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

#payloadObject (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

Returns:

  • (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

#eventsObject



31
32
33
# File 'lib/ledger_sync/test/support/test_ledger/webhook.rb', line 31

def events
  notifications.map(&:events)
end

#resourcesObject



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

Returns:

  • (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