sift 1.1.7.3 → 2.0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
1
  module Sift
2
- VERSION = "1.1.7.3"
3
- API_VERSION = "203"
2
+ VERSION = "2.0.0.0"
3
+ API_VERSION = "204"
4
4
  end
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.name = "sift"
7
7
  s.version = Sift::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Fred Sadaghiani", "Yoav Schatzberg"]
9
+ s.authors = ["Fred Sadaghiani", "Yoav Schatzberg", "Jacob Burnim"]
10
10
  s.email = ["support@siftscience.com"]
11
11
  s.homepage = "http://siftscience.com"
12
12
  s.summary = %q{Sift Science Ruby API Gem}
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
21
21
 
22
22
  # Gems that must be intalled for sift to compile and build
23
23
  s.add_development_dependency "rspec", ">=2.14.1"
24
- s.add_development_dependency "webmock", ">= 1.16.0"
24
+ s.add_development_dependency "webmock", ">= 1.16.0", "< 2"
25
25
 
26
26
  # Gems that must be intalled for sift to work
27
27
  s.add_dependency "httparty", ">= 0.11.0"
@@ -0,0 +1,192 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+
3
+ describe Sift::Client do
4
+
5
+ before :each do
6
+ Sift.api_key = nil
7
+ end
8
+
9
+ def valid_transaction_properties
10
+ {
11
+ :$buyer_user_id => "123456",
12
+ :$seller_user_id => "654321",
13
+ :$amount => 1253200,
14
+ :$currency_code => "USD",
15
+ :$time => Time.now.to_i,
16
+ :$transaction_id => "my_transaction_id",
17
+ :$billing_name => "Mike Snow",
18
+ :$billing_bin => "411111",
19
+ :$billing_last4 => "1111",
20
+ :$billing_address1 => "123 Main St.",
21
+ :$billing_city => "San Francisco",
22
+ :$billing_region => "CA",
23
+ :$billing_country => "US",
24
+ :$billing_zip => "94131",
25
+ :$user_email => "mike@example.com"
26
+ }
27
+ end
28
+
29
+ def score_response_json
30
+ {
31
+ :user_id => "247019",
32
+ :score => 0.93,
33
+ :reasons => [{
34
+ :name => "UsersPerDevice",
35
+ :value => 4,
36
+ :details => {
37
+ :users => "a, b, c, d"
38
+ }
39
+ }],
40
+ :status => 0,
41
+ :error_message => "OK"
42
+ }
43
+ end
44
+
45
+ def action_response_json
46
+ {
47
+ :user_id => "247019",
48
+ :score => 0.93,
49
+ :actions => [{
50
+ :action_id => "1234567890abcdefghijklmn",
51
+ :time => 1437421587052,
52
+ :triggers => [{
53
+ :triggerType => "FORMULA",
54
+ :source => "synchronous_action",
55
+ :trigger_id => "12345678900987654321abcd"
56
+ }],
57
+ :entity => {
58
+ :type => "USER",
59
+ :id => "23056"
60
+ }
61
+ },
62
+ {
63
+ :action_id => "12345678901234567890abcd",
64
+ :time => 1437421587410,
65
+ :triggers => [{
66
+ :triggerType => "FORMULA",
67
+ :source => "synchronous_action",
68
+ :trigger_id => "abcd12345678901234567890"
69
+ }],
70
+ :entity => {
71
+ :type => "ORDER",
72
+ :id => "order_at_ 1437421587009"
73
+ }
74
+ }],
75
+ :status => 0,
76
+ :error_message => "OK"
77
+ }
78
+ end
79
+
80
+ def fully_qualified_api_endpoint
81
+ Sift::Client::API_ENDPOINT + Sift.rest_api_path
82
+ end
83
+
84
+ it "Successfully submits a v203 event with overridden key" do
85
+ response_json = { :status => 0, :error_message => "OK"}
86
+ stub_request(:post, "https://api.siftscience.com/v203/events").
87
+ with { | request|
88
+ parsed_body = JSON.parse(request.body)
89
+ expect(parsed_body).to include("$buyer_user_id" => "123456")
90
+ expect(parsed_body).to include("$api_key" => "overridden")
91
+ }.to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {})
92
+
93
+ api_key = "foobar"
94
+ event = "$transaction"
95
+ properties = valid_transaction_properties
96
+
97
+ response = Sift::Client.new(:api_key => api_key, :version => "203")
98
+ .track(event, properties, :api_key => "overridden")
99
+ expect(response.ok?).to eq(true)
100
+ expect(response.api_status).to eq(0)
101
+ expect(response.api_error_message).to eq("OK")
102
+ end
103
+
104
+
105
+ it "Successfully fetches a v203 score" do
106
+
107
+ api_key = "foobar"
108
+ response_json = score_response_json
109
+
110
+ stub_request(:get, "https://api.siftscience.com/v203/score/247019/?api_key=foobar")
111
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
112
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
113
+ "content-length"=> "74"})
114
+
115
+ response = Sift::Client.new(:api_key => api_key)
116
+ .score(score_response_json[:user_id], :version => 203)
117
+ expect(response.ok?).to eq(true)
118
+ expect(response.api_status).to eq(0)
119
+ expect(response.api_error_message).to eq("OK")
120
+
121
+ expect(response.body["score"]).to eq(0.93)
122
+ end
123
+
124
+
125
+ it "Successfully fetches a v203 score with an overridden key" do
126
+
127
+ api_key = "foobar"
128
+ response_json = score_response_json
129
+
130
+ stub_request(:get, "https://api.siftscience.com/v203/score/247019/?api_key=overridden")
131
+ .to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {})
132
+
133
+ response = Sift::Client.new(:api_key => api_key, :version => 203)
134
+ .score(score_response_json[:user_id], :api_key => "overridden")
135
+ expect(response.ok?).to eq(true)
136
+ expect(response.api_status).to eq(0)
137
+ expect(response.api_error_message).to eq("OK")
138
+
139
+ expect(response.body["score"]).to eq(0.93)
140
+ end
141
+
142
+
143
+ it "Successfuly make a v203 sync score request" do
144
+
145
+ api_key = "foobar"
146
+ response_json = {
147
+ :status => 0,
148
+ :error_message => "OK",
149
+ :score_response => score_response_json
150
+ }
151
+
152
+ stub_request(:post, "https://api.siftscience.com/v203/events?return_score=true")
153
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
154
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
155
+ "content-length"=> "74"})
156
+
157
+ event = "$transaction"
158
+ properties = valid_transaction_properties
159
+ response = Sift::Client.new(:api_key => api_key)
160
+ .track(event, properties, :return_score => true, :version => "203")
161
+ expect(response.ok?).to eq(true)
162
+ expect(response.api_status).to eq(0)
163
+ expect(response.api_error_message).to eq("OK")
164
+ expect(response.body["score_response"]["score"]).to eq(0.93)
165
+ end
166
+
167
+
168
+ it "Successfuly make a v203 sync action request" do
169
+
170
+ api_key = "foobar"
171
+ response_json = {
172
+ :status => 0,
173
+ :error_message => "OK",
174
+ :score_response => action_response_json
175
+ }
176
+
177
+ stub_request(:post, "https://api.siftscience.com/v203/events?return_action=true")
178
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
179
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
180
+ "content-length"=> "74"})
181
+
182
+ event = "$transaction"
183
+ properties = valid_transaction_properties
184
+ response = Sift::Client.new(:api_key => api_key, :version => "203")
185
+ .track(event, properties, :return_action => true)
186
+ expect(response.ok?).to eq(true)
187
+ expect(response.api_status).to eq(0)
188
+ expect(response.api_error_message).to eq("OK")
189
+ expect(response.body["score_response"]["actions"].first["entity"]["type"]).to eq("USER")
190
+ end
191
+
192
+ end
@@ -3,6 +3,14 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
3
3
  describe Sift::Client do
4
4
 
5
5
  def valid_label_properties
6
+ {
7
+ :$abuse_type => 'content_abuse',
8
+ :$is_bad => true,
9
+ :$description => "Listed a fake item"
10
+ }
11
+ end
12
+
13
+ def valid_label_properties_203
6
14
  {
7
15
  :$reasons => [ "$fake" ],
8
16
  :$is_bad => true,
@@ -10,39 +18,75 @@ describe Sift::Client do
10
18
  }
11
19
  end
12
20
 
13
- def fully_qualified_users_labels_endpoint(user_id)
14
- Sift::Client::API_ENDPOINT + Sift.current_users_label_api_path(user_id)
21
+
22
+ it "Successfuly handles a $label and returns OK" do
23
+
24
+ response_json = { :status => 0, :error_message => "OK" }
25
+ user_id = "frodo_baggins"
26
+
27
+ stub_request(:post, "https://api.siftscience.com/v204/users/frodo_baggins/labels")
28
+ .with(:body => ('{"$abuse_type":"content_abuse","$is_bad":true,"$description":"Listed a fake item","$type":"$label","$api_key":"foobar"}'))
29
+ .to_return(:body => MultiJson.dump(response_json), :status => 200,
30
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
31
+ "content-length"=> "74"})
32
+
33
+ api_key = "foobar"
34
+ properties = valid_label_properties
35
+
36
+ response = Sift::Client.new(:api_key => api_key).label(user_id, properties)
37
+ expect(response.ok?).to eq(true)
38
+ expect(response.api_status).to eq(0)
39
+ expect(response.api_error_message).to eq("OK")
15
40
  end
16
41
 
42
+
43
+ it "Successfully handles an $unlabel and returns OK" do
44
+ response_json = { :status => 0, :error_message => "OK" }
45
+ user_id = "frodo_baggins"
46
+
47
+ stub_request(:delete,
48
+ "https://api.siftscience.com/v204/users/frodo_baggins/labels?api_key=foobar&abuse_type=payment_abuse")
49
+ .to_return(:status => 204)
50
+
51
+ api_key = "foobar"
52
+
53
+ response = Sift::Client.new(:api_key => api_key).unlabel(user_id, :abuse_type => 'payment_abuse')
54
+ expect(response.ok?).to eq(true)
55
+ end
56
+
57
+
17
58
  it "Successfuly handles a $label with the v203 API and returns OK" do
18
59
 
19
60
  response_json = { :status => 0, :error_message => "OK" }
20
61
  user_id = "frodo_baggins"
21
62
 
22
- stub_request(:post, "https://api.siftscience.com/v203/users/frodo_baggins/labels").
23
- with(:body => '{"$reasons":["$fake"],"$is_bad":true,"$description":"Listed a fake item","$type":"$label","$api_key":"foobar"}').
24
- to_return(:body => MultiJson.dump(response_json), :status => 200, :headers =>
25
- {"content-type"=>"application/json; charset=UTF-8","content-length"=> "74"})
63
+ stub_request(:post, "https://api.siftscience.com/v203/users/frodo_baggins/labels")
64
+ .with(:body => ('{"$reasons":["$fake"],"$is_bad":true,"$description":"Listed a fake item","$type":"$label","$api_key":"foobar"}'))
65
+ .to_return(:body => MultiJson.dump(response_json), :status => 200,
66
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
67
+ "content-length"=> "74"})
26
68
 
27
69
  api_key = "foobar"
28
- properties = valid_label_properties
70
+ properties = valid_label_properties_203
29
71
 
30
- response = Sift::Client.new(api_key).label(user_id, properties)
72
+ response = Sift::Client.new(:api_key => api_key, :version => 203).label(user_id, properties)
31
73
  expect(response.ok?).to eq(true)
32
74
  expect(response.api_status).to eq(0)
33
75
  expect(response.api_error_message).to eq("OK")
34
76
  end
35
77
 
78
+
36
79
  it "Successfully handles an $unlabel with the v203 API endpoing and returns OK" do
37
80
  response_json = { :status => 0, :error_message => "OK" }
38
81
  user_id = "frodo_baggins"
39
82
 
40
- stub_request(:delete, "https://api.siftscience.com/v203/users/frodo_baggins/labels?api_key=foobar").
41
- to_return(:status => 204)
83
+ stub_request(:delete,
84
+ "https://api.siftscience.com/v203/users/frodo_baggins/labels?api_key=foobar")
85
+ .to_return(:status => 204)
42
86
 
43
87
  api_key = "foobar"
44
88
 
45
- response = Sift::Client.new(api_key).unlabel(user_id)
89
+ response = Sift::Client.new(:api_key => api_key).unlabel(user_id, :version => "203")
46
90
  expect(response.ok?).to eq(true)
47
91
  end
48
92
 
@@ -78,60 +78,69 @@ describe Sift::Client do
78
78
  end
79
79
 
80
80
  def fully_qualified_api_endpoint
81
- Sift::Client::API_ENDPOINT + Sift.current_rest_api_path
81
+ Sift::Client::API_ENDPOINT + Sift.rest_api_path
82
82
  end
83
83
 
84
+
84
85
  it "Can instantiate client with blank api key if Sift.api_key set" do
85
86
  Sift.api_key = "test_global_api_key"
86
87
  expect(Sift::Client.new().api_key).to eq(Sift.api_key)
87
88
  end
88
89
 
90
+
89
91
  it "Parameter passed api key takes precedence over Sift.api_key" do
90
92
  Sift.api_key = "test_global_api_key"
91
93
  api_key = "test_local_api_key"
92
- expect(Sift::Client.new(api_key).api_key).to eq(api_key)
94
+ expect(Sift::Client.new(:api_key => api_key).api_key).to eq(api_key)
93
95
  end
94
96
 
97
+
95
98
  it "Cannot instantiate client with nil, empty, non-string, or blank api key" do
96
- expect(lambda { Sift::Client.new(nil) }).to raise_error(StandardError)
97
- expect(lambda { Sift::Client.new("") }).to raise_error(StandardError)
98
- expect(lambda { Sift::Client.new(123456) }).to raise_error(StandardError)
99
+ expect(lambda { Sift::Client.new(:api_key => nil) }).to raise_error(StandardError)
100
+ expect(lambda { Sift::Client.new(:api_key => "") }).to raise_error(StandardError)
101
+ expect(lambda { Sift::Client.new(:api_key => 123456) }).to raise_error(StandardError)
99
102
  expect(lambda { Sift::Client.new() }).to raise_error(StandardError)
100
103
  end
101
104
 
102
- it "Cannot instantiate client with nil, empty, non-string, or blank path" do
105
+
106
+ it "Cannot instantiate client with empty, non-string, or blank path" do
103
107
  api_key = "test_local_api_key"
104
- expect(lambda { Sift::Client.new(api_key, nil) }).to raise_error(StandardError)
105
- expect(lambda { Sift::Client.new(api_key, "") }).to raise_error(StandardError)
106
- expect(lambda { Sift::Client.new(api_key, 123456) }).to raise_error(StandardError)
108
+ expect(lambda { Sift::Client.new(:path => "") }).to raise_error(StandardError)
109
+ expect(lambda { Sift::Client.new(:path => 123456) }).to raise_error(StandardError)
107
110
  end
108
111
 
112
+
109
113
  it "Can instantiate client with non-default timeout" do
110
- expect(lambda { Sift::Client.new("test_local_api_key", Sift.current_rest_api_path, 4) }).not_to raise_error
114
+ expect(lambda { Sift::Client.new(:api_key => "foo", :timeout => 4) })
115
+ .not_to raise_error
111
116
  end
112
117
 
118
+
113
119
  it "Track call must specify an event name" do
114
- expect(lambda { Sift::Client.new("foo").track(nil) }).to raise_error(StandardError)
115
- expect(lambda { Sift::Client.new("foo").track("") }).to raise_error(StandardError)
120
+ expect(lambda { Sift::Client.new().track(nil) }).to raise_error(StandardError)
121
+ expect(lambda { Sift::Client.new().track("") }).to raise_error(StandardError)
116
122
  end
117
123
 
124
+
118
125
  it "Must specify an event name" do
119
- expect(lambda { Sift::Client.new("foo").track(nil) }).to raise_error(StandardError)
120
- expect(lambda { Sift::Client.new("foo").track("") }).to raise_error(StandardError)
126
+ expect(lambda { Sift::Client.new().track(nil) }).to raise_error(StandardError)
127
+ expect(lambda { Sift::Client.new().track("") }).to raise_error(StandardError)
121
128
  end
122
129
 
130
+
123
131
  it "Must specify properties" do
124
132
  event = "custom_event_name"
125
- expect(lambda { Sift::Client.new("foo").track(event) }).to raise_error(StandardError)
133
+ expect(lambda { Sift::Client.new().track(event) }).to raise_error(StandardError)
126
134
  end
127
135
 
136
+
128
137
  it "Score call must specify a user_id" do
129
- expect(lambda { Sift::Client.new("foo").score(nil) }).to raise_error(StandardError)
130
- expect(lambda { Sift::Client.new("foo").score("") }).to raise_error(StandardError)
138
+ expect(lambda { Sift::Client.new().score(nil) }).to raise_error(StandardError)
139
+ expect(lambda { Sift::Client.new().score("") }).to raise_error(StandardError)
131
140
  end
132
141
 
133
- it "Doesn't raise an exception on Net/HTTP errors" do
134
142
 
143
+ it "Doesn't raise an exception on Net/HTTP errors" do
135
144
  api_key = "foobar"
136
145
  event = "$transaction"
137
146
  properties = valid_transaction_properties
@@ -140,11 +149,11 @@ describe Sift::Client do
140
149
 
141
150
  # This method should just return nil -- the track call failed because
142
151
  # of an HTTP error
143
- expect(Sift::Client.new(api_key).track(event, properties)).to eq(nil)
152
+ expect(Sift::Client.new(:api_key => api_key).track(event, properties)).to eq(nil)
144
153
  end
145
154
 
146
- it "Returns nil when a StandardError occurs within the request" do
147
155
 
156
+ it "Returns nil when a StandardError occurs within the request" do
148
157
  api_key = "foobar"
149
158
  event = "$transaction"
150
159
  properties = valid_transaction_properties
@@ -153,32 +162,35 @@ describe Sift::Client do
153
162
 
154
163
  # This method should just return nil -- the track call failed because
155
164
  # a StandardError exception was thrown
156
- expect(Sift::Client.new(api_key).track(event, properties)).to eq(nil)
165
+ expect(Sift::Client.new(:api_key => api_key).track(event, properties)).to eq(nil)
157
166
  end
158
167
 
159
- it "Successfuly handles an event and returns OK" do
160
168
 
169
+ it "Successfuly handles an event and returns OK" do
161
170
  response_json = { :status => 0, :error_message => "OK" }
162
171
 
163
- stub_request(:post, "https://api.siftscience.com/v203/events").
172
+ stub_request(:post, "https://api.siftscience.com/v204/events").
164
173
  with { |request|
165
174
  parsed_body = JSON.parse(request.body)
166
175
  expect(parsed_body).to include("$buyer_user_id" => "123456")
167
- }.to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {"content-type"=>"application/json; charset=UTF-8","content-length"=> "74"})
176
+ }.to_return(:status => 200, :body => MultiJson.dump(response_json),
177
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
178
+ "content-length"=> "74"})
168
179
 
169
180
  api_key = "foobar"
170
181
  event = "$transaction"
171
182
  properties = valid_transaction_properties
172
183
 
173
- response = Sift::Client.new(api_key).track(event, properties)
184
+ response = Sift::Client.new(:api_key => api_key).track(event, properties)
174
185
  expect(response.ok?).to eq(true)
175
186
  expect(response.api_status).to eq(0)
176
187
  expect(response.api_error_message).to eq("OK")
177
188
  end
178
189
 
190
+
179
191
  it "Successfully submits event with overridden key" do
180
192
  response_json = { :status => 0, :error_message => "OK"}
181
- stub_request(:post, "https://api.siftscience.com/v203/events").
193
+ stub_request(:post, "https://api.siftscience.com/v204/events").
182
194
  with { | request|
183
195
  parsed_body = JSON.parse(request.body)
184
196
  expect(parsed_body).to include("$buyer_user_id" => "123456")
@@ -189,22 +201,25 @@ describe Sift::Client do
189
201
  event = "$transaction"
190
202
  properties = valid_transaction_properties
191
203
 
192
- response = Sift::Client.new(api_key).track(event, properties, nil, nil, false, "overridden", false)
204
+ response = Sift::Client.new(:api_key => api_key)
205
+ .track(event, properties, :api_key => "overridden")
193
206
  expect(response.ok?).to eq(true)
194
207
  expect(response.api_status).to eq(0)
195
208
  expect(response.api_error_message).to eq("OK")
196
209
  end
197
210
 
198
- it "Successfuly scrubs nils" do
199
211
 
212
+ it "Successfully scrubs nils" do
200
213
  response_json = { :status => 0, :error_message => "OK" }
201
214
 
202
- stub_request(:post, "https://api.siftscience.com/v203/events").
203
- with { |request|
215
+ stub_request(:post, "https://api.siftscience.com/v204/events")
216
+ .with { |request|
204
217
  parsed_body = JSON.parse(request.body)
205
218
  expect(parsed_body).not_to include("fake_property")
206
219
  expect(parsed_body).to include("sub_object" => {"one" => "two"})
207
- }.to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {"content-type"=>"application/json; charset=UTF-8","content-length"=> "74"})
220
+ }.to_return(:status => 200, :body => MultiJson.dump(response_json),
221
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
222
+ "content-length"=> "74"})
208
223
 
209
224
  api_key = "foobar"
210
225
  event = "$transaction"
@@ -215,21 +230,23 @@ describe Sift::Client do
215
230
  "three" => nil
216
231
  }
217
232
  )
218
- response = Sift::Client.new(api_key).track(event, properties)
233
+ response = Sift::Client.new(:api_key => api_key).track(event, properties)
219
234
  expect(response.ok?).to eq(true)
220
235
  expect(response.api_status).to eq(0)
221
236
  expect(response.api_error_message).to eq("OK")
222
237
  end
223
238
 
224
- it "Successfully fetches a score" do
225
239
 
240
+ it "Successfully fetches a score" do
226
241
  api_key = "foobar"
227
242
  response_json = score_response_json
228
243
 
229
- stub_request(:get, "https://api.siftscience.com/v203/score/247019/?api_key=foobar").
230
- to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {"content-type"=>"application/json; charset=UTF-8","content-length"=> "74"})
244
+ stub_request(:get, "https://api.siftscience.com/v204/score/247019/?api_key=foobar")
245
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
246
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
247
+ "content-length"=> "74"})
231
248
 
232
- response = Sift::Client.new(api_key).score(score_response_json[:user_id])
249
+ response = Sift::Client.new(:api_key => api_key).score(score_response_json[:user_id])
233
250
  expect(response.ok?).to eq(true)
234
251
  expect(response.api_status).to eq(0)
235
252
  expect(response.api_error_message).to eq("OK")
@@ -237,15 +254,16 @@ describe Sift::Client do
237
254
  expect(response.body["score"]).to eq(0.93)
238
255
  end
239
256
 
240
- it "Successfully fetches a score with an overridden key" do
241
257
 
258
+ it "Successfully fetches a score with an overridden key" do
242
259
  api_key = "foobar"
243
260
  response_json = score_response_json
244
261
 
245
- stub_request(:get, "https://api.siftscience.com/v203/score/247019/?api_key=overridden").
246
- to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {})
262
+ stub_request(:get, "https://api.siftscience.com/v204/score/247019/?api_key=overridden")
263
+ .to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {})
247
264
 
248
- response = Sift::Client.new(api_key).score(score_response_json[:user_id], nil, "overridden")
265
+ response = Sift::Client.new(:api_key => api_key)
266
+ .score(score_response_json[:user_id], :api_key => "overridden")
249
267
  expect(response.ok?).to eq(true)
250
268
  expect(response.api_status).to eq(0)
251
269
  expect(response.api_error_message).to eq("OK")
@@ -254,8 +272,7 @@ describe Sift::Client do
254
272
  end
255
273
 
256
274
 
257
- it "Successfuly make a sync score request" do
258
-
275
+ it "Successfully make a sync score request" do
259
276
  api_key = "foobar"
260
277
  response_json = {
261
278
  :status => 0,
@@ -263,20 +280,23 @@ describe Sift::Client do
263
280
  :score_response => score_response_json
264
281
  }
265
282
 
266
- stub_request(:post, "https://api.siftscience.com/v203/events?return_score=true").
267
- to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {"content-type"=>"application/json; charset=UTF-8","content-length"=> "74"})
283
+ stub_request(:post, "https://api.siftscience.com/v204/events?return_score=true")
284
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
285
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
286
+ "content-length"=> "74"})
268
287
 
269
288
  event = "$transaction"
270
289
  properties = valid_transaction_properties
271
- response = Sift::Client.new(api_key).track(event, properties, nil, nil, true, nil, nil)
290
+ response = Sift::Client.new(:api_key => api_key)
291
+ .track(event, properties, :return_score => true)
272
292
  expect(response.ok?).to eq(true)
273
293
  expect(response.api_status).to eq(0)
274
294
  expect(response.api_error_message).to eq("OK")
275
295
  expect(response.body["score_response"]["score"]).to eq(0.93)
276
296
  end
277
297
 
278
- it "Successfuly make a sync action request" do
279
298
 
299
+ it "Successfully make a sync action request" do
280
300
  api_key = "foobar"
281
301
  response_json = {
282
302
  :status => 0,
@@ -284,12 +304,15 @@ describe Sift::Client do
284
304
  :score_response => action_response_json
285
305
  }
286
306
 
287
- stub_request(:post, "https://api.siftscience.com/v203/events?return_action=true").
288
- to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {"content-type"=>"application/json; charset=UTF-8","content-length"=> "74"})
307
+ stub_request(:post, "https://api.siftscience.com/v204/events?return_action=true")
308
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
309
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
310
+ "content-length"=> "74"})
289
311
 
290
312
  event = "$transaction"
291
313
  properties = valid_transaction_properties
292
- response = Sift::Client.new(api_key).track(event, properties, nil, nil, nil, nil, true)
314
+ response = Sift::Client.new(:api_key => api_key)
315
+ .track(event, properties, :return_action => true)
293
316
  expect(response.ok?).to eq(true)
294
317
  expect(response.api_status).to eq(0)
295
318
  expect(response.api_error_message).to eq("OK")
@@ -297,5 +320,74 @@ describe Sift::Client do
297
320
  end
298
321
 
299
322
 
323
+ it "Successfully make a sync workflow request" do
324
+ api_key = "foobar"
325
+ response_json = {
326
+ :status => 0,
327
+ :error_message => "OK",
328
+ :score_response => {
329
+ :status => -1,
330
+ :error_message => "Internal server error."
331
+ }
332
+ }
333
+
334
+ stub_request(:post,
335
+ "https://api.siftscience.com/v204/events?return_workflow_status=true&abuse_types=legacy,payment_abuse")
336
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
337
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
338
+ "content-length"=> "74"})
339
+
340
+ event = "$transaction"
341
+ properties = valid_transaction_properties
342
+ response = Sift::Client.new(:api_key => api_key)
343
+ .track(event, properties,
344
+ :return_workflow_status => true, :abuse_types => ['legacy', 'payment_abuse'])
345
+ expect(response.ok?).to eq(true)
346
+ expect(response.api_status).to eq(0)
347
+ expect(response.api_error_message).to eq("OK")
348
+ end
349
+
350
+
351
+ it "Successfully make a workflow status request" do
352
+ response_text = '{"id":"skdjfnkse","config":{"id":"5rrbr4iaaa","version":"1468367620871"},"config_display_name":"workflow config","abuse_types":["payment_abuse"],"state":"running","entity":{"id":"example_user","type":"user"},"history":[{"app":"user","name":"Entity","state":"finished","config":{}}]}'
353
+
354
+ stub_request(:get, "https://foobar:@api3.siftscience.com/v3/accounts/ACCT/workflows/runs/skdjfnkse")
355
+ .to_return(:status => 200, :body => response_text, :headers => {})
356
+
357
+ client = Sift::Client.new(:api_key => "foobar", :account_id => "ACCT")
358
+ response = client.get_workflow_status("skdjfnkse")
359
+
360
+ expect(response.ok?).to eq(true)
361
+ expect(response.body["id"]).to eq("skdjfnkse")
362
+ expect(response.body["state"]).to eq("running")
363
+ end
364
+
365
+
366
+ it "Successfully make a user decisions request" do
367
+ response_text = '{"decisions":{"content_abuse":{"decision":{"id":"user_decision"},"time":1468707128659,"webhook_succeeded":false}}}'
368
+
369
+ stub_request(:get, "https://foobar:@api3.siftscience.com/v3/accounts/ACCT/users/example_user/decisions")
370
+ .to_return(:status => 200, :body => response_text, :headers => {})
371
+
372
+ client = Sift::Client.new(:api_key => "foobar", :account_id => "ACCT")
373
+ response = client.get_user_decisions("example_user")
374
+
375
+ expect(response.ok?).to eq(true)
376
+ expect(response.body["decisions"]["content_abuse"]["decision"]["id"]).to eq("user_decision")
377
+ end
378
+
379
+
380
+ it "Successfully make an order decisions request" do
381
+ response_text = '{"decisions":{"payment_abuse":{"decision":{"id":"decision7"},"time":1468599638005,"webhook_succeeded":false},"promotion_abuse":{"decision":{"id":"good_order"},"time":1468517407135,"webhook_succeeded":true}}}'
382
+
383
+ stub_request(:get, "https://foobar:@api3.siftscience.com/v3/accounts/ACCT/orders/example_order/decisions")
384
+ .to_return(:status => 200, :body => response_text, :headers => {})
385
+
386
+ client = Sift::Client.new(:api_key => "foobar", :account_id => "ACCT")
387
+ response = client.get_order_decisions("example_order", :timeout => 3)
388
+
389
+ expect(response.ok?).to eq(true)
390
+ expect(response.body["decisions"]["payment_abuse"]["decision"]["id"]).to eq("decision7")
391
+ end
300
392
 
301
393
  end