sift 2.0.0.0 → 2.1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
1
+ require_relative "../spec_helper"
2
+ require "sift"
2
3
 
3
4
  describe Sift::Client do
4
5
 
@@ -1,4 +1,5 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
1
+ require_relative "../spec_helper"
2
+ require "sift"
2
3
 
3
4
  describe Sift::Client do
4
5
 
@@ -41,7 +42,6 @@ describe Sift::Client do
41
42
 
42
43
 
43
44
  it "Successfully handles an $unlabel and returns OK" do
44
- response_json = { :status => 0, :error_message => "OK" }
45
45
  user_id = "frodo_baggins"
46
46
 
47
47
  stub_request(:delete,
@@ -77,7 +77,6 @@ describe Sift::Client do
77
77
 
78
78
 
79
79
  it "Successfully handles an $unlabel with the v203 API endpoing and returns OK" do
80
- response_json = { :status => 0, :error_message => "OK" }
81
80
  user_id = "frodo_baggins"
82
81
 
83
82
  stub_request(:delete,
@@ -1,4 +1,5 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
1
+ require_relative "../spec_helper"
2
+ require "sift"
2
3
 
3
4
  describe Sift::Client do
4
5
 
@@ -104,7 +105,6 @@ describe Sift::Client do
104
105
 
105
106
 
106
107
  it "Cannot instantiate client with empty, non-string, or blank path" do
107
- api_key = "test_local_api_key"
108
108
  expect(lambda { Sift::Client.new(:path => "") }).to raise_error(StandardError)
109
109
  expect(lambda { Sift::Client.new(:path => 123456) }).to raise_error(StandardError)
110
110
  end
@@ -373,7 +373,7 @@ describe Sift::Client do
373
373
  response = client.get_user_decisions("example_user")
374
374
 
375
375
  expect(response.ok?).to eq(true)
376
- expect(response.body["decisions"]["content_abuse"]["decision"]["id"]).to eq("user_decision")
376
+ expect(response.body["decisions"]["content_abuse"]["decision"]["id"]).to eq("user_decision")
377
377
  end
378
378
 
379
379
 
@@ -0,0 +1,37 @@
1
+ require_relative "../spec_helper"
2
+
3
+ require "sift/router"
4
+
5
+ module Sift
6
+ describe Router do
7
+ let(:path) { "https://example.com" }
8
+ let(:body) { { question: "Do you wanna go ball?" } }
9
+
10
+ describe ".get" do
11
+ it "with a successful request will return a response object" do
12
+ stub_request(:get, path)
13
+ .with(body: MultiJson.dump(body))
14
+ .to_return(body: MultiJson.dump({ cool: true }))
15
+
16
+ response = Router.get(path, { body: body })
17
+
18
+ expect(response.ok?).to be(true)
19
+ expect(response.body["cool"]).to be(true)
20
+ end
21
+
22
+ it "with an unsuccessful request will return a response object" do
23
+ stub_request(:get, path)
24
+ .with(body: MultiJson.dump(body))
25
+ .to_return(body: MultiJson.dump({ cool: false}), status: 403)
26
+
27
+ response = Router.get(path, { body: body })
28
+
29
+ expect(response.ok?).to be(false)
30
+ expect(response.body["cool"]).to be(false)
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+
37
+
@@ -0,0 +1,85 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ require "sift/validate/decision"
4
+
5
+ module Sift
6
+ module Validate
7
+ describe Decision do
8
+ describe "#valid_user?" do
9
+ it "will return true" do
10
+ validator = Decision.new(
11
+ user_id: "hello world",
12
+ source: :hello_world,
13
+ analyst: "asdfsadf@heyo.com"
14
+ )
15
+
16
+ expect(validator.valid_user?).to be(true)
17
+ end
18
+
19
+ it "with invalid user_id, will return false" do
20
+ validator = Decision.new(order_id: "asfasdf")
21
+ expect(validator.valid_user?).to be(false), "nil user is valid"
22
+
23
+ validator = Decision.new(order_id: "asfasdf", user_id: {})
24
+ expect(validator.valid_user?).to be(false), "user hash is valid"
25
+
26
+ validator = Decision.new(order_id: "asfasdf", user_id: /werwer/)
27
+ expect(validator.valid_user?).to be(false), "regex user is valid"
28
+ end
29
+ end
30
+
31
+ describe "#valid_order?" do
32
+ it "will return true with valid configs" do
33
+ validator = Decision.new(
34
+ order_id: "order_foo_bar_12354",
35
+ user_id: "hello world",
36
+ source: :hello_world,
37
+ analyst: "asdfsadf@heyo.com"
38
+ )
39
+
40
+ expect(validator.valid_order?).to be(true)
41
+ end
42
+
43
+ context "with invalid params:" do
44
+ it "user_id, will return false" do
45
+ validator = Decision.new(order_id: "asfasdf")
46
+ expect(validator.valid_order?).to be(false), "nil user is valid"
47
+
48
+ validator = Decision.new(order_id: "asfasdf", user_id: {})
49
+ expect(validator.valid_order?).to be(false), "user hash is valid"
50
+
51
+ validator = Decision.new(order_id: "asfasdf", user_id: /werwer/)
52
+ expect(validator.valid_order?).to be(false), "regex user is valid"
53
+ end
54
+
55
+ it "order_id, will return false" do
56
+ validator = Decision.new(user_id: 1235)
57
+ expect(validator.valid_order?).to be(false)
58
+
59
+ validator = Decision.new(user_id: 1235, order_id: {})
60
+ expect(validator.valid_order?).to be(false)
61
+
62
+ validator = Decision.new(user_id: 1235, order_id: /23424/)
63
+ expect(validator.valid_order?).to be(false)
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "#error_messages" do
69
+ it "will return an array of error messages" do
70
+ validator = Decision.new
71
+
72
+ error_message =
73
+ "#{Primitive::ERROR_MESSAGES[:non_empty_string]}, got NilClass"
74
+
75
+ validator.valid_order?
76
+
77
+ expect(validator.error_messages).to contain_exactly(
78
+ "order_id #{error_message}",
79
+ "user_id #{error_message}"
80
+ )
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,73 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ require "sift/validate/primitive"
4
+
5
+ module Sift
6
+ module Validate
7
+ describe Primitive do
8
+ describe ".non_empty_string" do
9
+ it "will return nil for valid values" do
10
+ expect(Primitive.non_empty_string("foobar")).to be_nil
11
+ end
12
+
13
+ it "will return an error message" do
14
+ [nil, 1, /asdfasdf/].each do |value|
15
+ error_message = "#{Primitive::ERROR_MESSAGES[:non_empty_string]}" \
16
+ ", got #{value.class}"
17
+
18
+ expect(Primitive.non_empty_string(value)).to eq(error_message)
19
+ end
20
+
21
+ expect(Primitive.non_empty_string("")).to(
22
+ eq(Primitive.send(:empty_string_message, :non_empty_string))
23
+ )
24
+ end
25
+ end
26
+
27
+ describe ".is_numeric" do
28
+ it "will return nil for numeric values" do
29
+ [1, 29.01, 29 ** 1992848499198, -2].each do |value|
30
+ expect(Primitive.numeric(value)).to(
31
+ be_nil,
32
+ "#{value} is a non numeric value"
33
+ )
34
+ end
35
+ end
36
+
37
+ it "will return an error message for non numeric values" do
38
+ ["foobar", nil, :hello_world, {}].each do |value|
39
+ expect(Primitive.numeric(value)).to(
40
+ be(Primitive::ERROR_MESSAGES[:numeric]),
41
+ "#{value} is a numeric value"
42
+ )
43
+ end
44
+ end
45
+ end
46
+
47
+ describe ".is_string_or_number?" do
48
+ it "will return nil for numeric or strings" do
49
+ [1234, "asdfasdf", "1234_asdfsdf"].each do |value|
50
+ expect(Primitive.string_or_number(value)).to(
51
+ be_nil,
52
+ "#{value} is a non numeric value"
53
+ )
54
+ end
55
+ end
56
+
57
+ it "will return an error message for any other type" do
58
+ [{}, [], :hello_world, nil].each do |value|
59
+ error_message = "#{Primitive::ERROR_MESSAGES[:string_or_number]}," \
60
+ " got #{value.class}"
61
+
62
+ expect(Primitive.string_or_number(value)).to eq(error_message)
63
+ end
64
+
65
+ error_message = "#{Primitive::ERROR_MESSAGES[:string_or_number]}, " \
66
+ "got an empty string"
67
+ expect(Primitive.string_or_number("")).to eq(error_message)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sift
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.0
4
+ version: 2.1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fred Sadaghiani
@@ -10,22 +10,36 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-07-19 00:00:00.000000000 Z
13
+ date: 2017-01-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '3.5'
29
+ - !ruby/object:Gem::Dependency
30
+ name: pry
17
31
  requirement: !ruby/object:Gem::Requirement
18
32
  requirements:
19
33
  - - ">="
20
34
  - !ruby/object:Gem::Version
21
- version: 2.14.1
35
+ version: '0'
22
36
  type: :development
23
37
  prerelease: false
24
38
  version_requirements: !ruby/object:Gem::Requirement
25
39
  requirements:
26
40
  - - ">="
27
41
  - !ruby/object:Gem::Version
28
- version: 2.14.1
42
+ version: '0'
29
43
  - !ruby/object:Gem::Dependency
30
44
  name: webmock
31
45
  requirement: !ruby/object:Gem::Requirement
@@ -100,19 +114,31 @@ files:
100
114
  - Gemfile
101
115
  - HISTORY
102
116
  - LICENSE
103
- - README.rdoc
117
+ - README.md
104
118
  - Rakefile
105
119
  - examples/simple.rb
106
120
  - lib/sift.rb
107
121
  - lib/sift/client.rb
122
+ - lib/sift/client/decision.rb
123
+ - lib/sift/client/decision/apply_to.rb
124
+ - lib/sift/error.rb
125
+ - lib/sift/router.rb
126
+ - lib/sift/utils/hash_getter.rb
127
+ - lib/sift/validate/decision.rb
128
+ - lib/sift/validate/primitive.rb
108
129
  - lib/sift/version.rb
109
130
  - sift.gemspec
131
+ - spec/fixtures/fake_responses.rb
110
132
  - spec/integration/sift_spec.rb
111
133
  - spec/spec_helper.rb
134
+ - spec/unit/client/decision/apply_to_spec.rb
135
+ - spec/unit/client/decision_spec.rb
112
136
  - spec/unit/client_203_spec.rb
113
137
  - spec/unit/client_label_spec.rb
114
138
  - spec/unit/client_spec.rb
115
- - spec/unit/sift_spec.rb
139
+ - spec/unit/router_spec.rb
140
+ - spec/unit/validate/decision_spec.rb
141
+ - spec/unit/validate/primitive_spec.rb
116
142
  homepage: http://siftscience.com
117
143
  licenses: []
118
144
  metadata: {}
@@ -132,14 +158,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
158
  version: '0'
133
159
  requirements: []
134
160
  rubyforge_project: sift
135
- rubygems_version: 2.4.6
161
+ rubygems_version: 2.5.1
136
162
  signing_key:
137
163
  specification_version: 4
138
164
  summary: Sift Science Ruby API Gem
139
165
  test_files:
166
+ - spec/fixtures/fake_responses.rb
140
167
  - spec/integration/sift_spec.rb
141
168
  - spec/spec_helper.rb
169
+ - spec/unit/client/decision/apply_to_spec.rb
170
+ - spec/unit/client/decision_spec.rb
142
171
  - spec/unit/client_203_spec.rb
143
172
  - spec/unit/client_label_spec.rb
144
173
  - spec/unit/client_spec.rb
145
- - spec/unit/sift_spec.rb
174
+ - spec/unit/router_spec.rb
175
+ - spec/unit/validate/decision_spec.rb
176
+ - spec/unit/validate/primitive_spec.rb
@@ -1,110 +0,0 @@
1
- = Sift Science Ruby bindings {<img src="https://travis-ci.org/SiftScience/sift-ruby.png?branch=master" alt="Build Status" />}[https://travis-ci.org/SiftScience/sift-ruby]
2
-
3
-
4
- == Requirements
5
-
6
- * Ruby 1.8.7 or above. (Ruby 1.8.6 might work if you load ActiveSupport.)
7
- * HTTParty, 0.11.0 or greater
8
- * Multi Json, 1.0 or greater
9
-
10
- For development only:
11
- * bundler
12
- * rspec, 2.14.1 or greater
13
- * webmock, 1.16 or greater
14
- * rake, any version
15
-
16
-
17
- == Installation
18
-
19
- If you want to build the gem from source:
20
-
21
- $ gem build sift.gemspec
22
-
23
- Alternatively, you can install the gem from Rubyforge:
24
-
25
- $ gem install sift
26
-
27
-
28
- == Usage
29
-
30
- require "sift"
31
-
32
- Sift.api_key = '<your_api_key_here>'
33
- Sift.account_id = '<your_account_id_here>'
34
- client = Sift::Client.new()
35
-
36
- # send a transaction event -- note this is blocking
37
- event = "$transaction"
38
-
39
- user_id = "23056" # User ID's may only contain a-z, A-Z, 0-9, =, ., -, _, +, @, :, &, ^, %, !, $
40
-
41
- properties = {
42
- "$user_id" => user_id,
43
- "$user_email" => "buyer@gmail.com",
44
- "$seller_user_id" => "2371",
45
- "seller_user_email" => "seller@gmail.com",
46
- "$transaction_id" => "573050",
47
- "$payment_method" => {
48
- "$payment_type" => "$credit_card",
49
- "$payment_gateway" => "$braintree",
50
- "$card_bin" => "542486",
51
- "$card_last4" => "4444"
52
- },
53
- "$currency_code" => "USD",
54
- "$amount" => 15230000,
55
- }
56
-
57
- response = client.track(event, properties)
58
-
59
- response.ok? # returns true or false
60
- response.body # API response body
61
- response.http_status_code # HTTP response code, 200 is ok.
62
- response.api_status # status field in the return body, Link to Error Codes
63
- response.api_error_message # Error message associated with status Error Code
64
-
65
-
66
- # Request a score for the user with user_id 23056
67
- response = client.score(user_id)
68
-
69
-
70
- # Label the user with user_id 23056 as Bad with all optional fields
71
- response = client.label(user_id, {
72
- "$is_bad" => true,
73
- "$abuse_type" => "payment_abuse",
74
- "$description" => "Chargeback issued",
75
- "$source" => "Manual Review",
76
- "$analyst" => "analyst.name@your_domain.com"
77
- })
78
-
79
-
80
- # Get the status of a workflow run
81
- response = client.get_workflow_status('my_run_id')
82
-
83
-
84
- # Get the latest decisions for a user
85
- response = client.get_user_decisions('example_user_id')
86
-
87
-
88
- # Get the latest decisions for an order
89
- response = client.get_order_decisions('example_order_id')
90
-
91
-
92
- == Building
93
-
94
- Building and publishing the gem is captured by the following steps:
95
-
96
- $ gem build sift.gemspec
97
- $ gem push sift-<current version>.gem
98
-
99
- $ bundle
100
- $ rake -T
101
- $ rake build
102
- $ rake install
103
- $ rake release
104
-
105
-
106
- == Testing
107
-
108
- To run the various tests use the rake command as follows:
109
-
110
- $ rake spec