minfraud 1.0.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/rubocop.yml +12 -0
  3. data/.github/workflows/test.yml +32 -0
  4. data/.gitignore +2 -0
  5. data/.rubocop.yml +108 -0
  6. data/CHANGELOG.md +70 -2
  7. data/CODE_OF_CONDUCT.md +4 -4
  8. data/Gemfile +11 -2
  9. data/LICENSE.txt +2 -1
  10. data/README.dev.md +4 -0
  11. data/README.md +245 -59
  12. data/Rakefile +9 -3
  13. data/bin/console +4 -3
  14. data/lib/maxmind/geoip2/model/city.rb +99 -0
  15. data/lib/maxmind/geoip2/model/country.rb +94 -0
  16. data/lib/maxmind/geoip2/model/insights.rb +38 -0
  17. data/lib/maxmind/geoip2/record/abstract.rb +46 -0
  18. data/lib/maxmind/geoip2/record/city.rb +62 -0
  19. data/lib/maxmind/geoip2/record/continent.rb +61 -0
  20. data/lib/maxmind/geoip2/record/country.rb +78 -0
  21. data/lib/maxmind/geoip2/record/location.rb +97 -0
  22. data/lib/maxmind/geoip2/record/maxmind.rb +41 -0
  23. data/lib/maxmind/geoip2/record/place.rb +52 -0
  24. data/lib/maxmind/geoip2/record/postal.rb +54 -0
  25. data/lib/maxmind/geoip2/record/represented_country.rb +47 -0
  26. data/lib/maxmind/geoip2/record/subdivision.rb +72 -0
  27. data/lib/maxmind/geoip2/record/traits.rb +233 -0
  28. data/lib/minfraud.rb +48 -8
  29. data/lib/minfraud/assessments.rb +118 -49
  30. data/lib/minfraud/components/account.rb +31 -9
  31. data/lib/minfraud/components/addressable.rb +73 -26
  32. data/lib/minfraud/components/base.rb +35 -11
  33. data/lib/minfraud/components/billing.rb +5 -0
  34. data/lib/minfraud/components/credit_card.rb +64 -20
  35. data/lib/minfraud/components/custom_inputs.rb +25 -0
  36. data/lib/minfraud/components/device.rb +51 -10
  37. data/lib/minfraud/components/email.rb +29 -7
  38. data/lib/minfraud/components/event.rb +60 -13
  39. data/lib/minfraud/components/order.rb +60 -22
  40. data/lib/minfraud/components/payment.rb +166 -21
  41. data/lib/minfraud/components/report/transaction.rb +80 -0
  42. data/lib/minfraud/components/shipping.rb +14 -5
  43. data/lib/minfraud/components/shopping_cart.rb +19 -12
  44. data/lib/minfraud/components/shopping_cart_item.rb +42 -13
  45. data/lib/minfraud/enum.rb +22 -8
  46. data/lib/minfraud/error_handler.rb +45 -18
  47. data/lib/minfraud/errors.rb +22 -2
  48. data/lib/minfraud/http_service.rb +22 -8
  49. data/lib/minfraud/http_service/request.rb +19 -18
  50. data/lib/minfraud/http_service/response.rb +49 -12
  51. data/lib/minfraud/model/abstract.rb +20 -0
  52. data/lib/minfraud/model/address.rb +52 -0
  53. data/lib/minfraud/model/billing_address.rb +11 -0
  54. data/lib/minfraud/model/credit_card.rb +75 -0
  55. data/lib/minfraud/model/device.rb +54 -0
  56. data/lib/minfraud/model/disposition.rb +35 -0
  57. data/lib/minfraud/model/email.rb +54 -0
  58. data/lib/minfraud/model/email_domain.rb +24 -0
  59. data/lib/minfraud/model/error.rb +28 -0
  60. data/lib/minfraud/model/factors.rb +24 -0
  61. data/lib/minfraud/model/geoip2_location.rb +25 -0
  62. data/lib/minfraud/model/insights.rb +68 -0
  63. data/lib/minfraud/model/ip_address.rb +82 -0
  64. data/lib/minfraud/model/issuer.rb +49 -0
  65. data/lib/minfraud/model/score.rb +76 -0
  66. data/lib/minfraud/model/score_ip_address.rb +23 -0
  67. data/lib/minfraud/model/shipping_address.rb +30 -0
  68. data/lib/minfraud/model/subscores.rb +178 -0
  69. data/lib/minfraud/model/warning.rb +63 -0
  70. data/lib/minfraud/report.rb +58 -0
  71. data/lib/minfraud/resolver.rb +25 -16
  72. data/lib/minfraud/validates.rb +187 -0
  73. data/lib/minfraud/version.rb +4 -1
  74. data/minfraud.gemspec +23 -18
  75. metadata +113 -39
  76. data/.travis.yml +0 -5
@@ -1,19 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
1
4
  require 'minfraud'
2
5
  require 'minfraud/enum'
6
+ require 'minfraud/validates'
3
7
  require 'minfraud/components/base'
4
8
  require 'minfraud/components/account'
5
9
  require 'minfraud/components/addressable'
6
10
  require 'minfraud/components/billing'
7
11
  require 'minfraud/components/credit_card'
12
+ require 'minfraud/components/custom_inputs'
8
13
  require 'minfraud/components/device'
9
14
  require 'minfraud/components/email'
10
15
  require 'minfraud/components/event'
11
16
  require 'minfraud/components/order'
12
17
  require 'minfraud/components/payment'
13
- require 'minfraud/components/shopping_cart_item'
18
+ require 'minfraud/components/report/transaction'
14
19
  require 'minfraud/components/shipping'
15
20
  require 'minfraud/components/shopping_cart'
16
- require 'minfraud/components/device'
21
+ require 'minfraud/components/shopping_cart_item'
17
22
  require 'minfraud/resolver'
18
23
  require 'minfraud/version'
19
24
  require 'minfraud/errors'
@@ -22,23 +27,58 @@ require 'minfraud/http_service/request'
22
27
  require 'minfraud/http_service/response'
23
28
  require 'minfraud/error_handler'
24
29
  require 'minfraud/assessments'
30
+ require 'minfraud/report'
25
31
 
32
+ # This class holds global configuration parameters and provides a namespace
33
+ # for the gem's classes.
26
34
  module Minfraud
27
35
  class << self
28
- # @!attribute user_id
29
- # @return [String] MaxMind username that is used for authorization
36
+ # The MaxMind account ID that is used for authorization.
37
+ #
38
+ # @return [Integer, nil]
39
+ attr_accessor :account_id
40
+
41
+ # Enable client side validation. This is disabled by default.
42
+ #
43
+ # @return [Boolean, nil]
44
+ attr_accessor :enable_validation
45
+
46
+ # The host to use when connecting to the web service.
47
+ #
48
+ # @return [String, nil]
49
+ attr_accessor :host
50
+
51
+ # The MaxMind account ID that is used for authorization.
52
+ #
53
+ # @deprecated Use {::account_id} instead. This will be removed in the next
54
+ # major version.
55
+ #
56
+ # @return [Integer, nil]
30
57
  attr_accessor :user_id
31
58
 
32
- # @!attribute license_key
33
- # @return [String] MaxMind license key that is used for authorization
59
+ # The MaxMind license key that is used for authorization.
60
+ #
61
+ # @return [String, nil]
34
62
  attr_accessor :license_key
35
63
 
36
- # @yield [self] to accept configuration settings
64
+ # @!visibility private
65
+ attr_reader :connection
66
+
67
+ # Yield self to accept configuration settings.
68
+ #
69
+ # @yield [self]
37
70
  def configure
38
71
  yield self
72
+
73
+ config = Minfraud::HTTPService.configuration
74
+ @connection = Faraday.new(config[:server], {}, &config[:middleware])
39
75
  end
40
76
 
41
- # @return [Hash] current Minfraud configuration
77
+ # The current Minfraud configuration.
78
+ #
79
+ # @deprecated This will be removed in the next major version.
80
+ #
81
+ # @return [Hash]
42
82
  def configuration
43
83
  {
44
84
  user_id: @user_id,
@@ -1,90 +1,159 @@
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/
2
8
  class Assessments
3
9
  include ::Minfraud::HTTPService
4
10
  include ::Minfraud::Resolver
5
11
 
6
- # @attribute account
7
- # @return [Minfraud::Components::Account] Account component
12
+ # The Account component.
13
+ #
14
+ # @return [Minfraud::Components::Account, nil]
8
15
  attr_accessor :account
9
16
 
10
- # @attribute billing
11
- # @return [Minfraud::Components::Billing] Billing component
17
+ # The Billing component.
18
+ #
19
+ # @return [Minfraud::Components::Billing, nil]
12
20
  attr_accessor :billing
13
21
 
14
- # @attribute credit_card
15
- # @return [Minfraud::Components::CreditCard] CreditCard component
22
+ # The CreditCard component.
23
+ #
24
+ # @return [Minfraud::Components::CreditCard, nil]
16
25
  attr_accessor :credit_card
17
26
 
18
- # @attribute device
19
- # @return [Minfraud::Components::Device] Device component
27
+ # The CustomInputs component.
28
+ #
29
+ # @return [Minfraud::Components::CustomInputs, nil]
30
+ attr_accessor :custom_inputs
31
+
32
+ # The Device component.
33
+ #
34
+ # @return [Minfraud::Components::Device, nil]
20
35
  attr_accessor :device
21
36
 
22
- # @attribute email
23
- # @return [Minfraud::Components::Email] Email component
37
+ # The Email component.
38
+ #
39
+ # @return [Minfraud::Components::Email, nil]
24
40
  attr_accessor :email
25
41
 
26
- # @attribute event
27
- # @return [Minfraud::Components::Event] Event component
42
+ # The Event component.
43
+ #
44
+ # @return [Minfraud::Components::Event, nil]
28
45
  attr_accessor :event
29
46
 
30
- # @attribute order
31
- # @return [Minfraud::Components::Order] Order component
47
+ # The Order component.
48
+ #
49
+ # @return [Minfraud::Components::Order, nil]
32
50
  attr_accessor :order
33
51
 
34
- # @attribute payment
35
- # @return [Minfraud::Components::Payment] Payment component
52
+ # The Payment component.
53
+ #
54
+ # @return [Minfraud::Components::Payment, nil]
36
55
  attr_accessor :payment
37
56
 
38
- # @!attribute shipping
39
- # @return [Minfraud::Components::Shipping] Shipping component
57
+ # The Shipping component.
58
+ #
59
+ # @return [Minfraud::Components::Shipping, nil]
40
60
  attr_accessor :shipping
41
61
 
42
- # @!attribute shopping_cart
43
- # @return [Minfraud::Components::ShoppingCarat] ShoppingCart component
62
+ # The ShoppingCart component.
63
+ #
64
+ # @return [Minfraud::Components::ShoppingCart, nil]
44
65
  attr_accessor :shopping_cart
45
66
 
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
67
+ # @param params [Hash] Hash of parameters. Each key is a symbol
68
+ # corresponding to one of the available component attributes. Values may
69
+ # be component objects or hashes that will be provided to the component
70
+ # constructors.
71
+ #
72
+ # @param resolver [Minfraud::Resolver] Resolver that maps parameters to
73
+ # components.
50
74
  def initialize(params = {}, resolver = ::Minfraud::Resolver)
75
+ @locales = params.delete('locales')
76
+ @locales = ['en'] if @locales.nil?
77
+
51
78
  resolver.assign(self, params)
52
79
  end
53
80
 
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
- )
67
-
68
- ::Minfraud::ErrorHandler.inspect(response)
69
- end
81
+ # Perform a minFraud Factors request.
82
+ #
83
+ # @return [Minfraud::HTTPService::Response]
84
+ #
85
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
86
+ # problem.
87
+ #
88
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
89
+ # of your inputs.
90
+ #
91
+ # @raise [Minfraud::ServerError] If the server reported an error of some
92
+ # kind.
93
+ def factors
94
+ perform_request(:factors)
70
95
  end
71
96
 
72
- define :score
73
- define :insights
74
- define :factors
97
+ # Perform a minFraud Insights request.
98
+ #
99
+ # @return [Minfraud::HTTPService::Response]
100
+ #
101
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
102
+ # problem.
103
+ #
104
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
105
+ # of your inputs.
106
+ #
107
+ # @raise [Minfraud::ServerError] If the server reported an error of some
108
+ # kind.
109
+ def insights
110
+ perform_request(:insights)
111
+ end
112
+
113
+ # Perform a minFraud Score request.
114
+ #
115
+ # @return [Minfraud::HTTPService::Response]
116
+ #
117
+ # @raise [Minfraud::AuthorizationError] If there was an authentication
118
+ # problem.
119
+ #
120
+ # @raise [Minfraud::ClientError] If there was a critical problem with one
121
+ # of your inputs.
122
+ #
123
+ # @raise [Minfraud::ServerError] If the server reported an error of some
124
+ # kind.
125
+ def score
126
+ perform_request(:score)
127
+ end
75
128
 
76
129
  private
77
- # Creates a unified request body from components converted to JSON
78
- # @return [Hash] Request body
130
+
131
+ def perform_request(endpoint)
132
+ raw = request.perform(
133
+ verb: :post,
134
+ endpoint: endpoint.to_s,
135
+ body: request_body,
136
+ )
137
+
138
+ response = ::Minfraud::HTTPService::Response.new(
139
+ endpoint: endpoint,
140
+ locales: @locales,
141
+ status: raw.status.to_i,
142
+ body: raw.body,
143
+ headers: raw.headers
144
+ )
145
+
146
+ ::Minfraud::ErrorHandler.examine(response)
147
+ end
148
+
79
149
  def request_body
80
- MAPPING.keys.inject({}) do |mem, e|
81
- next mem unless value = send(e)
150
+ MAPPING.keys.reduce({}) do |mem, e|
151
+ next mem unless (value = send(e))
152
+
82
153
  mem.merge!(e.to_s => value.to_json)
83
154
  end
84
155
  end
85
156
 
86
- # Creates memoized Minfraud::HTTPService::Request instance
87
- # @return [Minfraud::HTTPService::Request] Request instance based on configuration params
88
157
  def request
89
158
  @request ||= Request.new(::Minfraud::HTTPService.configuration)
90
159
  end
@@ -1,22 +1,44 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
2
4
  module Components
5
+ # Account corresponds to the account object of a minFraud request.
6
+ #
7
+ # @see https://dev.maxmind.com/minfraud/#Account_(/account)
3
8
  class Account < Base
4
- # @attribute user_id
5
- # @return [String] A unique user ID associated with the end-user in your system.
6
- # If your system allows the login name for the account to be changed, this should not be the login name for the account,
7
- # but rather should be an internal ID that does not change. This is not your MaxMind user ID
9
+ include Minfraud::Validates
10
+
11
+ # A unique user ID associated with the end-user in your system. If your
12
+ # system allows the login name for the account to be changed, this should
13
+ # not be the login name for the account, but rather should be an internal
14
+ # ID that does not change. This is not your MaxMind account ID. No
15
+ # specific format is required.
16
+ #
17
+ # @return [String, nil]
8
18
  attr_accessor :user_id
9
19
 
10
- # @attribute username_md5
11
- # @return [String] An MD5 hash as a hexadecimal string of the username or login name associated with the account
20
+ # An MD5 hash as a hexadecimal string of the username or login name
21
+ # associated with the account.
22
+ #
23
+ # @return [String, nil]
12
24
  attr_accessor :username_md5
13
25
 
14
- # Creates Minfraud::Components::Account instance
15
- # @param [Hash] params hash of parameters
16
- # @return [Minfraud::Components::Account] an Account instance
26
+ # @param params [Hash] Hash of parameters. Each key/value should
27
+ # correspond to one of the available attributes.
17
28
  def initialize(params = {})
18
29
  @user_id = params[:user_id]
19
30
  @username_md5 = params[:username_md5]
31
+
32
+ validate
33
+ end
34
+
35
+ private
36
+
37
+ def validate
38
+ return if !Minfraud.enable_validation
39
+
40
+ validate_string('user_id', 255, @user_id)
41
+ validate_md5('username_md5', @username_md5)
20
42
  end
21
43
  end
22
44
  end
@@ -1,54 +1,81 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Minfraud
2
4
  module Components
5
+ # This is a parent class for the Billing and Shipping components.
3
6
  class Addressable < Base
4
- # @attribute first_name
5
- # @return [String] The first name of the end user as provided in their billing / shipping information
7
+ include Minfraud::Validates
8
+
9
+ # The first name of the end user as provided in their billing / shipping
10
+ # information.
11
+ #
12
+ # @return [String, nil]
6
13
  attr_accessor :first_name
7
14
 
8
- # @attribute last_name
9
- # @return [String] The last name of the end user as provided in their billing / shipping information
15
+ # The last name of the end user as provided in their billing / shipping
16
+ # information.
17
+ #
18
+ # @return [String, nil]
10
19
  attr_accessor :last_name
11
20
 
12
- # @attribute company
13
- # @return [String] The company of the end user as provided in their billing / shipping information
21
+ # The company of the end user as provided in their billing / shipping
22
+ # information.
23
+ #
24
+ # @return [String, nil]
14
25
  attr_accessor :company
15
26
 
16
- # @attribute address
17
- # @return [String] The first line of the user’s billing / shipping address
27
+ # The first line of the user's billing / shipping address.
28
+ #
29
+ # @return [String, nil]
18
30
  attr_accessor :address
19
31
 
20
- # @attribute address_2
21
- # @return [String] The second line of the user’s billing / shipping address
32
+ # The second line of the user's billing / shipping address.
33
+ #
34
+ # @return [String, nil]
22
35
  attr_accessor :address_2
23
36
 
24
- # @attribute city
25
- # @return [String] The city of the user’s billing / shipping address
37
+ # The city of the user's billing / shipping address.
38
+ #
39
+ # @return [String, nil]
26
40
  attr_accessor :city
27
41
 
28
- # @attribute region
29
- # @return [String] The ISO 3166-2 subdivision code for the user’s billing / shipping address
42
+ # The ISO 3166-2 subdivision code for the user's billing / shipping
43
+ # address.
44
+ #
45
+ # @see https://en.wikipedia.org/wiki/ISO_3166-2
46
+ #
47
+ # @return [String, nil]
30
48
  attr_accessor :region
31
49
 
32
- # @attribute country
33
- # @return [String] The two character ISO 3166-1 alpha-2 country code of the user’s billing / shipping address
50
+ # The two character ISO 3166-1 alpha-2 country code of the user's billing
51
+ # / shipping address.
52
+ #
53
+ # @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
54
+ #
55
+ # @return [String, nil]
34
56
  attr_accessor :country
35
57
 
36
- # @attribute postal
37
- # @return [String] The postal code of the user’s billing / shipping address
58
+ # The postal code of the user's billing / shipping address.
59
+ #
60
+ # @return [String, nil]
38
61
  attr_accessor :postal
39
62
 
40
- # @attribute phone_number
41
- # @return [String] The phone number without the country code for the user’s billing / shipping address
63
+ # The phone number without the country code for the user's billing /
64
+ # shipping address. Punctuation characters will be stripped. After
65
+ # stripping punctuation characters, the number must contain only digits.
66
+ #
67
+ # @return [String, nil]
42
68
  attr_accessor :phone_number
43
69
 
44
- # @attribute phone_country_code
45
- # @return [String] The country code for phone number associated with the user’s billing / shipping address
70
+ # The country code for the phone number associated with the user's
71
+ # billing / shipping address. If you provide this information then you
72
+ # must provide at least one digit.
73
+ #
74
+ # @return [String, nil]
46
75
  attr_accessor :phone_country_code
47
76
 
48
- # Creates Minfraud::Components::Addressable instance
49
- # @note This class is used as a parent class for Billing and Shipping components
50
- # @param [Hash] params hash of parameters
51
- # @return [Minfraud::Components::Addressable] an Addressable instance
77
+ # @param params [Hash] Hash of parameters. Each key/value should
78
+ # correspond to one of the available attributes.
52
79
  def initialize(params = {})
53
80
  @first_name = params[:first_name]
54
81
  @last_name = params[:last_name]
@@ -61,6 +88,26 @@ module Minfraud
61
88
  @postal = params[:postal]
62
89
  @phone_number = params[:phone_number]
63
90
  @phone_country_code = params[:phone_country_code]
91
+
92
+ validate
93
+ end
94
+
95
+ private
96
+
97
+ def validate
98
+ return if !Minfraud.enable_validation
99
+
100
+ validate_string('first_name', 255, @first_name)
101
+ validate_string('last_name', 255, @last_name)
102
+ validate_string('company', 255, @company)
103
+ validate_string('address', 255, @address)
104
+ validate_string('address_2', 255, @address_2)
105
+ validate_string('city', 255, @city)
106
+ validate_subdivision_code('region', @region)
107
+ validate_country_code('country', @country)
108
+ validate_string('postal', 255, @postal)
109
+ validate_string('phone_number', 255, @phone_number)
110
+ validate_telephone_country_code('phone_country_code', @phone_country_code)
64
111
  end
65
112
  end
66
113
  end