From 625144d3af6a74b6b1031d4994161c42484cebc1 Mon Sep 17 00:00:00 2001 From: Sasha Bogolii Date: Thu, 16 May 2024 13:33:57 +0300 Subject: [PATCH 1/9] API-7683: Added support for warnings in events response --- README.md | 19 ++++++++++++++++--- lib/sift/client.rb | 12 ++++++++++-- spec/unit/client_205_spec.rb | 19 +++++++++++++++---- spec/unit/client_spec.rb | 17 ++++++++++++++--- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e1aca84..ad7c8b0 100644 --- a/README.md +++ b/README.md @@ -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). @@ -39,8 +38,22 @@ client = Sift::Client.new(api_key: '', account_id: ' 0, - :error_message => 'OK' + :error_message => 'OK', + :warnings => { + :count => 1, + :items => [{ + :message => 'Invalid currency' + }] + } } end - it "Successfully submits a v205 event with SCORE_PERCENTILES" do + it "Successfully submits a v205 event with SCORE_PERCENTILES and WARNINGS" do response_json = { :status => 0, :error_message => "OK", :score_response => percentile_response_json} - stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES&return_score=true"). + stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES,WARNINGS&return_score=true"). with { | request| parsed_body = JSON.parse(request.body) expect(parsed_body).to include("$api_key" => "overridden") @@ -108,10 +114,15 @@ def percentile_response_json properties = valid_transaction_properties response = Sift::Client.new(:api_key => api_key, :version => "205") - .track(event, properties, :api_key => "overridden", :include_score_percentiles => "true", :return_score => "true") + .track(event, properties, :api_key => "overridden", + :include_score_percentiles => "true", + :warnings => "true", + :return_score => "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["score_response"]["scores"]["account_abuse"]["percentiles"]["last_7_days"]).to eq(-1.0) + expect(response.warnings.count).to eq(1) + expect(response.warnings.items[0].message).to eq("Invalid currency") end end \ No newline at end of file diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index 5dfc8fc..47ff5c4 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -160,7 +160,13 @@ def percentile_response_json } }, :status => 0, - :error_message => 'OK' + :error_message => 'OK', + :warnings => { + :count => 1, + :items => [{ + :message => 'Invalid currency' + }] + } } end @@ -632,7 +638,7 @@ def fully_qualified_api_endpoint it "Successfully submits a v205 event with SCORE_PERCENTILES" do response_json = { :status => 0, :error_message => "OK", :score_response => percentile_response_json} - stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES&return_score=true"). + stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES,WARNINGS&return_score=true"). with { | request| parsed_body = JSON.parse(request.body) expect(parsed_body).to include("$api_key" => "overridden") @@ -643,11 +649,16 @@ def fully_qualified_api_endpoint properties = valid_transaction_properties response = Sift::Client.new(:api_key => api_key, :version => "205") - .track(event, properties, :api_key => "overridden", :include_score_percentiles => "true", :return_score => "true") + .track(event, properties, :api_key => "overridden", + :include_score_percentiles => "true", + :warnings => "true", + :return_score => "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["score_response"]["scores"]["account_abuse"]["percentiles"]["last_7_days"]).to eq(-1.0) + expect(response.warnings.count).to eq(1) + expect(response.warnings.items[0].message).to eq("Invalid currency") end it "Successfully fetches a v205 score with SCORE_PERCENTILES" do From 4c7d93de41e575587c0c1c2389a1a97571aede2b Mon Sep 17 00:00:00 2001 From: Sasha Bogolii Date: Thu, 16 May 2024 13:35:37 +0300 Subject: [PATCH 2/9] API-7683: Bumped version --- HISTORY | 3 +++ lib/sift/version.rb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/HISTORY b/HISTORY index ee2679b..1ffc45a 100644 --- a/HISTORY +++ b/HISTORY @@ -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 diff --git a/lib/sift/version.rb b/lib/sift/version.rb index 05319d6..6675b86 100644 --- a/lib/sift/version.rb +++ b/lib/sift/version.rb @@ -1,4 +1,4 @@ module Sift - VERSION = "4.4.0" + VERSION = "4.5.0" API_VERSION = "205" end From 82148e9fb18a35729f99ca6204bf561dd36caf4b Mon Sep 17 00:00:00 2001 From: Sasha Bogolii Date: Thu, 16 May 2024 14:23:16 +0300 Subject: [PATCH 3/9] API-7683: Added test --- lib/sift/client.rb | 4 ++- spec/unit/client_205_spec.rb | 13 ++------ spec/unit/client_spec.rb | 64 ++++++++++++++++++++++++++++++------ 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/lib/sift/client.rb b/lib/sift/client.rb index f7a5837..95ef0b3 100644 --- a/lib/sift/client.rb +++ b/lib/sift/client.rb @@ -17,7 +17,8 @@ class Response :api_error_message, :request, :api_error_description, - :api_error_issues + :api_error_issues, + :api_warnings # Constructor # @@ -47,6 +48,7 @@ def initialize(http_response, http_response_code, http_raw_response) @request = MultiJson.load(@body["request"].to_s) if @body["request"] @api_status = @body["status"].to_i if @body["status"] @api_error_message = @body["error_message"] + @api_warnings = MultiJson.load(@body["warnings"].to_s) if @body["warnings"] if @body["error"] @api_error_message = @body["error"] diff --git a/spec/unit/client_205_spec.rb b/spec/unit/client_205_spec.rb index 3a0f163..8ac2c01 100644 --- a/spec/unit/client_205_spec.rb +++ b/spec/unit/client_205_spec.rb @@ -90,20 +90,14 @@ def percentile_response_json } }, :status => 0, - :error_message => 'OK', - :warnings => { - :count => 1, - :items => [{ - :message => 'Invalid currency' - }] - } + :error_message => 'OK' } end it "Successfully submits a v205 event with SCORE_PERCENTILES and WARNINGS" do response_json = { :status => 0, :error_message => "OK", :score_response => percentile_response_json} - stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES,WARNINGS&return_score=true"). + stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES&return_score=true"). with { | request| parsed_body = JSON.parse(request.body) expect(parsed_body).to include("$api_key" => "overridden") @@ -116,13 +110,10 @@ def percentile_response_json response = Sift::Client.new(:api_key => api_key, :version => "205") .track(event, properties, :api_key => "overridden", :include_score_percentiles => "true", - :warnings => "true", :return_score => "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["score_response"]["scores"]["account_abuse"]["percentiles"]["last_7_days"]).to eq(-1.0) - expect(response.warnings.count).to eq(1) - expect(response.warnings.items[0].message).to eq("Invalid currency") end end \ No newline at end of file diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index 47ff5c4..f456155 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -110,6 +110,17 @@ def action_response_json } end + def warnings + { + :warnings => { + :count => 1, + :items => [{ + :message => 'Invalid currency' + }] + } + } + end + def percentile_response_json { :user_id => 'billy_jones_301', @@ -160,13 +171,7 @@ def percentile_response_json } }, :status => 0, - :error_message => 'OK', - :warnings => { - :count => 1, - :items => [{ - :message => 'Invalid currency' - }] - } + :error_message => 'OK' } end @@ -638,7 +643,7 @@ def fully_qualified_api_endpoint it "Successfully submits a v205 event with SCORE_PERCENTILES" do response_json = { :status => 0, :error_message => "OK", :score_response => percentile_response_json} - stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES,WARNINGS&return_score=true"). + stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES&return_score=true"). with { | request| parsed_body = JSON.parse(request.body) expect(parsed_body).to include("$api_key" => "overridden") @@ -657,8 +662,6 @@ def fully_qualified_api_endpoint expect(response.api_status).to eq(0) expect(response.api_error_message).to eq("OK") expect(response.body["score_response"]["scores"]["account_abuse"]["percentiles"]["last_7_days"]).to eq(-1.0) - expect(response.warnings.count).to eq(1) - expect(response.warnings.items[0].message).to eq("Invalid currency") end it "Successfully fetches a v205 score with SCORE_PERCENTILES" do @@ -699,5 +702,46 @@ def fully_qualified_api_endpoint expect(response.body["entity_id"]).to eq("247019") expect(response.body["scores"]["payment_abuse"]["score"]).to eq(0.78) end +it "Successfully submits a v205 event with SCORE_PERCENTILES" do + response_json = + { :status => 0, :error_message => "OK", :score_response => percentile_response_json} + stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES&return_score=true"). + 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", :include_score_percentiles => "true", :return_score => "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["score_response"]["scores"]["account_abuse"]["percentiles"]["last_7_days"]).to eq(-1.0) + 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.api_warnings["count"]).to eq("1") + expect(response.api_warnings["items"][0]["message"]).to eq("Invalid currency") + end end From 93564874127737303f775d3422df868015dd03ca Mon Sep 17 00:00:00 2001 From: Sasha Bogolii Date: Thu, 16 May 2024 14:40:13 +0300 Subject: [PATCH 4/9] API-7683: Fixed test --- lib/sift/client.rb | 1 - spec/unit/client_spec.rb | 21 ++++++++------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/sift/client.rb b/lib/sift/client.rb index 95ef0b3..4300e1c 100644 --- a/lib/sift/client.rb +++ b/lib/sift/client.rb @@ -48,7 +48,6 @@ def initialize(http_response, http_response_code, http_raw_response) @request = MultiJson.load(@body["request"].to_s) if @body["request"] @api_status = @body["status"].to_i if @body["status"] @api_error_message = @body["error_message"] - @api_warnings = MultiJson.load(@body["warnings"].to_s) if @body["warnings"] if @body["error"] @api_error_message = @body["error"] diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index f456155..edc2ef0 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -112,12 +112,10 @@ def action_response_json def warnings { - :warnings => { - :count => 1, - :items => [{ - :message => 'Invalid currency' - }] - } + :count => 1, + :items => [{ + :message => 'Invalid currency' + }] } end @@ -654,10 +652,7 @@ def fully_qualified_api_endpoint properties = valid_transaction_properties response = Sift::Client.new(:api_key => api_key, :version => "205") - .track(event, properties, :api_key => "overridden", - :include_score_percentiles => "true", - :warnings => "true", - :return_score => "true") + .track(event, properties, :api_key => "overridden", :include_score_percentiles => "true", :return_score => "true") expect(response.ok?).to eq(true) expect(response.api_status).to eq(0) expect(response.api_error_message).to eq("OK") @@ -725,7 +720,7 @@ def fully_qualified_api_endpoint it "Successfully submits a v205 event with WARNINGS" do response_json = - { :status => 0, :error_message => "OK", :warnings => warnings} + { :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) @@ -741,7 +736,7 @@ def fully_qualified_api_endpoint expect(response.ok?).to eq(true) expect(response.api_status).to eq(0) expect(response.api_error_message).to eq("OK") - expect(response.api_warnings["count"]).to eq("1") - expect(response.api_warnings["items"][0]["message"]).to eq("Invalid currency") + expect(response.body["warnings"]["count"]).to eq(1) + expect(response.body["warnings"]["items"][0]["message"]).to eq("Invalid currency") end end From f7a54afd21e1142ef525da735088751ef7294b13 Mon Sep 17 00:00:00 2001 From: Sasha Bogolii Date: Thu, 16 May 2024 14:52:56 +0300 Subject: [PATCH 5/9] API-7683: Fixed test --- spec/unit/client_205_spec.rb | 6 ++---- spec/unit/client_spec.rb | 20 -------------------- 2 files changed, 2 insertions(+), 24 deletions(-) diff --git a/spec/unit/client_205_spec.rb b/spec/unit/client_205_spec.rb index 8ac2c01..8778114 100644 --- a/spec/unit/client_205_spec.rb +++ b/spec/unit/client_205_spec.rb @@ -94,7 +94,7 @@ def percentile_response_json } end - it "Successfully submits a v205 event with SCORE_PERCENTILES and WARNINGS" do + it "Successfully submits a v205 event with SCORE_PERCENTILES" do response_json = { :status => 0, :error_message => "OK", :score_response => percentile_response_json} stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES&return_score=true"). @@ -108,9 +108,7 @@ def percentile_response_json properties = valid_transaction_properties response = Sift::Client.new(:api_key => api_key, :version => "205") - .track(event, properties, :api_key => "overridden", - :include_score_percentiles => "true", - :return_score => "true") + .track(event, properties, :api_key => "overridden", :include_score_percentiles => "true", :return_score => "true") expect(response.ok?).to eq(true) expect(response.api_status).to eq(0) expect(response.api_error_message).to eq("OK") diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index edc2ef0..f1c9d3b 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -697,26 +697,6 @@ def fully_qualified_api_endpoint expect(response.body["entity_id"]).to eq("247019") expect(response.body["scores"]["payment_abuse"]["score"]).to eq(0.78) end -it "Successfully submits a v205 event with SCORE_PERCENTILES" do - response_json = - { :status => 0, :error_message => "OK", :score_response => percentile_response_json} - stub_request(:post, "https://api.siftscience.com/v205/events?fields=SCORE_PERCENTILES&return_score=true"). - 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", :include_score_percentiles => "true", :return_score => "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["score_response"]["scores"]["account_abuse"]["percentiles"]["last_7_days"]).to eq(-1.0) - end it "Successfully submits a v205 event with WARNINGS" do response_json = From 7707cd576cb47b55e8e3336f0e86b6ded99fccbe Mon Sep 17 00:00:00 2001 From: Sasha Bogolii Date: Thu, 16 May 2024 14:53:36 +0300 Subject: [PATCH 6/9] API-7683: Fixed test --- lib/sift/client.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/sift/client.rb b/lib/sift/client.rb index 4300e1c..f7a5837 100644 --- a/lib/sift/client.rb +++ b/lib/sift/client.rb @@ -17,8 +17,7 @@ class Response :api_error_message, :request, :api_error_description, - :api_error_issues, - :api_warnings + :api_error_issues # Constructor # From 27340e00d8073f1082598840289d1c0fb47fd506 Mon Sep 17 00:00:00 2001 From: Sasha Bogolii Date: Thu, 16 May 2024 14:54:19 +0300 Subject: [PATCH 7/9] API-7683: Updated README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index ad7c8b0..322cec9 100644 --- a/README.md +++ b/README.md @@ -306,7 +306,6 @@ All requests to our apis will return a `Response` instance. - `api_error_message` returns a string describing the api error code. - `api_error_description` a summary of the error that occured. - `api_error_issues` a hash describing the items the error occured. The `key` is the item and the `value` is the description of the error. -- `warnings` returns list of warnings if requested. ## Building From 2d77c6651a1f9e19b168d8512b35f07a9d88bde8 Mon Sep 17 00:00:00 2001 From: Sasha Bogolii Date: Thu, 16 May 2024 14:54:36 +0300 Subject: [PATCH 8/9] API-7683: Updated README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 322cec9..274ffa1 100644 --- a/README.md +++ b/README.md @@ -307,6 +307,7 @@ All requests to our apis will return a `Response` instance. - `api_error_description` a summary of the error that occured. - `api_error_issues` a hash describing the items the error occured. The `key` is the item and the `value` is the description of the error. + ## Building Building and publishing the gem is captured by the following steps: From 009f91ce98a02c1d3d4eadb281a45b89be946a0f Mon Sep 17 00:00:00 2001 From: Sasha Bogolii Date: Thu, 16 May 2024 15:37:45 +0300 Subject: [PATCH 9/9] API-7683: Updated integration app --- test_integration_app/events_api/test_events_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_integration_app/events_api/test_events_api.rb b/test_integration_app/events_api/test_events_api.rb index c0759ac..299b39d 100644 --- a/test_integration_app/events_api/test_events_api.rb +++ b/test_integration_app/events_api/test_events_api.rb @@ -122,7 +122,7 @@ def transaction() } } - return @@client.track("$transaction", properties) + return @@client.track("$transaction", properties, :warnings => 'true') end def create_order()