minfraud 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,58 +1,76 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Minfraud
4
+ # Assessments is used to perform minFraud Score, Insights, and Factors
5
+ # requests.
6
+ #
7
+ # @see https://dev.maxmind.com/minfraud/
4
8
  class Assessments
5
9
  include ::Minfraud::HTTPService
6
10
  include ::Minfraud::Resolver
7
11
 
8
- # @attribute account
9
- # @return [Minfraud::Components::Account] Account component
12
+ # The Account component.
13
+ #
14
+ # @return [Minfraud::Components::Account, nil]
10
15
  attr_accessor :account
11
16
 
12
- # @attribute billing
13
- # @return [Minfraud::Components::Billing] Billing component
17
+ # The Billing component.
18
+ #
19
+ # @return [Minfraud::Components::Billing, nil]
14
20
  attr_accessor :billing
15
21
 
16
- # @attribute credit_card
17
- # @return [Minfraud::Components::CreditCard] CreditCard component
22
+ # The CreditCard component.
23
+ #
24
+ # @return [Minfraud::Components::CreditCard, nil]
18
25
  attr_accessor :credit_card
19
26
 
20
- # @attribute custom_inputs
21
- # @return [Minfraud::Components::CustomInputs] CustomInputs component
27
+ # The CustomInputs component.
28
+ #
29
+ # @return [Minfraud::Components::CustomInputs, nil]
22
30
  attr_accessor :custom_inputs
23
31
 
24
- # @attribute device
25
- # @return [Minfraud::Components::Device] Device component
32
+ # The Device component.
33
+ #
34
+ # @return [Minfraud::Components::Device, nil]
26
35
  attr_accessor :device
27
36
 
28
- # @attribute email
29
- # @return [Minfraud::Components::Email] Email component
37
+ # The Email component.
38
+ #
39
+ # @return [Minfraud::Components::Email, nil]
30
40
  attr_accessor :email
31
41
 
32
- # @attribute event
33
- # @return [Minfraud::Components::Event] Event component
42
+ # The Event component.
43
+ #
44
+ # @return [Minfraud::Components::Event, nil]
34
45
  attr_accessor :event
35
46
 
36
- # @attribute order
37
- # @return [Minfraud::Components::Order] Order component
47
+ # The Order component.
48
+ #
49
+ # @return [Minfraud::Components::Order, nil]
38
50
  attr_accessor :order
39
51
 
40
- # @attribute payment
41
- # @return [Minfraud::Components::Payment] Payment component
52
+ # The Payment component.
53
+ #
54
+ # @return [Minfraud::Components::Payment, nil]
42
55
  attr_accessor :payment
43
56
 
44
- # @!attribute shipping
45
- # @return [Minfraud::Components::Shipping] Shipping component
57
+ # The Shipping component.
58
+ #
59
+ # @return [Minfraud::Components::Shipping, nil]
46
60
  attr_accessor :shipping
47
61
 
48
- # @!attribute shopping_cart
49
- # @return [Minfraud::Components::ShoppingCart] ShoppingCart component
62
+ # The ShoppingCart component.
63
+ #
64
+ # @return [Minfraud::Components::ShoppingCart, nil]
50
65
  attr_accessor :shopping_cart
51
66
 
52
- # @param [Hash] params hash of parameters
53
- # @param [Minfraud::Resolver] resolver resolver that maps params to components
54
- # @note In case when params is a Hash of components it just assigns them to the corresponding instance variables
55
- # @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.
56
74
  def initialize(params = {}, resolver = ::Minfraud::Resolver)
57
75
  @locales = params.delete('locales')
58
76
  @locales = ['en'] if @locales.nil?
@@ -60,34 +78,74 @@ module Minfraud
60
78
  resolver.assign(self, params)
61
79
  end
62
80
 
63
- # @!macro [attach] define
64
- # @method $1
65
- # Makes a request to minFraud $1 endpoint.
66
- # Raises an error in case of invalid response
67
- # @return [Minfraud::HTTPService::Response] Wrapped minFraud response
68
- def self.define(endpoint)
69
- define_method(endpoint) do
70
- raw = request.perform(verb: :post, endpoint: endpoint.to_s, body: request_body)
71
- response = ::Minfraud::HTTPService::Response.new(
72
- endpoint: endpoint,
73
- locales: @locales,
74
- status: raw.status.to_i,
75
- body: raw.body,
76
- headers: raw.headers
77
- )
78
-
79
- ::Minfraud::ErrorHandler.examine(response)
80
- 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)
95
+ end
96
+
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)
81
111
  end
82
112
 
83
- define :score
84
- define :insights
85
- define :factors
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
86
128
 
87
129
  private
88
130
 
89
- # Creates a unified request body from components converted to JSON
90
- # @return [Hash] Request body
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
+
91
149
  def request_body
92
150
  MAPPING.keys.reduce({}) do |mem, e|
93
151
  next mem unless (value = send(e))
@@ -96,8 +154,6 @@ module Minfraud
96
154
  end
97
155
  end
98
156
 
99
- # Creates memoized Minfraud::HTTPService::Request instance
100
- # @return [Minfraud::HTTPService::Request] Request instance based on configuration params
101
157
  def request
102
158
  @request ||= Request.new(::Minfraud::HTTPService.configuration)
103
159
  end
@@ -2,23 +2,43 @@
2
2
 
3
3
  module Minfraud
4
4
  module Components
5
+ # Account corresponds to the account object of a minFraud request.
6
+ #
7
+ # @see https://dev.maxmind.com/minfraud/#Account_(/account)
5
8
  class Account < Base
6
- # @attribute user_id
7
- # @return [String] A unique user ID associated with the end-user in your system.
8
- # If your system allows the login name for the account to be changed, this should not be the login name for the account,
9
- # 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]
10
18
  attr_accessor :user_id
11
19
 
12
- # @attribute username_md5
13
- # @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]
14
24
  attr_accessor :username_md5
15
25
 
16
- # Creates Minfraud::Components::Account instance
17
- # @param [Hash] params hash of parameters
18
- # @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.
19
28
  def initialize(params = {})
20
29
  @user_id = params[:user_id]
21
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)
22
42
  end
23
43
  end
24
44
  end
@@ -2,55 +2,80 @@
2
2
 
3
3
  module Minfraud
4
4
  module Components
5
+ # This is a parent class for the Billing and Shipping components.
5
6
  class Addressable < Base
6
- # @attribute first_name
7
- # @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]
8
13
  attr_accessor :first_name
9
14
 
10
- # @attribute last_name
11
- # @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]
12
19
  attr_accessor :last_name
13
20
 
14
- # @attribute company
15
- # @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]
16
25
  attr_accessor :company
17
26
 
18
- # @attribute address
19
- # @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]
20
30
  attr_accessor :address
21
31
 
22
- # @attribute address_2
23
- # @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]
24
35
  attr_accessor :address_2
25
36
 
26
- # @attribute city
27
- # @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]
28
40
  attr_accessor :city
29
41
 
30
- # @attribute region
31
- # @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]
32
48
  attr_accessor :region
33
49
 
34
- # @attribute country
35
- # @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]
36
56
  attr_accessor :country
37
57
 
38
- # @attribute postal
39
- # @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]
40
61
  attr_accessor :postal
41
62
 
42
- # @attribute phone_number
43
- # @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]
44
68
  attr_accessor :phone_number
45
69
 
46
- # @attribute phone_country_code
47
- # @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]
48
75
  attr_accessor :phone_country_code
49
76
 
50
- # Creates Minfraud::Components::Addressable instance
51
- # @note This class is used as a parent class for Billing and Shipping components
52
- # @param [Hash] params hash of parameters
53
- # @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.
54
79
  def initialize(params = {})
55
80
  @first_name = params[:first_name]
56
81
  @last_name = params[:last_name]
@@ -63,6 +88,26 @@ module Minfraud
63
88
  @postal = params[:postal]
64
89
  @phone_number = params[:phone_number]
65
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)
66
111
  end
67
112
  end
68
113
  end
@@ -2,20 +2,26 @@
2
2
 
3
3
  module Minfraud
4
4
  module Components
5
- # @note This class is used as a parent class for all other components
6
- # @note It defines a method which is used for basic JSON representation of PORO objects
5
+ # This is a parent class for all components. It defines a method which is
6
+ # used for basic JSON representation of the component objects.
7
7
  class Base
8
- # @return [Hash] a JSON representation of component attributes
8
+ # A JSON representation of component attributes.
9
+ #
10
+ # @return [Hash]
9
11
  def to_json(*_args)
10
12
  instance_variables.reduce({}) { |mem, e| populate!(mem, e) }
11
13
  end
12
14
 
13
15
  private
14
16
 
15
- # @note This method may modify passed hash. Non-existing instance variables are ignored
16
- # @param [Hash] hash an accumulator
17
- # @param [Symbol] v_sym an instance variable symbol
18
- # @return [Hash] a hash containing a JSON representation of instance variable name and it's value
17
+ # Create a hash containing a JSON representation of instance variable
18
+ # name and its value.
19
+ #
20
+ # @param hash [Hash] An accumulator.
21
+ #
22
+ # @param v_sym [Symbol] An instance variable symbol.
23
+ #
24
+ # @return [Hash]
19
25
  def populate!(hash, v_sym)
20
26
  return hash unless (value = instance_variable_get(v_sym))
21
27
 
@@ -23,9 +29,13 @@ module Minfraud
23
29
  hash.merge!(key => represent(key, value))
24
30
  end
25
31
 
26
- # param [Symbol] key instance variable symbol
27
- # param [Object] value instance variable value
28
- # @return [Object] value representation according to the request format
32
+ # Return the value according to the request format.
33
+ #
34
+ # @param key [Symbol] An instance variable symbol.
35
+ #
36
+ # @param value [Object] An instance variable value.
37
+ #
38
+ # @return [Object]
29
39
  def represent(key, value)
30
40
  BOOLS.include?(key) ? value : value.to_s
31
41
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Minfraud
4
4
  module Components
5
+ # Billing corresponds to the billing object of a minFraud request.
6
+ #
7
+ # @see https://dev.maxmind.com/minfraud/#Billing_(/billing)
5
8
  class Billing < Addressable; end
6
9
  end
7
10
  end