Skip to content

Commit

Permalink
feat: use occurred_at instead of timestamp in register_feedback requests
Browse files Browse the repository at this point in the history
This commit also adds a test to validate whether the new
request_token field is supported.
  • Loading branch information
figueredo committed Jul 24, 2024
1 parent 9b6ec9b commit c2442f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,14 @@ The `expires_at` argument should be a _Time_, _DateTime_ or an date in **RFC 333


```ruby
account_id = "cdb2cfbb-8ad8-4668-b276-5fff9bbfdc96"
timestamp = DateTime.parse('2022-06-20 23:29:00 UTC-3')
installation_id = 'installation-id'
account_id = 'account-id'
occurred_at = DateTime.parse('2024-07-22T15:20:00Z')

success = api.register_feedback(
event: Incognia::Constants::FeedbackEvent::IDENTITY_FRAUD,
timestamp: timestamp,
event: Incognia::Constants::FeedbackEvent::ACCOUNT_TAKEOVER,
occurred_at: occurred_at,
installation_id: installation_id,
account_id: account_id
)

Expand All @@ -219,9 +221,9 @@ For custom fraud, set the value of `event` with the corresponding code:
```ruby
success = api.register_feedback(
event: 'custom_fraud_name',
timestamp: timestamp,
account_id: account_id,
installation_id: installation_id
occurred_at: occurred_at,
installation_id: installation_id,
account_id: account_id
)

# => true
Expand Down
9 changes: 7 additions & 2 deletions lib/incognia_api/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ def register_login(installation_id:, account_id:, **opts)
LoginAssessment.from_hash(response.body) if response.success?
end

def register_feedback(event:, timestamp: nil, expires_at: nil, **ids)
def register_feedback(event:, occurred_at: nil, expires_at: nil, timestamp: nil, **ids)
if !timestamp.nil?
warn("Deprecation warning: use occurred_at instead of timestamp")
end

timestamp = timestamp.strftime('%s%L') if timestamp.respond_to? :strftime
occurred_at = occurred_at.to_datetime.rfc3339 if occurred_at.respond_to? :to_datetime
expires_at = expires_at.to_datetime.rfc3339 if expires_at.respond_to? :to_datetime

params = { event: event, timestamp: timestamp&.to_i, expires_at: expires_at }.compact
params = { event: event, timestamp: timestamp&.to_i, occurred_at: occurred_at, expires_at: expires_at }.compact
params.merge!(ids)

response = connection.request(
Expand Down
34 changes: 34 additions & 0 deletions spec/incognia_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,40 @@ module Incognia
end
end

context "when receiving occurred_at as a Time" do
let(:occurred_at) { Time.now }

it "hits the endpoint with expires_at in RFC3339" do
stub = stub_register_feedback_request.with(
body: { event: event, occurred_at: occurred_at.to_datetime.rfc3339 },
headers: {
'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/
}
)

api.register_feedback(event: event, occurred_at: occurred_at)

expect(stub).to have_been_made.once
end
end

context "when receiving occurred_at as a DateTime" do
let(:occurred_at) { DateTime.now }

it "hits the endpoint with occurred_at in RFC3339" do
stub = stub_register_feedback_request.with(
body: { event: event, occurred_at: occurred_at.to_datetime.rfc3339 },
headers: {
'Content-Type' => 'application/json', 'Authorization' => /Bearer.*/
}
)

api.register_feedback(event: event, occurred_at: occurred_at)

expect(stub).to have_been_made.once
end
end

context "when receiving expires_at as a Time" do
let(:expires_at) { Time.now }

Expand Down

0 comments on commit c2442f4

Please sign in to comment.