Skip to content

Commit

Permalink
Merge pull request #91 from SiftScience/API-7683
Browse files Browse the repository at this point in the history
API-7683: Add support for warnings in Events API
  • Loading branch information
sbogolii-sift authored May 17, 2024
2 parents 05c9957 + 009f91c commit 0544b4a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
3 changes: 3 additions & 0 deletions HISTORY
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
=== 4.5.0 2024-05-16
- Support for warnings in Events API

=== 4.4.0 2023-10-05
- Score percentiles in Score API

Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# sift-ruby
[![CircleCI](https://circleci.com/gh/SiftScience/sift-ruby.svg?style=svg)](https://circleci.com/gh/SiftScience/sift-ruby)

The official Ruby bindings for the latest version (v205) of the [Sift API](https://sift.com/developers/docs/java/apis-overview).

Expand Down Expand Up @@ -39,8 +38,22 @@ client = Sift::Client.new(api_key: '<your_api_key_here>', account_id: '<your_acc

```

### Sending a transaction event
### Sending an event
Send event to Sift.
To learn more about the Events API visit our [developer docs](https://developers.sift.com/docs/ruby/events-api/overview).


**Optional Params**
- `return_score`: `:true` or `:false`
- `return_action`: `:true` or `:false`
- `return_workflow_status`: `:true` or `:false`
- `return_route_info`: `:true` or `:false`
- `force_workflow_run`: `:true` or `:false`
- `include_score_percentiles`: `:true` or `:false`
- `warnings`: `:true` or `:false`
- `abuse_types`: `["payment_abuse", "content_abuse", "content_abuse", "account_abuse", "legacy", "account_takeover"]`

**Example:**
```ruby
event = "$transaction"

Expand Down
12 changes: 10 additions & 2 deletions lib/sift/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def user_agent
# :include_score_percentiles::
# include_score_percentiles(optional) : Whether to add new parameter in the query parameter.
#
# :warnings::
# warnings(optional) : Whether to add list of warnings (if any) to response.
#
# ==== Returns:
#
# In the case of a network error (timeout, broken connection, etc.),
Expand All @@ -223,6 +226,7 @@ def track(event, properties = {}, opts = {})
force_workflow_run = opts[:force_workflow_run]
abuse_types = opts[:abuse_types]
include_score_percentiles = opts[:include_score_percentiles]
warnings = opts[:warnings]

raise("event must be a non-empty string") if (!event.is_a? String) || event.empty?
raise("properties cannot be empty") if properties.empty?
Expand All @@ -235,8 +239,12 @@ def track(event, properties = {}, opts = {})
query["return_route_info"] = "true" if return_route_info
query["force_workflow_run"] = "true" if force_workflow_run
query["abuse_types"] = abuse_types.join(",") if abuse_types
if include_score_percentiles == "true"
query["fields"] = "SCORE_PERCENTILES"

if include_score_percentiles == "true" || warnings == "true"
fields = []
fields << "SCORE_PERCENTILES" if include_score_percentiles == "true"
fields << "WARNINGS" if warnings == "true"
query["fields"] = fields.join(",")
end

options = {
Expand Down
2 changes: 1 addition & 1 deletion lib/sift/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Sift
VERSION = "4.4.0"
VERSION = "4.5.0"
API_VERSION = "205"
end
30 changes: 30 additions & 0 deletions spec/unit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ def action_response_json
}
end

def warnings
{
:count => 1,
:items => [{
:message => 'Invalid currency'
}]
}
end

def percentile_response_json
{
:user_id => 'billy_jones_301',
Expand Down Expand Up @@ -689,4 +698,25 @@ def fully_qualified_api_endpoint
expect(response.body["scores"]["payment_abuse"]["score"]).to eq(0.78)
end

it "Successfully submits a v205 event with WARNINGS" do
response_json =
{ :status => 0, :error_message => "OK", :warnings => warnings}
stub_request(:post, "https://api.siftscience.com/v205/events?fields=WARNINGS").
with { | request|
parsed_body = JSON.parse(request.body)
expect(parsed_body).to include("$api_key" => "overridden")
}.to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {})

api_key = "foobar"
event = "$transaction"
properties = valid_transaction_properties

response = Sift::Client.new(:api_key => api_key, :version => "205")
.track(event, properties, :api_key => "overridden",:warnings => "true")
expect(response.ok?).to eq(true)
expect(response.api_status).to eq(0)
expect(response.api_error_message).to eq("OK")
expect(response.body["warnings"]["count"]).to eq(1)
expect(response.body["warnings"]["items"][0]["message"]).to eq("Invalid currency")
end
end
2 changes: 1 addition & 1 deletion test_integration_app/events_api/test_events_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def transaction()
}
}

return @@client.track("$transaction", properties)
return @@client.track("$transaction", properties, :warnings => 'true')
end

def create_order()
Expand Down

0 comments on commit 0544b4a

Please sign in to comment.