Class: StarkBank::Event

Inherits:
Utils::Resource show all
Defined in:
lib/event/event.rb,
lib/event/attempt.rb

Overview

# Webhook Event object

An Event is the notification received from the subscription to the Webhook. Events cannot be created, but may be retrieved from the Stark Bank API to list all generated updates on entities.

## Attributes:

  • id [string]: unique id returned when the event is created. ex: ‘5656565656565656’

  • log [Log]: a Log object from one the subscription services (TransferLog, InvoiceLog, BoletoLog, BoletoPaymentlog or UtilityPaymentLog)

  • created [DateTime]: creation datetime for the notification event. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)

  • is_delivered [bool]: true if the event has been successfully delivered to the user url. ex: False

  • workspace_id [string]: ID of the Workspace that generated this event. Mostly used when multiple Workspaces have Webhooks registered to the same endpoint. ex: ‘4545454545454545’

  • subscription [string]: service that triggered this event. ex: ‘transfer’, ‘utility-payment’

Defined Under Namespace

Classes: Attempt

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Utils::SubResource

#inspect, #to_s

Constructor Details

#initialize(id:, log:, created:, is_delivered:, workspace_id:, subscription:) ⇒ Event

Returns a new instance of Event.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/event/event.rb', line 37

def initialize(id:, log:, created:, is_delivered:, workspace_id:, subscription:)
  super(id)
  @created = StarkBank::Utils::Checks.check_datetime(created)
  @is_delivered = is_delivered
  @workspace_id = workspace_id
  @subscription = subscription

  resource = {
    'transfer': StarkBank::Transfer::Log.resource,
    'invoice': StarkBank::Invoice::Log.resource,
    'deposit': StarkBank::Deposit::Log.resource,
    'brcode-payment': StarkBank::BrcodePayment::Log.resource,
    'boleto': StarkBank::Boleto::Log.resource,
    'boleto-payment': StarkBank::BoletoPayment::Log.resource,
    'utility-payment': StarkBank::UtilityPayment::Log.resource,
    'tax-payment': StarkBank::TaxPayment::Log.resource,
    'darf-payment': StarkBank::DarfPayment::Log.resource,
    'boleto-holmes': StarkBank::BoletoHolmes::Log.resource
  }[subscription.to_sym]

  @log = log
  @log = StarkBank::Utils::API.from_api_json(resource[:resource_maker], log) unless resource.nil?
end

Instance Attribute Details

#createdObject (readonly)

Returns the value of attribute created.



36
37
38
# File 'lib/event/event.rb', line 36

def created
  @created
end

#idObject (readonly)

Returns the value of attribute id.



36
37
38
# File 'lib/event/event.rb', line 36

def id
  @id
end

#is_deliveredObject (readonly)

Returns the value of attribute is_delivered.



36
37
38
# File 'lib/event/event.rb', line 36

def is_delivered
  @is_delivered
end

#logObject (readonly)

Returns the value of attribute log.



36
37
38
# File 'lib/event/event.rb', line 36

def log
  @log
end

#subscriptionObject (readonly)

Returns the value of attribute subscription.



36
37
38
# File 'lib/event/event.rb', line 36

def subscription
  @subscription
end

#workspace_idObject (readonly)

Returns the value of attribute workspace_id.



36
37
38
# File 'lib/event/event.rb', line 36

def workspace_id
  @workspace_id
end

Class Method Details

.delete(id, user: nil) ⇒ Object

# Delete a notification Event

Delete a of notification Event entity previously created in the Stark Bank API by its ID

## Parameters (required):

  • id [string]: Event unique id. ex: ‘5656565656565656’

## Parameters (optional):

  • user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call

## Return:

  • deleted Event object



144
145
146
# File 'lib/event/event.rb', line 144

def self.delete(id, user: nil)
  StarkBank::Utils::Rest.delete_id(id: id, user: user, **resource)
end

.get(id, user: nil) ⇒ Object

# Retrieve a specific notification Event

Receive a single notification Event object previously created in the Stark Bank API by passing its id

## Parameters (required):

  • id [string]: object unique id. ex: ‘5656565656565656’

## Parameters (optional):

  • user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call

## Return:

  • Event object with updated attributes



73
74
75
# File 'lib/event/event.rb', line 73

def self.get(id, user: nil)
  StarkBank::Utils::Rest.get_id(id: id, user: user, **resource)
end

.page(cursor: nil, limit: nil, after: nil, before: nil, is_delivered: nil, user: nil) ⇒ Object

# Retrieve paged Events

Receive a list of up to 100 Event objects previously created in the Stark Bank API and the cursor to the next page. Use this function instead of query if you want to manually page your requests.

## Parameters (optional):

  • cursor [string, default nil]: cursor returned on the previous page function call

  • limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35

  • after [Date, DateTime, Time or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)

  • before [Date, DateTime, Time or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)

  • is_delivered [bool, default nil]: bool to filter successfully delivered events. ex: True or False

  • user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call

## Return:

  • list of Event objects with updated attributes and cursor to retrieve the next page of Event objects



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/event/event.rb', line 118

def self.page(cursor: nil, limit: nil, after: nil, before: nil, is_delivered: nil, user: nil)
  after = StarkBank::Utils::Checks.check_date(after)
  before = StarkBank::Utils::Checks.check_date(before)
  return StarkBank::Utils::Rest.get_page(
    cursor: cursor,
    limit: limit,
    after: after,
    before: before,
    is_delivered: is_delivered,
    user: user,
    **resource
  )
end

.parse(content:, signature:, user: nil) ⇒ Object

# Create single notification Event from a content string

Create a single Event object received from event listening at subscribed user endpoint. If the provided digital signature does not check out with the StarkBank public key, a starkbank.exception.InvalidSignatureException will be raised.

## Parameters (required):

  • content [string]: response content from request received at user endpoint (not parsed)

  • signature [string]: base-64 digital signature received at response header ‘Digital-Signature’

## Parameters (optional):

  • user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call

## Return:

  • Parsed Event object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/event/event.rb', line 181

def self.parse(content:, signature:, user: nil)
  event = StarkBank::Utils::API.from_api_json(resource[:resource_maker], JSON.parse(content)['event'])

  begin
    signature = EllipticCurve::Signature.fromBase64(signature)
  rescue
    raise(StarkBank::Error::InvalidSignatureError, 'The provided signature is not valid')
  end

  return event if verify_signature(content: content, signature: signature, user: user)

  return event if verify_signature(content: content, signature: signature, user: user, refresh: true)

  raise(StarkBank::Error::InvalidSignatureError, 'The provided signature and content do not match the Stark Bank public key')
end

.query(limit: nil, after: nil, before: nil, is_delivered: nil, user: nil) ⇒ Object

# Retrieve notification Events

Receive a generator of notification Event objects previously created in the Stark Bank API

## Parameters (optional):

  • limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35

  • after [Date, DateTime, Time or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)

  • before [Date, DateTime, Time or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)

  • is_delivered [bool, default nil]: bool to filter successfully delivered events. ex: True or False

  • user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call

## Return:

  • generator of Event objects with updated attributes



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/event/event.rb', line 90

def self.query(limit: nil, after: nil, before: nil, is_delivered: nil, user: nil)
  after = StarkBank::Utils::Checks.check_date(after)
  before = StarkBank::Utils::Checks.check_date(before)
  StarkBank::Utils::Rest.get_stream(
    user: user,
    limit: limit,
    after: after,
    before: before,
    is_delivered: is_delivered,
    **resource
  )
end

.update(id, is_delivered:, user: nil) ⇒ Object

# Update notification Event entity

Update notification Event by passing id. If is_delivered is True, the event will no longer be returned on queries with is_delivered=False.

## Parameters (required):

  • id [list of strings]: Event unique ids. ex: ‘5656565656565656’

  • is_delivered [bool]: If True and event hasn’t been delivered already, event will be set as delivered. ex: True

## Parameters (optional):

  • user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call

## Return:

  • target Event with updated attributes



162
163
164
# File 'lib/event/event.rb', line 162

def self.update(id, is_delivered:, user: nil)
  StarkBank::Utils::Rest.patch_id(id: id, user: user, is_delivered: is_delivered, **resource)
end