minfraud 1.0.4 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +5 -5
  2. data/.github/dependabot.yml +11 -0
  3. data/.github/workflows/release.yml +28 -0
  4. data/.github/workflows/rubocop.yml +18 -0
  5. data/.github/workflows/test.yml +36 -0
  6. data/.gitignore +2 -0
  7. data/.rubocop.yml +90 -0
  8. data/CHANGELOG.md +197 -4
  9. data/CODE_OF_CONDUCT.md +4 -4
  10. data/Gemfile +2 -2
  11. data/LICENSE.txt +2 -1
  12. data/README.dev.md +4 -0
  13. data/README.md +255 -58
  14. data/Rakefile +9 -3
  15. data/bin/console +4 -3
  16. data/dev-bin/release.sh +59 -0
  17. data/lib/minfraud/assessments.rb +127 -53
  18. data/lib/minfraud/components/account.rb +31 -9
  19. data/lib/minfraud/components/addressable.rb +73 -26
  20. data/lib/minfraud/components/base.rb +26 -14
  21. data/lib/minfraud/components/billing.rb +5 -0
  22. data/lib/minfraud/components/credit_card.rb +117 -29
  23. data/lib/minfraud/components/custom_inputs.rb +25 -0
  24. data/lib/minfraud/components/device.rb +51 -10
  25. data/lib/minfraud/components/email.rb +120 -9
  26. data/lib/minfraud/components/event.rb +60 -13
  27. data/lib/minfraud/components/order.rb +59 -22
  28. data/lib/minfraud/components/payment.rb +192 -22
  29. data/lib/minfraud/components/report/transaction.rb +80 -0
  30. data/lib/minfraud/components/shipping.rb +15 -6
  31. data/lib/minfraud/components/shopping_cart.rb +19 -12
  32. data/lib/minfraud/components/shopping_cart_item.rb +42 -13
  33. data/lib/minfraud/enum.rb +22 -8
  34. data/lib/minfraud/error_handler.rb +45 -18
  35. data/lib/minfraud/errors.rb +22 -2
  36. data/lib/minfraud/http_service/response.rb +61 -17
  37. data/lib/minfraud/model/abstract.rb +20 -0
  38. data/lib/minfraud/model/address.rb +52 -0
  39. data/lib/minfraud/model/billing_address.rb +11 -0
  40. data/lib/minfraud/model/credit_card.rb +75 -0
  41. data/lib/minfraud/model/device.rb +51 -0
  42. data/lib/minfraud/model/disposition.rb +42 -0
  43. data/lib/minfraud/model/email.rb +54 -0
  44. data/lib/minfraud/model/email_domain.rb +24 -0
  45. data/lib/minfraud/model/error.rb +28 -0
  46. data/lib/minfraud/model/factors.rb +24 -0
  47. data/lib/minfraud/model/geoip2_location.rb +25 -0
  48. data/lib/minfraud/model/insights.rb +68 -0
  49. data/lib/minfraud/model/ip_address.rb +58 -0
  50. data/lib/minfraud/model/ip_risk_reason.rb +48 -0
  51. data/lib/minfraud/model/issuer.rb +49 -0
  52. data/lib/minfraud/model/score.rb +76 -0
  53. data/lib/minfraud/model/score_ip_address.rb +23 -0
  54. data/lib/minfraud/model/shipping_address.rb +30 -0
  55. data/lib/minfraud/model/subscores.rb +156 -0
  56. data/lib/minfraud/model/warning.rb +63 -0
  57. data/lib/minfraud/report.rb +66 -0
  58. data/lib/minfraud/resolver.rb +25 -16
  59. data/lib/minfraud/validates.rb +187 -0
  60. data/lib/minfraud/version.rb +4 -1
  61. data/lib/minfraud.rb +55 -16
  62. data/minfraud.gemspec +27 -18
  63. metadata +107 -36
  64. data/.travis.yml +0 -5
  65. data/lib/minfraud/http_service/request.rb +0 -37
  66. data/lib/minfraud/http_service.rb +0 -31
data/README.md CHANGED
@@ -1,12 +1,14 @@
1
- # Simple Ruby Wrapper to the MaxMind minFraud API
1
+ # Ruby API for MaxMind minFraud Services
2
2
 
3
- [![Code Climate](https://codeclimate.com/github/kushniryb/minfraud-api-v2/badges/gpa.svg)](https://codeclimate.com/github/kushniryb/minfraud-api-v2)
4
- [![Coverage Status](https://coveralls.io/repos/github/kushniryb/minfraud-api-v2/badge.svg?branch=master)](https://coveralls.io/github/kushniryb/minfraud-api-v2?branch=master)
5
- [![Build Status](https://travis-ci.org/kushniryb/minfraud-api-v2.svg?branch=master)](https://travis-ci.org/kushniryb/minfraud-api-v2)
3
+ ## Description
6
4
 
7
- Compatible with version minFraud API v2.0
5
+ This package provides an API for the [MaxMind minFraud web
6
+ services](https://dev.maxmind.com/minfraud?lang=en). This includes minFraud Score,
7
+ Insights, and Factors. It also includes our [minFraud Report Transaction
8
+ API](https://dev.maxmind.com/minfraud/report-a-transaction?lang=en).
8
9
 
9
- [minFraud API documentation](http://dev.maxmind.com/minfraud/)
10
+ The legacy minFraud Standard and Premium services are not supported by this
11
+ API.
10
12
 
11
13
  ## Installation
12
14
 
@@ -18,90 +20,285 @@ gem 'minfraud'
18
20
 
19
21
  And then execute:
20
22
 
21
- ```ruby
23
+ ```
22
24
  $ bundle
23
25
  ```
24
26
 
25
27
  Or install it yourself as:
28
+
26
29
  ```
27
30
  $ gem install minfraud
28
31
  ```
29
32
 
30
- ## Configuration
33
+ ## API Documentation
34
+
35
+ See the [API documentation](https://www.rubydoc.info/gems/minfraud) for
36
+ more details.
37
+
38
+ ## Usage
39
+
40
+ ### Configuration
41
+
42
+ An account ID and license key are required to work with the web services.
43
+ Configure these before making a request:
44
+
45
+ ```ruby
46
+ Minfraud.configure do |c|
47
+ c.account_id = 12345
48
+ c.license_key = 'your_license_key'
49
+ c.enable_validation = true
50
+ end
51
+ ````
31
52
 
32
- User Id and License Key are required to work with minFraud API
53
+ To use the Sandbox web service instead of the production web service, you can provide the host:
33
54
 
34
55
  ```ruby
35
56
  Minfraud.configure do |c|
57
+ c.account_id = 12345
36
58
  c.license_key = 'your_license_key'
37
- c.user_id = 'your_user_id'
59
+ c.host = 'sandbox.maxmind.com'
38
60
  end
39
61
  ```
40
62
 
41
- ## Usage
63
+ ### Making a minFraud Score, Insights, or Factors Request
64
+
65
+ To use the minFraud API, create a `Minfraud::Assessments` object. The
66
+ constructor takes a hash of symbols corresponding to each component of the
67
+ minFraud request. You can also set components by their attribute after
68
+ creating the object.
69
+
70
+ After populating the object, call the method for the minFraud endpoint you
71
+ want to use: `#score`, `#insights`, or `#factors`. The returned value is a
72
+ `MinFraud::Response` object. You can access the response model through its
73
+ `#body` attribute.
74
+
75
+ An exception will be thrown for critical errors. You should check for
76
+ `warnings` related to your inputs after a request.
77
+
42
78
  ```ruby
43
- # You can either provide a hash of params to initializer
79
+ # Prepare the request.
44
80
  assessment = Minfraud::Assessments.new(
45
81
  device: {
46
- ip_address: '1.2.3.4.5'
47
- }
82
+ ip_address: '152.216.7.110',
83
+ accept_language: 'en-US,en;q=0.8',
84
+ session_age: 3600.5,
85
+ session_id: 'foo',
86
+ user_agent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',
87
+ },
88
+ event: {
89
+ transaction_id: 'txn3134133',
90
+ shop_id: 's2123',
91
+ time: '2012-04-12T23:20:50+00:00',
92
+ type: :purchase,
93
+ },
94
+ account: {
95
+ user_id: '3132',
96
+ username_md5: '4f9726678c438914fa04bdb8c1a24088',
97
+ },
98
+ email: {
99
+ address: 'test@maxmind.com',
100
+ domain: 'maxmind.com',
101
+ },
102
+ billing: {
103
+ first_name: 'First',
104
+ last_name: 'Last',
105
+ company: 'Company',
106
+ address: '101 Address Rd.',
107
+ address_2: 'Unit 5',
108
+ city: 'New Haven',
109
+ region: 'CT',
110
+ country: 'US',
111
+ postal: '06510',
112
+ phone_number: '123-456-7890',
113
+ phone_country_code: '1',
114
+ },
115
+ shipping: {
116
+ first_name: 'ShipFirst',
117
+ last_name: 'ShipLast',
118
+ company: 'ShipCo',
119
+ address: '322 Ship Addr. Ln.',
120
+ address_2: 'St. 43',
121
+ city: 'Nowhere',
122
+ region: 'OK',
123
+ country: 'US',
124
+ postal: '73003',
125
+ phone_number: '123-456-0000',
126
+ phone_country_code: '1',
127
+ delivery_speed: :same_day,
128
+ },
129
+ payment: {
130
+ processor: :stripe,
131
+ was_authorized: false,
132
+ decline_code: 'invalid number',
133
+ },
134
+ credit_card: {
135
+ issuer_id_number: '411111',
136
+ last_digits: '7643',
137
+ bank_name: 'Bank of No Hope',
138
+ bank_phone_country_code: '1',
139
+ bank_phone_number: '123-456-1234',
140
+ token: 'abcd',
141
+ avs_result: 'Y',
142
+ cvv_result: 'N',
143
+ was_3d_secure_successful: true,
144
+ },
145
+ order: {
146
+ amount: 323.21,
147
+ currency: 'USD',
148
+ discount_code: 'FIRST',
149
+ is_gift: true,
150
+ has_gift_message: false,
151
+ affiliate_id: 'af12',
152
+ subaffiliate_id: 'saf42',
153
+ referrer_uri: 'http://www.amazon.com/',
154
+ },
155
+ shopping_cart: [
156
+ {
157
+ category: 'pets',
158
+ item_id: 'leash-0231',
159
+ quantity: 2,
160
+ price: 20.43,
161
+ },
162
+ {
163
+ category: 'beauty',
164
+ item_id: 'msc-1232',
165
+ quantity: 1,
166
+ price: 100.00,
167
+ },
168
+ ],
169
+ custom_inputs: {
170
+ section: 'news',
171
+ previous_purchases: 19,
172
+ discount: 3.2,
173
+ previous_user: true,
174
+ },
48
175
  )
49
- # or create a component and assign them to assessments object directly, e.g
50
- device = Minfraud::Components::Device.new(ip_address: '1.2.3.4.5')
51
- assessment = Minfraud::Assessments.new(device: device)
52
- # or
53
- assessment = Minfraud::Assessments.new
54
- assessment.device = device
55
- # There are multiple components that reflect minFraud request top level keys
56
-
57
- # Some components will raise an error if provided with the wrong values for attributes, e.g
58
- event = Minfraud::Components::Event.new(type: 'foobar') # => Minfraud::NotEnumValueError
59
- # You can check the list of permitted values for the attribute by calling a class method
60
- Minfraud::Components::Event.type_values # => ["account_creation", "account_login", ....]
61
-
62
- # You can now call 3 different minFraud endpoints: score, insights, factors
63
- assessment.insights
64
- assessment.factors
65
-
66
- result = assessment.score # => Minfraud::Response instance
67
-
68
- result.status # => Response status code
69
- result.code # => minFraud specific response code
70
- result.body # => Mashified body
71
- result.headers # => Response headers
72
-
73
- # You can also change data inbetween requests
74
- first_request = assessment.insights
75
- assessment.device.ip_address = '22.22.22.33'
76
- second_request = assessment.insights
176
+
177
+ # To get the Factors response model, use #factors.
178
+ factors_model = assessment.factors.body
179
+
180
+ factors_model.warnings.each { |w| puts w.warning }
181
+
182
+ p factors_model.subscores.email_address
183
+ p factors_model.risk_score
184
+
185
+ # To get the Insights response model, use #insights.
186
+ insights_model = assessment.insights.body
187
+
188
+ insights_model.warnings.each { |w| puts w.warning }
189
+
190
+ p insights_model.credit_card.issuer.name
191
+ p insights_model.risk_score
192
+
193
+ # To get the Score response model, use #score.
194
+ score_model = assessment.score.body
195
+
196
+ score_model.warnings.each { |w| puts w.warning }
197
+
198
+ p score_model.risk_score
77
199
  ```
78
200
 
79
- ### Exception handling
201
+ See the [API documentation](https://www.rubydoc.info/gems/minfraud) for
202
+ more details.
80
203
 
81
- Gem is supplied with four different types of exceptions:
82
- ```ruby
83
- # Raised if unpermitted key is provided to Minfraud::Assessments initializer
84
- class RequestFormatError < BaseError; end
204
+ ### Reporting a Transaction to MaxMind
85
205
 
86
- # Raised if IP address is absent / it is reserved / JSON body can not be decoded
87
- class ClientError < BaseError; end
206
+ MaxMind encourages the use of this API, as data received through this
207
+ channel is used to improve the accuracy of their fraud detection
208
+ algorithms.
88
209
 
89
- # Raised if there are some problems with the user id and / or license key
90
- class AuthorizationError < BaseError; end
210
+ To use the Report Transaction API, create a
211
+ `Minfraud::Components::Report::Transaction` object. An IP address and a
212
+ valid tag are required arguments for this API. Additional parameters may be
213
+ set, as shown below.
91
214
 
92
- # Raised if minFraud returns an error, or if there is an HTTP error
93
- class ServerError < BaseError; end
215
+ If the report is successful, nothing is returned. If the report fails, an
216
+ exception will be thrown.
94
217
 
95
- # Raised if an attribute value doesn't belong to the predefined set of values
96
- class NotEnumValueError < BaseError; end
218
+ ```ruby
219
+ # The report_transaction method only makes use of a transaction component:
220
+ txn = Minfraud::Components::Report::Transaction.new(
221
+ ip_address: '152.216.7.110',
222
+ tag: :suspected_fraud,
223
+ maxmind_id: '12345678',
224
+ minfraud_id: '58fa38d8-4b87-458b-a22b-f00eda1aa20d',
225
+ notes: 'notes go here',
226
+ transaction_id: '1FA254yZ'
227
+ )
228
+ reporter = Minfraud::Report.new(transaction: txn)
229
+ reporter.report_transaction
97
230
  ```
98
231
 
232
+ See the [API documentation](https://www.rubydoc.info/gems/minfraud) for
233
+ more details.
234
+
235
+ ### Persistent HTTP Connections
236
+
237
+ This gem supports persistent HTTP connections, allowing you to avoid the
238
+ overhead of creating a new HTTP connection for each minFraud request if you
239
+ plan to perform more than one. You do not need to do anything to enable
240
+ this functionality.
241
+
242
+ ### Exceptions
243
+
244
+ The gem supplies several distinct exception-types:
245
+
246
+ * `RequestFormatError` - Raised if an unknown key is provided to the
247
+ `Minfraud::Assessments` constructor
248
+ * `ClientError` - Raised if the IP address is absent, reserved, or the JSON
249
+ body cannot be decoded
250
+ * `AuthorizationError` - Raised if there are problems with the account ID
251
+ and/or license key
252
+ * `ServerError` - Raised if minFraud returns an error or if there is an
253
+ HTTP error
254
+ * `NotEnumValueError` - Raised if an attribute value doesn't belong to the
255
+ predefined set of values
256
+
257
+ ### Thread Safety
258
+
259
+ This gem is safe for use from multiple threads.
260
+
261
+ `Minfraud::Assessments` and `Minfraud::Report` objects must not be shared
262
+ across threads.
263
+
264
+ Please note that you must run `Minfraud.configure` before calling any
265
+ functionality using multiple threads.
266
+
267
+ ## Support
268
+
269
+ Please report all issues with this code using the
270
+ [GitHub issue tracker](https://github.com/maxmind/minfraud-api-ruby/issues).
271
+
272
+ If you are having an issue with the minFraud service that is not specific
273
+ to the client API, please see
274
+ [our support page](https://www.maxmind.com/en/support).
275
+
276
+ ## Requirements
277
+
278
+ This gem works with Ruby 2.7 and above.
279
+
99
280
  ## Contributing
100
281
 
101
- Bug reports and pull requests are welcome on GitHub [here](https://github.com/kushniryb/minfraud-api-v2). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
282
+ Bug reports and pull requests are welcome on
283
+ [GitHub](https://github.com/maxmind/minfraud-api-ruby). This project is
284
+ intended to be a safe, welcoming space for collaboration, and contributors
285
+ are expected to adhere to the [Contributor
286
+ Covenant](https://contributor-covenant.org) code of conduct.
287
+
288
+ ## Versioning
289
+
290
+ This API uses [Semantic Versioning](https://semver.org/).
291
+
292
+ ## Copyright and License
293
+
294
+ Copyright (c) 2016-2020 kushnir.yb.
102
295
 
296
+ Copyright (c) 2020-2024 MaxMind, Inc.
103
297
 
104
- ## License
298
+ The gem is available as open source under the terms of the [MIT
299
+ License](https://opensource.org/licenses/MIT).
105
300
 
106
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
301
+ ## Thank You
107
302
 
303
+ This gem is the work of the creator and original maintainer kushnir.yb
304
+ (@kushniryb).
data/Rakefile CHANGED
@@ -1,6 +1,12 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
3
6
 
4
7
  RSpec::Core::RakeTask.new(:spec)
5
8
 
6
- task :default => :spec
9
+ RuboCop::RakeTask.new
10
+
11
+ task default: :spec
12
+ task default: :rubocop
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "minfraud"
4
+ require 'bundler/setup'
5
+ require 'minfraud'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "minfraud"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start
@@ -0,0 +1,59 @@
1
+ #!/bin/bash
2
+
3
+ set -eu -o pipefail
4
+
5
+ changelog=$(cat CHANGELOG.md)
6
+
7
+ regex='
8
+ ## v([0-9]+\.[0-9]+\.[0-9]+) \(([0-9]{4}-[0-9]{2}-[0-9]{2})\)
9
+
10
+ ((.|
11
+ )*)
12
+ '
13
+
14
+ if [[ ! $changelog =~ $regex ]]; then
15
+ echo "Could not find date line in change log!"
16
+ exit 1
17
+ fi
18
+
19
+ version="${BASH_REMATCH[1]}"
20
+ date="${BASH_REMATCH[2]}"
21
+ notes="$(echo "${BASH_REMATCH[3]}" | sed -n -E '/^## v[0-9]+\.[0-9]+\.[0-9]+/,$!p')"
22
+
23
+ echo "$notes"
24
+ if [[ "$date" != "$(date +"%Y-%m-%d")" ]]; then
25
+ echo "$date is not today!"
26
+ exit 1
27
+ fi
28
+
29
+ tag="v$version"
30
+
31
+ if [ -n "$(git status --porcelain)" ]; then
32
+ echo ". is not clean." >&2
33
+ exit 1
34
+ fi
35
+
36
+ perl -pi -e "s/(?<=VERSION = \').+?(?=\')/$version/g" lib/minfraud/version.rb
37
+
38
+ echo $"Test results:"
39
+
40
+ rake
41
+
42
+ echo $'\nDiff:'
43
+ git diff
44
+
45
+ echo $'\nRelease notes:'
46
+ echo "$notes"
47
+
48
+ read -e -p "Commit changes and push to origin? " should_push
49
+
50
+ if [ "$should_push" != "y" ]; then
51
+ echo "Aborting"
52
+ exit 1
53
+ fi
54
+
55
+ git commit -m "Update for $tag" -a
56
+
57
+ git push
58
+
59
+ gh release create --target "$(git branch --show-current)" -t "$version" -n "$notes" "$tag"
@@ -1,92 +1,166 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
4
+ # Assessments is used to perform minFraud Score, Insights, and Factors
5
+ # requests.
6
+ #
7
+ # @see https://dev.maxmind.com/minfraud?lang=en
2
8
  class Assessments
3
- include ::Minfraud::HTTPService
4
9
  include ::Minfraud::Resolver
5
10
 
6
- # @attribute account
7
- # @return [Minfraud::Components::Account] Account component
11
+ # The Account component.
12
+ #
13
+ # @return [Minfraud::Components::Account, nil]
8
14
  attr_accessor :account
9
15
 
10
- # @attribute billing
11
- # @return [Minfraud::Components::Billing] Billing component
16
+ # The Billing component.
17
+ #
18
+ # @return [Minfraud::Components::Billing, nil]
12
19
  attr_accessor :billing
13
20
 
14
- # @attribute credit_card
15
- # @return [Minfraud::Components::CreditCard] CreditCard component
21
+ # The CreditCard component.
22
+ #
23
+ # @return [Minfraud::Components::CreditCard, nil]
16
24
  attr_accessor :credit_card
17
25
 
18
- # @attribute device
19
- # @return [Minfraud::Components::Device] Device component
26
+ # The CustomInputs component.
27
+ #
28
+ # @return [Minfraud::Components::CustomInputs, nil]
29
+ attr_accessor :custom_inputs
30
+
31
+ # The Device component.
32
+ #
33
+ # @return [Minfraud::Components::Device, nil]
20
34
  attr_accessor :device
21
35
 
22
- # @attribute email
23
- # @return [Minfraud::Components::Email] Email component
36
+ # The Email component.
37
+ #
38
+ # @return [Minfraud::Components::Email, nil]
24
39
  attr_accessor :email
25
40
 
26
- # @attribute event
27
- # @return [Minfraud::Components::Event] Event component
41
+ # The Event component.
42
+ #
43
+ # @return [Minfraud::Components::Event, nil]
28
44
  attr_accessor :event
29
45
 
30
- # @attribute order
31
- # @return [Minfraud::Components::Order] Order component
46
+ # The Order component.
47
+ #
48
+ # @return [Minfraud::Components::Order, nil]
32
49
  attr_accessor :order
33
50
 
34
- # @attribute payment
35
- # @return [Minfraud::Components::Payment] Payment component
51
+ # The Payment component.
52
+ #
53
+ # @return [Minfraud::Components::Payment, nil]
36
54
  attr_accessor :payment
37
55
 
38
- # @!attribute shipping
39
- # @return [Minfraud::Components::Shipping] Shipping component
56
+ # The Shipping component.
57
+ #
58
+ # @return [Minfraud::Components::Shipping, nil]
40
59
  attr_accessor :shipping
41
60
 
42
- # @!attribute shopping_cart
43
- # @return [Minfraud::Components::ShoppingCarat] ShoppingCart component
61
+ # The ShoppingCart component.
62
+ #
63
+ # @return [Minfraud::Components::ShoppingCart, nil]
44
64
  attr_accessor :shopping_cart
45
65
 
46
- # @param [Hash] params hash of parameters
47
- # @param [Minfraud::Resolver] resolver resolver that maps params to components
48
- # @note In case when params is a Hash of components it just assigns them to the corresponding instance variables
49
- # @return [Minfraud::Assessments] Assessments instance
66
+ # @param params [Hash] Hash of parameters. Each key is a symbol
67
+ # corresponding to one of the available component attributes. Values may
68
+ # be component objects or hashes that will be provided to the component
69
+ # constructors.
70
+ #
71
+ # @param resolver [Minfraud::Resolver] Resolver that maps parameters to
72
+ # components.
50
73
  def initialize(params = {}, resolver = ::Minfraud::Resolver)
74
+ @locales = params.delete('locales')
75
+ @locales = ['en'] if @locales.nil?
76
+
51
77
  resolver.assign(self, params)
52
78
  end
53
79
 
54
- # @!macro [attach] define
55
- # @method $1
56
- # Makes a request to minFraud $1 endpoint.
57
- # Raises an error in case of invalid response
58
- # @return [Minfraud::HTTPService::Response] Wrapped minFraud response
59
- def self.define(endpoint)
60
- define_method(endpoint) do
61
- raw = request.perform(verb: :post, endpoint: endpoint.to_s, body: request_body)
62
- response = ::Minfraud::HTTPService::Response.new(
63
- status: raw.status.to_i,
64
- body: raw.body,
65
- headers: raw.headers
66
- )
80
+ # Perform a minFraud Factors request.
81
+ #
82
+ # @return [Minfraud::HTTPService::Response]
83
+ #
84
+ # @raise [JSON::ParserError] if there was invalid JSON in the response.
85
+ #
86
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
87
+ # problem.
88
+ #
89
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
90
+ # of your inputs.
91
+ #
92
+ # @raise [Minfraud::ServerError] If the server reported an error of some
93
+ # kind.
94
+ def factors
95
+ perform_request(:factors)
96
+ end
67
97
 
68
- ::Minfraud::ErrorHandler.inspect(response)
69
- end
98
+ # Perform a minFraud Insights request.
99
+ #
100
+ # @return [Minfraud::HTTPService::Response]
101
+ #
102
+ # @raise [JSON::ParserError] if there was invalid JSON in the response.
103
+ #
104
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
105
+ # problem.
106
+ #
107
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
108
+ # of your inputs.
109
+ #
110
+ # @raise [Minfraud::ServerError] If the server reported an error of some
111
+ # kind.
112
+ def insights
113
+ perform_request(:insights)
70
114
  end
71
115
 
72
- define :score
73
- define :insights
74
- define :factors
116
+ # Perform a minFraud Score request.
117
+ #
118
+ # @return [Minfraud::HTTPService::Response]
119
+ #
120
+ # @raise [JSON::ParserError] if there was invalid JSON in the response.
121
+ #
122
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
123
+ # problem.
124
+ #
125
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
126
+ # of your inputs.
127
+ #
128
+ # @raise [Minfraud::ServerError] If the server reported an error of some
129
+ # kind.
130
+ def score
131
+ perform_request(:score)
132
+ end
75
133
 
76
134
  private
77
- # Creates a unified request body from components converted to JSON
78
- # @return [Hash] Request body
79
- def request_body
80
- MAPPING.keys.inject({}) do |mem, e|
81
- next mem unless value = send(e)
82
- mem.merge!(e.to_s => value.to_json)
135
+
136
+ def perform_request(endpoint)
137
+ response = nil
138
+ body = nil
139
+ Minfraud.connection_pool.with do |client|
140
+ response = client.post(
141
+ "/minfraud/v2.0/#{endpoint}",
142
+ json: request_body,
143
+ )
144
+
145
+ body = response.to_s
83
146
  end
147
+
148
+ response = ::Minfraud::HTTPService::Response.new(
149
+ endpoint,
150
+ @locales,
151
+ response,
152
+ body,
153
+ )
154
+
155
+ ::Minfraud::ErrorHandler.examine(response)
84
156
  end
85
157
 
86
- # Creates memoized Minfraud::HTTPService::Request instance
87
- # @return [Minfraud::HTTPService::Request] Request instance based on configuration params
88
- def request
89
- @request ||= Request.new(::Minfraud::HTTPService.configuration)
158
+ def request_body
159
+ MAPPING.keys.reduce({}) do |mem, e|
160
+ next mem unless (value = send(e))
161
+
162
+ mem.merge!(e.to_s => value.to_json)
163
+ end
90
164
  end
91
165
  end
92
166
  end