Class: LedgerSync::Ledgers::TestLedger::WebhookEvent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload:, webhook_notification: nil) ⇒ WebhookEvent

Returns a new instance of WebhookEvent.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 18

def initialize(payload:, webhook_notification: nil)
  @original_payload = payload
  @payload = payload.is_a?(String) ? JSON.parse(payload) : payload

  @deleted_id = @payload['deletedId']

  @event_operation = @payload['operation']
  raise 'Invalid payload: Could not find operation' if @event_operation.blank?

  @last_updated_at = @payload['lastUpdated']
  raise 'Invalid payload: Could not find lastUpdated' if @last_updated_at.blank?

  @last_updated_at = Time.parse(@last_updated_at)

  @ledger_id = @payload['id']
  raise 'Invalid payload: Could not find id' if @ledger_id.blank?

  @test_ledger_resource_type = @payload['name']
  raise 'Invalid payload: Could not find name' if @test_ledger_resource_type.blank?

  @webhook_notification = webhook_notification
  @webhook = webhook_notification.try(:webhook)
end

Instance Attribute Details

#deleted_idObject (readonly)

Returns the value of attribute deleted_id.



8
9
10
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 8

def deleted_id
  @deleted_id
end

#event_operationObject (readonly)

Returns the value of attribute event_operation.



8
9
10
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 8

def event_operation
  @event_operation
end

#last_updated_atObject (readonly)

Returns the value of attribute last_updated_at.



8
9
10
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 8

def last_updated_at
  @last_updated_at
end

#ledger_idObject (readonly)

Returns the value of attribute ledger_id.



8
9
10
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 8

def ledger_id
  @ledger_id
end

#original_payloadObject (readonly)

Returns the value of attribute original_payload.



8
9
10
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 8

def original_payload
  @original_payload
end

#payloadObject (readonly)

Returns the value of attribute payload.



8
9
10
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 8

def payload
  @payload
end

#test_ledger_resource_typeObject (readonly)

Returns the value of attribute test_ledger_resource_type.



8
9
10
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 8

def test_ledger_resource_type
  @test_ledger_resource_type
end

#webhookObject (readonly)

Returns the value of attribute webhook.



8
9
10
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 8

def webhook
  @webhook
end

#webhook_notificationObject (readonly)

Returns the value of attribute webhook_notification.



8
9
10
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 8

def webhook_notification
  @webhook_notification
end

Instance Method Details

#find(client:) ⇒ Object



42
43
44
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 42

def find(client:)
  find_operation(client: client).perform
end

#find_operation(client:) ⇒ Object



46
47
48
49
50
51
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 46

def find_operation(client:)
  find_operation_class(client: client).new(
    client: client,
    resource: resource_class.new(ledger_id: ledger_id)
  )
end

#find_operation_class(client:) ⇒ Object



53
54
55
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 53

def find_operation_class(client:)
  client.class.base_operations_module_for(resource_class: resource_class)::Find
end

#local_resource_typeObject



57
58
59
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 57

def local_resource_type
  @local_resource_type ||= resource_class.resource_type
end

#resourceObject



61
62
63
64
65
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 61

def resource
  return unless resource_class.present?

  resource_class.new(ledger_id: ledger_id)
end

#resource!Object



67
68
69
70
71
72
73
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 67

def resource!
  if resource.nil?
    raise "Resource class does not exist for QuickBooks Online object: #{test_ledger_resource_type}"
  end

  resource
end

#resource_classObject



75
76
77
# File 'lib/ledger_sync/test/support/test_ledger/webhook_event.rb', line 75

def resource_class
  @resource_class ||= Client.resource_from_ledger_type(type: test_ledger_resource_type.downcase)
end