Class: StarkBank::Event
- Inherits:
-
Utils::Resource
- Object
- Utils::Resource
- StarkBank::Event
- Defined in:
- lib/event/event.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, 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
-
subscription [string]: service that triggered this event. ex: ‘transfer’, ‘utility-payment’
Instance Attribute Summary collapse
-
#created ⇒ Object
readonly
Returns the value of attribute created.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#is_delivered ⇒ Object
readonly
Returns the value of attribute is_delivered.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#subscription ⇒ Object
readonly
Returns the value of attribute subscription.
Class Method Summary collapse
-
.delete(id, user: nil) ⇒ Object
# Delete a notification Event.
-
.get(id, user: nil) ⇒ Object
# Retrieve a specific notification Event.
-
.parse(content:, signature:, user: nil) ⇒ Object
# Create single notification Event from a content string.
-
.query(limit: nil, after: nil, before: nil, is_delivered: nil, user: nil) ⇒ Object
# Retrieve notification Events.
-
.update(id, is_delivered:, user: nil) ⇒ Object
# Update notification Event entity.
Instance Method Summary collapse
-
#initialize(id:, log:, created:, is_delivered:, subscription:) ⇒ Event
constructor
A new instance of Event.
Methods inherited from Utils::Resource
Constructor Details
#initialize(id:, log:, created:, is_delivered:, subscription:) ⇒ Event
Returns a new instance of Event.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/event/event.rb', line 30 def initialize(id:, log:, created:, is_delivered:, subscription:) super(id) @created = StarkBank::Utils::Checks.check_datetime(created) @is_delivered = is_delivered @subscription = subscription maker = { 'transfer': StarkBank::Transfer::Log.resource, 'boleto': StarkBank::Boleto::Log.resource, 'boleto-payment': StarkBank::BoletoPayment::Log.resource, 'utility-payment': StarkBank::UtilityPayment::Log.resource }[subscription.to_sym][:resource_maker] @log = StarkBank::Utils::API.from_api_json(maker, log) end |
Instance Attribute Details
#created ⇒ Object (readonly)
Returns the value of attribute created.
29 30 31 |
# File 'lib/event/event.rb', line 29 def created @created end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
29 30 31 |
# File 'lib/event/event.rb', line 29 def id @id end |
#is_delivered ⇒ Object (readonly)
Returns the value of attribute is_delivered.
29 30 31 |
# File 'lib/event/event.rb', line 29 def is_delivered @is_delivered end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
29 30 31 |
# File 'lib/event/event.rb', line 29 def log @log end |
#subscription ⇒ Object (readonly)
Returns the value of attribute subscription.
29 30 31 |
# File 'lib/event/event.rb', line 29 def subscription @subscription 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 [Project object]: Project object. Not necessary if StarkBank.user was set before function call
## Return:
-
deleted Event with updated attributes
100 101 102 |
# File 'lib/event/event.rb', line 100 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 [Project object]: Project object. Not necessary if StarkBank.user was set before function call
## Return:
-
Event object with updated attributes
58 59 60 |
# File 'lib/event/event.rb', line 58 def self.get(id, user: nil) StarkBank::Utils::Rest.get_id(id: id, 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 [Project object]: Project object. Not necessary if StarkBank.user was set before function call
## Return:
-
Parsed Event object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/event/event.rb', line 137 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 [Project object, default nil]: Project object. Not necessary if StarkBank.user was set before function call
## Return:
-
generator of Event objects with updated attributes
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/event/event.rb', line 75 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_list( 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 [Project object]: Project object. Not necessary if StarkBank.user was set before function call
## Return:
-
target Event with updated attributes
118 119 120 |
# File 'lib/event/event.rb', line 118 def self.update(id, is_delivered:, user: nil) StarkBank::Utils::Rest.patch_id(id: id, user: user, is_delivered: is_delivered, **resource) end |