minfraud-ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +340 -0
- data/README.md +100 -0
- data/Rakefile +11 -0
- data/lib/minfraud/minfraud.rb +71 -0
- data/lib/minfraud/request.rb +69 -0
- data/lib/minfraud/response.rb +71 -0
- data/lib/minfraud/transaction.rb +113 -0
- data/lib/minfraud/version.rb +3 -0
- data/lib/minfraud-ruby.rb +5 -0
- data/minfraud-ruby.gemspec +27 -0
- data/spec/minfraud/minfraud_spec.rb +80 -0
- data/spec/minfraud/request_spec.rb +54 -0
- data/spec/minfraud/response_spec.rb +31 -0
- data/spec/minfraud/transaction_spec.rb +142 -0
- data/spec/spec_helper.rb +9 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 70984ef59b83860d9aa8683045bf0f7d38b2dfbb
|
4
|
+
data.tar.gz: a51d52744dbd3c64531d086812014a1d9baeeb6b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50015aca3d50f32f23fe9a422c65c5ecc6625846076e00197030a174f87ff391ef694d100d2ea1c8ad9d02943cc544f8e066e759966936dbd99337b1307f8d31
|
7
|
+
data.tar.gz: 5a1ecf78b729dccef28f2e69d10d8e6496a7399575c6cfbe44f20d57ca3baef4c6f88242cbee84edb1f585caa0db04aac903341c2a64c31f1534c544a4a52696
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,340 @@
|
|
1
|
+
GNU GENERAL PUBLIC LICENSE
|
2
|
+
Version 2, June 1991
|
3
|
+
|
4
|
+
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
5
|
+
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies
|
7
|
+
of this license document, but changing it is not allowed.
|
8
|
+
|
9
|
+
Preamble
|
10
|
+
|
11
|
+
The licenses for most software are designed to take away your
|
12
|
+
freedom to share and change it. By contrast, the GNU General Public
|
13
|
+
License is intended to guarantee your freedom to share and change free
|
14
|
+
software--to make sure the software is free for all its users. This
|
15
|
+
General Public License applies to most of the Free Software
|
16
|
+
Foundation's software and to any other program whose authors commit to
|
17
|
+
using it. (Some other Free Software Foundation software is covered by
|
18
|
+
the GNU Library General Public License instead.) You can apply it to
|
19
|
+
your programs, too.
|
20
|
+
|
21
|
+
When we speak of free software, we are referring to freedom, not
|
22
|
+
price. Our General Public Licenses are designed to make sure that you
|
23
|
+
have the freedom to distribute copies of free software (and charge for
|
24
|
+
this service if you wish), that you receive source code or can get it
|
25
|
+
if you want it, that you can change the software or use pieces of it
|
26
|
+
in new free programs; and that you know you can do these things.
|
27
|
+
|
28
|
+
To protect your rights, we need to make restrictions that forbid
|
29
|
+
anyone to deny you these rights or to ask you to surrender the rights.
|
30
|
+
These restrictions translate to certain responsibilities for you if you
|
31
|
+
distribute copies of the software, or if you modify it.
|
32
|
+
|
33
|
+
For example, if you distribute copies of such a program, whether
|
34
|
+
gratis or for a fee, you must give the recipients all the rights that
|
35
|
+
you have. You must make sure that they, too, receive or can get the
|
36
|
+
source code. And you must show them these terms so they know their
|
37
|
+
rights.
|
38
|
+
|
39
|
+
We protect your rights with two steps: (1) copyright the software, and
|
40
|
+
(2) offer you this license which gives you legal permission to copy,
|
41
|
+
distribute and/or modify the software.
|
42
|
+
|
43
|
+
Also, for each author's protection and ours, we want to make certain
|
44
|
+
that everyone understands that there is no warranty for this free
|
45
|
+
software. If the software is modified by someone else and passed on, we
|
46
|
+
want its recipients to know that what they have is not the original, so
|
47
|
+
that any problems introduced by others will not reflect on the original
|
48
|
+
authors' reputations.
|
49
|
+
|
50
|
+
Finally, any free program is threatened constantly by software
|
51
|
+
patents. We wish to avoid the danger that redistributors of a free
|
52
|
+
program will individually obtain patent licenses, in effect making the
|
53
|
+
program proprietary. To prevent this, we have made it clear that any
|
54
|
+
patent must be licensed for everyone's free use or not licensed at all.
|
55
|
+
|
56
|
+
The precise terms and conditions for copying, distribution and
|
57
|
+
modification follow.
|
58
|
+
|
59
|
+
GNU GENERAL PUBLIC LICENSE
|
60
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
61
|
+
|
62
|
+
0. This License applies to any program or other work which contains
|
63
|
+
a notice placed by the copyright holder saying it may be distributed
|
64
|
+
under the terms of this General Public License. The "Program", below,
|
65
|
+
refers to any such program or work, and a "work based on the Program"
|
66
|
+
means either the Program or any derivative work under copyright law:
|
67
|
+
that is to say, a work containing the Program or a portion of it,
|
68
|
+
either verbatim or with modifications and/or translated into another
|
69
|
+
language. (Hereinafter, translation is included without limitation in
|
70
|
+
the term "modification".) Each licensee is addressed as "you".
|
71
|
+
|
72
|
+
Activities other than copying, distribution and modification are not
|
73
|
+
covered by this License; they are outside its scope. The act of
|
74
|
+
running the Program is not restricted, and the output from the Program
|
75
|
+
is covered only if its contents constitute a work based on the
|
76
|
+
Program (independent of having been made by running the Program).
|
77
|
+
Whether that is true depends on what the Program does.
|
78
|
+
|
79
|
+
1. You may copy and distribute verbatim copies of the Program's
|
80
|
+
source code as you receive it, in any medium, provided that you
|
81
|
+
conspicuously and appropriately publish on each copy an appropriate
|
82
|
+
copyright notice and disclaimer of warranty; keep intact all the
|
83
|
+
notices that refer to this License and to the absence of any warranty;
|
84
|
+
and give any other recipients of the Program a copy of this License
|
85
|
+
along with the Program.
|
86
|
+
|
87
|
+
You may charge a fee for the physical act of transferring a copy, and
|
88
|
+
you may at your option offer warranty protection in exchange for a fee.
|
89
|
+
|
90
|
+
2. You may modify your copy or copies of the Program or any portion
|
91
|
+
of it, thus forming a work based on the Program, and copy and
|
92
|
+
distribute such modifications or work under the terms of Section 1
|
93
|
+
above, provided that you also meet all of these conditions:
|
94
|
+
|
95
|
+
a) You must cause the modified files to carry prominent notices
|
96
|
+
stating that you changed the files and the date of any change.
|
97
|
+
|
98
|
+
b) You must cause any work that you distribute or publish, that in
|
99
|
+
whole or in part contains or is derived from the Program or any
|
100
|
+
part thereof, to be licensed as a whole at no charge to all third
|
101
|
+
parties under the terms of this License.
|
102
|
+
|
103
|
+
c) If the modified program normally reads commands interactively
|
104
|
+
when run, you must cause it, when started running for such
|
105
|
+
interactive use in the most ordinary way, to print or display an
|
106
|
+
announcement including an appropriate copyright notice and a
|
107
|
+
notice that there is no warranty (or else, saying that you provide
|
108
|
+
a warranty) and that users may redistribute the program under
|
109
|
+
these conditions, and telling the user how to view a copy of this
|
110
|
+
License. (Exception: if the Program itself is interactive but
|
111
|
+
does not normally print such an announcement, your work based on
|
112
|
+
the Program is not required to print an announcement.)
|
113
|
+
|
114
|
+
These requirements apply to the modified work as a whole. If
|
115
|
+
identifiable sections of that work are not derived from the Program,
|
116
|
+
and can be reasonably considered independent and separate works in
|
117
|
+
themselves, then this License, and its terms, do not apply to those
|
118
|
+
sections when you distribute them as separate works. But when you
|
119
|
+
distribute the same sections as part of a whole which is a work based
|
120
|
+
on the Program, the distribution of the whole must be on the terms of
|
121
|
+
this License, whose permissions for other licensees extend to the
|
122
|
+
entire whole, and thus to each and every part regardless of who wrote it.
|
123
|
+
|
124
|
+
Thus, it is not the intent of this section to claim rights or contest
|
125
|
+
your rights to work written entirely by you; rather, the intent is to
|
126
|
+
exercise the right to control the distribution of derivative or
|
127
|
+
collective works based on the Program.
|
128
|
+
|
129
|
+
In addition, mere aggregation of another work not based on the Program
|
130
|
+
with the Program (or with a work based on the Program) on a volume of
|
131
|
+
a storage or distribution medium does not bring the other work under
|
132
|
+
the scope of this License.
|
133
|
+
|
134
|
+
3. You may copy and distribute the Program (or a work based on it,
|
135
|
+
under Section 2) in object code or executable form under the terms of
|
136
|
+
Sections 1 and 2 above provided that you also do one of the following:
|
137
|
+
|
138
|
+
a) Accompany it with the complete corresponding machine-readable
|
139
|
+
source code, which must be distributed under the terms of Sections
|
140
|
+
1 and 2 above on a medium customarily used for software interchange; or,
|
141
|
+
|
142
|
+
b) Accompany it with a written offer, valid for at least three
|
143
|
+
years, to give any third party, for a charge no more than your
|
144
|
+
cost of physically performing source distribution, a complete
|
145
|
+
machine-readable copy of the corresponding source code, to be
|
146
|
+
distributed under the terms of Sections 1 and 2 above on a medium
|
147
|
+
customarily used for software interchange; or,
|
148
|
+
|
149
|
+
c) Accompany it with the information you received as to the offer
|
150
|
+
to distribute corresponding source code. (This alternative is
|
151
|
+
allowed only for noncommercial distribution and only if you
|
152
|
+
received the program in object code or executable form with such
|
153
|
+
an offer, in accord with Subsection b above.)
|
154
|
+
|
155
|
+
The source code for a work means the preferred form of the work for
|
156
|
+
making modifications to it. For an executable work, complete source
|
157
|
+
code means all the source code for all modules it contains, plus any
|
158
|
+
associated interface definition files, plus the scripts used to
|
159
|
+
control compilation and installation of the executable. However, as a
|
160
|
+
special exception, the source code distributed need not include
|
161
|
+
anything that is normally distributed (in either source or binary
|
162
|
+
form) with the major components (compiler, kernel, and so on) of the
|
163
|
+
operating system on which the executable runs, unless that component
|
164
|
+
itself accompanies the executable.
|
165
|
+
|
166
|
+
If distribution of executable or object code is made by offering
|
167
|
+
access to copy from a designated place, then offering equivalent
|
168
|
+
access to copy the source code from the same place counts as
|
169
|
+
distribution of the source code, even though third parties are not
|
170
|
+
compelled to copy the source along with the object code.
|
171
|
+
|
172
|
+
4. You may not copy, modify, sublicense, or distribute the Program
|
173
|
+
except as expressly provided under this License. Any attempt
|
174
|
+
otherwise to copy, modify, sublicense or distribute the Program is
|
175
|
+
void, and will automatically terminate your rights under this License.
|
176
|
+
However, parties who have received copies, or rights, from you under
|
177
|
+
this License will not have their licenses terminated so long as such
|
178
|
+
parties remain in full compliance.
|
179
|
+
|
180
|
+
5. You are not required to accept this License, since you have not
|
181
|
+
signed it. However, nothing else grants you permission to modify or
|
182
|
+
distribute the Program or its derivative works. These actions are
|
183
|
+
prohibited by law if you do not accept this License. Therefore, by
|
184
|
+
modifying or distributing the Program (or any work based on the
|
185
|
+
Program), you indicate your acceptance of this License to do so, and
|
186
|
+
all its terms and conditions for copying, distributing or modifying
|
187
|
+
the Program or works based on it.
|
188
|
+
|
189
|
+
6. Each time you redistribute the Program (or any work based on the
|
190
|
+
Program), the recipient automatically receives a license from the
|
191
|
+
original licensor to copy, distribute or modify the Program subject to
|
192
|
+
these terms and conditions. You may not impose any further
|
193
|
+
restrictions on the recipients' exercise of the rights granted herein.
|
194
|
+
You are not responsible for enforcing compliance by third parties to
|
195
|
+
this License.
|
196
|
+
|
197
|
+
7. If, as a consequence of a court judgment or allegation of patent
|
198
|
+
infringement or for any other reason (not limited to patent issues),
|
199
|
+
conditions are imposed on you (whether by court order, agreement or
|
200
|
+
otherwise) that contradict the conditions of this License, they do not
|
201
|
+
excuse you from the conditions of this License. If you cannot
|
202
|
+
distribute so as to satisfy simultaneously your obligations under this
|
203
|
+
License and any other pertinent obligations, then as a consequence you
|
204
|
+
may not distribute the Program at all. For example, if a patent
|
205
|
+
license would not permit royalty-free redistribution of the Program by
|
206
|
+
all those who receive copies directly or indirectly through you, then
|
207
|
+
the only way you could satisfy both it and this License would be to
|
208
|
+
refrain entirely from distribution of the Program.
|
209
|
+
|
210
|
+
If any portion of this section is held invalid or unenforceable under
|
211
|
+
any particular circumstance, the balance of the section is intended to
|
212
|
+
apply and the section as a whole is intended to apply in other
|
213
|
+
circumstances.
|
214
|
+
|
215
|
+
It is not the purpose of this section to induce you to infringe any
|
216
|
+
patents or other property right claims or to contest validity of any
|
217
|
+
such claims; this section has the sole purpose of protecting the
|
218
|
+
integrity of the free software distribution system, which is
|
219
|
+
implemented by public license practices. Many people have made
|
220
|
+
generous contributions to the wide range of software distributed
|
221
|
+
through that system in reliance on consistent application of that
|
222
|
+
system; it is up to the author/donor to decide if he or she is willing
|
223
|
+
to distribute software through any other system and a licensee cannot
|
224
|
+
impose that choice.
|
225
|
+
|
226
|
+
This section is intended to make thoroughly clear what is believed to
|
227
|
+
be a consequence of the rest of this License.
|
228
|
+
|
229
|
+
8. If the distribution and/or use of the Program is restricted in
|
230
|
+
certain countries either by patents or by copyrighted interfaces, the
|
231
|
+
original copyright holder who places the Program under this License
|
232
|
+
may add an explicit geographical distribution limitation excluding
|
233
|
+
those countries, so that distribution is permitted only in or among
|
234
|
+
countries not thus excluded. In such case, this License incorporates
|
235
|
+
the limitation as if written in the body of this License.
|
236
|
+
|
237
|
+
9. The Free Software Foundation may publish revised and/or new versions
|
238
|
+
of the General Public License from time to time. Such new versions will
|
239
|
+
be similar in spirit to the present version, but may differ in detail to
|
240
|
+
address new problems or concerns.
|
241
|
+
|
242
|
+
Each version is given a distinguishing version number. If the Program
|
243
|
+
specifies a version number of this License which applies to it and "any
|
244
|
+
later version", you have the option of following the terms and conditions
|
245
|
+
either of that version or of any later version published by the Free
|
246
|
+
Software Foundation. If the Program does not specify a version number of
|
247
|
+
this License, you may choose any version ever published by the Free Software
|
248
|
+
Foundation.
|
249
|
+
|
250
|
+
10. If you wish to incorporate parts of the Program into other free
|
251
|
+
programs whose distribution conditions are different, write to the author
|
252
|
+
to ask for permission. For software which is copyrighted by the Free
|
253
|
+
Software Foundation, write to the Free Software Foundation; we sometimes
|
254
|
+
make exceptions for this. Our decision will be guided by the two goals
|
255
|
+
of preserving the free status of all derivatives of our free software and
|
256
|
+
of promoting the sharing and reuse of software generally.
|
257
|
+
|
258
|
+
NO WARRANTY
|
259
|
+
|
260
|
+
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
261
|
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
262
|
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
263
|
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
264
|
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
265
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
266
|
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
267
|
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
268
|
+
REPAIR OR CORRECTION.
|
269
|
+
|
270
|
+
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
271
|
+
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
272
|
+
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
273
|
+
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
274
|
+
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
275
|
+
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
276
|
+
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
277
|
+
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
278
|
+
POSSIBILITY OF SUCH DAMAGES.
|
279
|
+
|
280
|
+
END OF TERMS AND CONDITIONS
|
281
|
+
|
282
|
+
How to Apply These Terms to Your New Programs
|
283
|
+
|
284
|
+
If you develop a new program, and you want it to be of the greatest
|
285
|
+
possible use to the public, the best way to achieve this is to make it
|
286
|
+
free software which everyone can redistribute and change under these terms.
|
287
|
+
|
288
|
+
To do so, attach the following notices to the program. It is safest
|
289
|
+
to attach them to the start of each source file to most effectively
|
290
|
+
convey the exclusion of warranty; and each file should have at least
|
291
|
+
the "copyright" line and a pointer to where the full notice is found.
|
292
|
+
|
293
|
+
<one line to give the program's name and a brief idea of what it does.>
|
294
|
+
Copyright (C) <year> <name of author>
|
295
|
+
|
296
|
+
This program is free software; you can redistribute it and/or modify
|
297
|
+
it under the terms of the GNU General Public License as published by
|
298
|
+
the Free Software Foundation; either version 2 of the License, or
|
299
|
+
(at your option) any later version.
|
300
|
+
|
301
|
+
This program is distributed in the hope that it will be useful,
|
302
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
303
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
304
|
+
GNU General Public License for more details.
|
305
|
+
|
306
|
+
You should have received a copy of the GNU General Public License
|
307
|
+
along with this program; if not, write to the Free Software
|
308
|
+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
309
|
+
|
310
|
+
|
311
|
+
Also add information on how to contact you by electronic and paper mail.
|
312
|
+
|
313
|
+
If the program is interactive, make it output a short notice like this
|
314
|
+
when it starts in an interactive mode:
|
315
|
+
|
316
|
+
Gnomovision version 69, Copyright (C) year name of author
|
317
|
+
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
318
|
+
This is free software, and you are welcome to redistribute it
|
319
|
+
under certain conditions; type `show c' for details.
|
320
|
+
|
321
|
+
The hypothetical commands `show w' and `show c' should show the appropriate
|
322
|
+
parts of the General Public License. Of course, the commands you use may
|
323
|
+
be called something other than `show w' and `show c'; they could even be
|
324
|
+
mouse-clicks or menu items--whatever suits your program.
|
325
|
+
|
326
|
+
You should also get your employer (if you work as a programmer) or your
|
327
|
+
school, if any, to sign a "copyright disclaimer" for the program, if
|
328
|
+
necessary. Here is a sample; alter the names:
|
329
|
+
|
330
|
+
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
331
|
+
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
332
|
+
|
333
|
+
<signature of Ty Coon>, 1 April 1989
|
334
|
+
Ty Coon, President of Vice
|
335
|
+
|
336
|
+
This General Public License does not permit incorporating your program into
|
337
|
+
proprietary programs. If your program is a subroutine library, you may
|
338
|
+
consider it more useful to permit linking proprietary applications with the
|
339
|
+
library. If this is what you want to do, use the GNU Library General
|
340
|
+
Public License instead of this License.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Ruby interface to the MaxMind minFraud API
|
2
|
+
|
3
|
+
Compatible with version minFraud API v1.3
|
4
|
+
[minFraud API documentation](http://dev.maxmind.com/minfraud/)
|
5
|
+
[minFraud](http://www.maxmind.com/en/ccv_overview)
|
6
|
+
|
7
|
+
[![Build Status](https://travis-ci.org/rdpitts/minfraud-ruby.svg?branch=master)](https://travis-ci.org/rdpitts/minfraud-ruby)
|
8
|
+
[![Code Climate](https://codeclimate.com/github/rdpitts/minfraud-ruby.png)](https://codeclimate.com/github/rdpitts/minfraud-ruby)
|
9
|
+
|
10
|
+
[Rubydoc documentation](http://rubydoc.info/github/rdpitts/minfraud-ruby/master/frames)
|
11
|
+
|
12
|
+
## Configuration
|
13
|
+
|
14
|
+
Your license key is how MaxMind will identify you, so it is required.
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
Minfraud.configure do |c|
|
18
|
+
c.license_key = 'abcd1234'
|
19
|
+
c.requested_type = 'standard'
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
`requested_type` can be set during configuration to use that default value or it can be set on the transaction. If unset minFraud will default to the highest level of service available to you.
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
transaction = Minfraud::Transaction.new do |t|
|
29
|
+
# Required fields
|
30
|
+
# Other fields listed later in documentation are optional
|
31
|
+
t.ip = '1.2.3.4'
|
32
|
+
t.city = 'richmond'
|
33
|
+
t.state = 'virginia'
|
34
|
+
t.postal = '12345'
|
35
|
+
t.country = 'US' # http://en.wikipedia.org/wiki/ISO_3166-1
|
36
|
+
# ...
|
37
|
+
end
|
38
|
+
|
39
|
+
transaction.risk_score
|
40
|
+
# => 3.48
|
41
|
+
```
|
42
|
+
|
43
|
+
### Exception handling
|
44
|
+
|
45
|
+
There are three different exceptions that this gem may raise. Please be prepared to handle them:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# Raised if configuration is invalid
|
49
|
+
class ConfigurationError < ArgumentError; end
|
50
|
+
|
51
|
+
# Raised if a transaction is invalid
|
52
|
+
class TransactionError < ArgumentError; end
|
53
|
+
|
54
|
+
# Raised if minFraud returns an error, or if there is an HTTP error
|
55
|
+
class ResponseError < ArgumentError; end
|
56
|
+
```
|
57
|
+
|
58
|
+
### Transaction fields
|
59
|
+
|
60
|
+
#### Required
|
61
|
+
|
62
|
+
| name | type (length) | example | description |
|
63
|
+
| ------------- | --------------------- | ----------------------------------- | ----------- |
|
64
|
+
| ip | string | `t.ip = '1.2.3.4'` | Customer IP address |
|
65
|
+
| city | string | `t.city = 'new york'` | Customer city |
|
66
|
+
| state | string | `t.state = 'new york'` | Customer state/province/region |
|
67
|
+
| postal | string | `t.postal = '10014'` | Customer zip/postal code |
|
68
|
+
| country | string | `t.country = 'US'` | Customer ISO 3166-1 country code |
|
69
|
+
|
70
|
+
#### Optional
|
71
|
+
|
72
|
+
| name | type (length) | description |
|
73
|
+
| ------------------ | ------------------ | ----------- |
|
74
|
+
| ship_addr | string | |
|
75
|
+
| ship_city | string | |
|
76
|
+
| ship_state | string | |
|
77
|
+
| ship_postal | string | |
|
78
|
+
| ship_country | string | |
|
79
|
+
| email | string | We will hash the email for you |
|
80
|
+
| phone | string | Any format acceptable |
|
81
|
+
| bin | string | CC bin number (first 6 digits) |
|
82
|
+
| session_id | string | Used for linking transactions |
|
83
|
+
| user_agent | string | Used for linking transactions |
|
84
|
+
| accept_language | string | Used for linking transactions |
|
85
|
+
| txn_id | string | Transaction/order id |
|
86
|
+
| amount | string | Transaction amount |
|
87
|
+
| currency | string | ISO 4217 currency code |
|
88
|
+
| txn_type | string | creditcard/debitcard/paypal/google/other/lead/survey/sitereg |
|
89
|
+
| avs_result | string | Standard AVS response code |
|
90
|
+
| cvv_result | string | Y/N |
|
91
|
+
| requested_type | string | standard/premium |
|
92
|
+
| forwarded_ip | string | The end user’s IP address, as forwarded by a transparent proxy |
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
1. Fork it ( http://github.com/rdpitts/minfraud-ruby/fork )
|
97
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
98
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
99
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
100
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
module Minfraud
|
2
|
+
|
3
|
+
# Raised if configuration is invalid
|
4
|
+
class ConfigurationError < ArgumentError; end
|
5
|
+
|
6
|
+
# Raised if a transaction is invalid
|
7
|
+
class TransactionError < ArgumentError; end
|
8
|
+
|
9
|
+
# Raised if minFraud returns an error, or if there is an HTTP error
|
10
|
+
class ResponseError < ArgumentError; end
|
11
|
+
|
12
|
+
# May be used to configure using common block style:
|
13
|
+
#
|
14
|
+
# ```ruby
|
15
|
+
# Minfraud.configure do |c|
|
16
|
+
# c.license_key = 'abcd1234'
|
17
|
+
# end
|
18
|
+
# ```
|
19
|
+
#
|
20
|
+
# @param [Proc] is passed the Minfraud module as its argument
|
21
|
+
# @return [nil, ConfigurationError]
|
22
|
+
def self.configure
|
23
|
+
yield self
|
24
|
+
unless has_required_configuration?
|
25
|
+
raise ConfigurationError, 'You must set license_key so MaxMind can identify you'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Module attribute getter for license_key
|
30
|
+
# This is the MaxMind API consumer's license key.
|
31
|
+
# @return [String, nil] license key if set
|
32
|
+
def self.license_key
|
33
|
+
class_variable_defined?(:@@license_key) ? @@license_key : nil
|
34
|
+
end
|
35
|
+
|
36
|
+
# Module attribute setter for license_key
|
37
|
+
# This is the MaxMind API consumer's license key.
|
38
|
+
# It is required for this gem to work.
|
39
|
+
# @param key [String] license key
|
40
|
+
# @return [String] license key
|
41
|
+
def self.license_key=(key)
|
42
|
+
@@license_key = key
|
43
|
+
end
|
44
|
+
|
45
|
+
# Module attribute getter for requested_type
|
46
|
+
# minFraud service level (standard/premium)
|
47
|
+
# @return [String, nil] service level if set
|
48
|
+
def self.requested_type
|
49
|
+
class_variable_defined?(:@@requested_type) ? @@requested_type : nil
|
50
|
+
end
|
51
|
+
|
52
|
+
# Module attribute setter for requested_type
|
53
|
+
# Desired service level (standard/premium)
|
54
|
+
# @param type [String] service level
|
55
|
+
# @return [String] service level
|
56
|
+
def self.requested_type=(type)
|
57
|
+
@@requested_type = type
|
58
|
+
end
|
59
|
+
|
60
|
+
# MaxMind minFraud API service URI
|
61
|
+
# @return [URI::HTTPS] service uri
|
62
|
+
def self.uri
|
63
|
+
@@uri ||= URI('https://minfraud.maxmind.com/app/ccv2r')
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [Boolean] service URI
|
67
|
+
def self.has_required_configuration?
|
68
|
+
class_variable_defined?(:@@license_key)
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module Minfraud
|
4
|
+
|
5
|
+
class Request
|
6
|
+
|
7
|
+
FIELD_MAP = {
|
8
|
+
ip: 'i',
|
9
|
+
city: 'city',
|
10
|
+
state: 'region',
|
11
|
+
postal: 'postal',
|
12
|
+
country: 'country',
|
13
|
+
license_key: 'license_key',
|
14
|
+
ship_addr: 'shipAddr',
|
15
|
+
ship_city: 'shipCity',
|
16
|
+
ship_state: 'shipRegion',
|
17
|
+
ship_postal: 'shipPostal',
|
18
|
+
ship_country: 'shipCountry',
|
19
|
+
email_domain: 'domain',
|
20
|
+
email_md5: 'emailMD5',
|
21
|
+
phone: 'custPhone',
|
22
|
+
bin: 'bin',
|
23
|
+
session_id: 'sessionID',
|
24
|
+
user_agent: 'user_agent',
|
25
|
+
accept_language: 'accept_language',
|
26
|
+
txn_id: 'txnID',
|
27
|
+
amount: 'order_amount',
|
28
|
+
currency: 'order_currency',
|
29
|
+
txn_type: 'txn_type',
|
30
|
+
avs_result: 'avs_result',
|
31
|
+
cvv_result: 'cvv_result',
|
32
|
+
requested_type: 'requested_type',
|
33
|
+
forwarded_ip: 'forwardedIP'
|
34
|
+
}
|
35
|
+
|
36
|
+
# @param trans [Transaction] transaction to be sent to MaxMind
|
37
|
+
def initialize(trans)
|
38
|
+
@transaction = trans
|
39
|
+
end
|
40
|
+
|
41
|
+
# Sends transaction to MaxMind and gives raw response to Response for handling
|
42
|
+
# @return [Response] wrapper for minFraud response
|
43
|
+
def get
|
44
|
+
Response.new(send_get_request)
|
45
|
+
end
|
46
|
+
|
47
|
+
# (see #get)
|
48
|
+
# @param trans [Transaction] transaction to get to MaxMind
|
49
|
+
def self.get(trans)
|
50
|
+
new(trans).get
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
# Transforms Transaction object into a hash for Net::HTTP::Get
|
56
|
+
# @return [Hash] keys are strings with minFraud field names
|
57
|
+
def encoded_query
|
58
|
+
Hash[@transaction.attributes.map { |k, v| [FIELD_MAP[k], v] }]
|
59
|
+
end
|
60
|
+
|
61
|
+
# @return [Net::HTTPResponse]
|
62
|
+
def send_get_request
|
63
|
+
uri = Minfraud.uri
|
64
|
+
uri.query = URI.encode_www_form(encoded_query)
|
65
|
+
Net::HTTP.get_response(uri)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Minfraud
|
2
|
+
|
3
|
+
# This class wraps the raw minFraud response. Any minFraud response field is accessible on a Response
|
4
|
+
# instance as a snake-cased instance method. For example, if you want the `ip_corporateProxy`
|
5
|
+
# field from minFraud, you can get it with `#ip_corporate_proxy`.
|
6
|
+
class Response
|
7
|
+
|
8
|
+
ERROR_CODES = %w( INVALID_LICENSE_KEY IP_REQUIRED IP_NOT_FOUND LICENSE_REQUIRED COUNTRY_REQUIRED MAX_REQUESTS_REACHED )
|
9
|
+
WARNING_CODES = %w( COUNTRY_NOT_FOUND CITY_NOT_FOUND CITY_REQUIRED POSTAL_CODE_REQUIRED POSTAL_CODE_NOT_FOUND )
|
10
|
+
|
11
|
+
# Sets attributes on self using minFraud response keys and values
|
12
|
+
# Raises an exception if minFraud returns an error message
|
13
|
+
# Does nothing (at the moment) if minFraud returns a warning message
|
14
|
+
# Raises an exception if minFraud responds with anything other than an HTTP success code
|
15
|
+
# @param raw [Net::HTTPResponse]
|
16
|
+
def initialize(raw)
|
17
|
+
raise ResponseError, "The minFraud service responded with http error #{raw.class}" unless raw.is_a? Net::HTTPSuccess
|
18
|
+
decode_body(raw.body)
|
19
|
+
raise ResponseError, "Error message from minFraud: #{error}" if errored?
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
# True if minFraud returns an error (but not a warning), false if not.
|
25
|
+
# @return [Boolean]
|
26
|
+
def errored?
|
27
|
+
ERROR_CODES.include? err
|
28
|
+
end
|
29
|
+
|
30
|
+
# If minFraud sends back an error or warning message, this will return the message, otherwise nil.
|
31
|
+
# @return [String, nil] minFraud error field in response
|
32
|
+
def error
|
33
|
+
err
|
34
|
+
end
|
35
|
+
|
36
|
+
# Parses raw response body and turns its keys and values into attributes on self.
|
37
|
+
# @param body [String] raw response body string
|
38
|
+
def decode_body(body)
|
39
|
+
# We bind the resultant hash to @body for #method_missing
|
40
|
+
@body = transform_keys(Hash[body.split(';').map { |e| e.split('=') }])
|
41
|
+
end
|
42
|
+
|
43
|
+
# Snake cases and symbolizes keys in passed hash.
|
44
|
+
# @param hash [Hash]
|
45
|
+
def transform_keys(hash)
|
46
|
+
hash = hash.to_a
|
47
|
+
hash.map! do |e|
|
48
|
+
key = e.first
|
49
|
+
value = e.last
|
50
|
+
if key.match(/\A[A-Z]+\z/)
|
51
|
+
key = key.downcase
|
52
|
+
else
|
53
|
+
key = key.
|
54
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
55
|
+
gsub(/([a-z])([A-Z])/, '\1_\2').
|
56
|
+
downcase.
|
57
|
+
to_sym
|
58
|
+
end
|
59
|
+
[key, value]
|
60
|
+
end
|
61
|
+
Hash[hash]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Allows keys in hash contained in @body to be used as methods
|
65
|
+
def method_missing(meth, *args, &block)
|
66
|
+
# We're not calling super because we want nil if an attribute isn't found
|
67
|
+
@body[meth]
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
3
|
+
module Minfraud
|
4
|
+
|
5
|
+
# This is the container for the data you're sending to MaxMind.
|
6
|
+
# A transaction holds data like name, address, IP, order amount, etc.
|
7
|
+
class Transaction
|
8
|
+
|
9
|
+
# Required attribute
|
10
|
+
attr_accessor :ip, :city, :state, :postal, :country
|
11
|
+
|
12
|
+
# Shipping address attribute (optional)
|
13
|
+
attr_accessor :ship_addr, :ship_city, :ship_state, :ship_postal, :ship_country
|
14
|
+
|
15
|
+
# User attribute (optional)
|
16
|
+
attr_accessor :email, :phone
|
17
|
+
|
18
|
+
# Credit card attribute (optional)
|
19
|
+
attr_accessor :bin
|
20
|
+
|
21
|
+
# Transaction linking attribute (optional)
|
22
|
+
attr_accessor :session_id, :user_agent, :accept_language
|
23
|
+
|
24
|
+
# Transaction attribute (optional)
|
25
|
+
attr_accessor :txn_id, :amount, :currency, :txn_type
|
26
|
+
|
27
|
+
# Credit card result attribute (optional)
|
28
|
+
attr_accessor :avs_result, :cvv_result
|
29
|
+
|
30
|
+
# Miscellaneous attribute (optional)
|
31
|
+
attr_accessor :requested_type, :forwarded_ip
|
32
|
+
|
33
|
+
def initialize
|
34
|
+
yield self
|
35
|
+
unless has_required_attributes?
|
36
|
+
raise TransactionError, 'You did not set all the required transaction attributes.'
|
37
|
+
end
|
38
|
+
validate_attributes
|
39
|
+
end
|
40
|
+
|
41
|
+
# Retrieves the risk score from MaxMind.
|
42
|
+
# A higher score indicates a higher risk of fraud.
|
43
|
+
# For example, a score of 20 indicates a 20% chance that a transaction is fraudulent.
|
44
|
+
# @return [Float] 0.01 - 100.0
|
45
|
+
def risk_score
|
46
|
+
results.risk_score
|
47
|
+
end
|
48
|
+
|
49
|
+
# Hash of attributes that have been set
|
50
|
+
# @return [Hash] present attributes
|
51
|
+
def attributes
|
52
|
+
attrs = [:ip, :city, :state, :postal, :country, :license_key, :ship_addr, :ship_city, :ship_state, :ship_postal, :ship_country, :email_domain, :email_md5, :phone, :bin, :session_id, :user_agent, :accept_language, :txn_id, :amount, :currency, :txn_type, :avs_result, :cvv_result, :requested_type, :forwarded_ip]
|
53
|
+
attrs.map! do |a|
|
54
|
+
[a, send(a)]
|
55
|
+
end
|
56
|
+
Hash[attrs]
|
57
|
+
end
|
58
|
+
|
59
|
+
# Uses the requested_type set on the instance, or if not present, the requested_type set during configuration
|
60
|
+
# @return [String, nil] requested type
|
61
|
+
def requested_type
|
62
|
+
@requested_type or Minfraud.requested_type
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
# Ensures the required attributes are present
|
68
|
+
# @return [Boolean]
|
69
|
+
def has_required_attributes?
|
70
|
+
ip and city and state and postal and country
|
71
|
+
end
|
72
|
+
|
73
|
+
# Validates the types of the attributes
|
74
|
+
# @return [nil, TransactionError]
|
75
|
+
def validate_attributes
|
76
|
+
[:ip, :city, :state, :postal, :country].each { |s| validate_string(s) }
|
77
|
+
end
|
78
|
+
|
79
|
+
# Given the symbol of an attribute that should be a string,
|
80
|
+
# it checks the attribute's type and throws an error if it's not a string.
|
81
|
+
# @param attr_name [Symbol] name of the attribute to validate
|
82
|
+
# @return [nil, TransactionError]
|
83
|
+
def validate_string(attr_name)
|
84
|
+
attribute = self.send(attr_name)
|
85
|
+
unless attribute.instance_of?(String)
|
86
|
+
raise TransactionError, "Transaction.#{attr_name} must me a string"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# Sends transaction to MaxMind in order to get risk data on it.
|
91
|
+
# Caches response object in @response.
|
92
|
+
# @return [Response]
|
93
|
+
def results
|
94
|
+
@response ||= Request.get(self)
|
95
|
+
end
|
96
|
+
|
97
|
+
# @return [String, nil] domain of the email address
|
98
|
+
def email_domain
|
99
|
+
email.to_s.split('@').last
|
100
|
+
end
|
101
|
+
|
102
|
+
# @return [String, nil] MD5 hash of the whole email address
|
103
|
+
def email_md5
|
104
|
+
Digest::MD5.hexdigest(email.to_s)
|
105
|
+
end
|
106
|
+
|
107
|
+
# @return [String] license key set during configuration
|
108
|
+
def license_key
|
109
|
+
Minfraud.license_key
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'minfraud/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "minfraud-ruby"
|
8
|
+
spec.version = Minfraud::VERSION
|
9
|
+
spec.authors = ["Robbie Pitts"]
|
10
|
+
spec.email = ["robbie@sweatypitts.com"]
|
11
|
+
spec.summary = %q{Ruby interface to the MaxMind minFraud API service.}
|
12
|
+
spec.license = "GNU GPL"
|
13
|
+
spec.homepage = "https://github.com/rdpitts/minfraud-ruby"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.required_ruby_version = '>= 1.9.3'
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "yard"
|
26
|
+
spec.add_development_dependency "ffaker"
|
27
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Minfraud do
|
4
|
+
|
5
|
+
describe '.configure' do
|
6
|
+
it 'yields the Minfraud module' do
|
7
|
+
Minfraud.stub(:has_required_configuration?).and_return(true)
|
8
|
+
Minfraud.configure do |c|
|
9
|
+
expect(c).to eql(Minfraud)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'raises an exception if required fields are not set' do
|
14
|
+
expect { Minfraud.configure { |c| true } }.to raise_exception(Minfraud::ConfigurationError)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.license_key' do
|
19
|
+
let(:license_key) { 'asdf' }
|
20
|
+
|
21
|
+
before { Minfraud.remove_class_variable(:@@license_key) if Minfraud.class_variable_defined?(:@@license_key) }
|
22
|
+
|
23
|
+
it 'gets license key attribute set on Minfraud module' do
|
24
|
+
Minfraud.class_variable_set(:@@license_key, license_key)
|
25
|
+
expect(Minfraud.license_key).to eq(license_key)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns nil when license key attribute is not set' do
|
29
|
+
expect(Minfraud.license_key).to eq(nil)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '.license_key=' do
|
34
|
+
let(:license_key) { 'asdf' }
|
35
|
+
|
36
|
+
before { Minfraud.remove_class_variable(:@@license_key) if Minfraud.class_variable_defined?(:@@license_key) }
|
37
|
+
|
38
|
+
it 'sets license key attribute on Minfraud module' do
|
39
|
+
Minfraud.license_key = license_key
|
40
|
+
expect(Minfraud.class_variable_get(:@@license_key)).to eq(license_key)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '.uri' do
|
45
|
+
after do
|
46
|
+
Minfraud.remove_class_variable(:@@uri)
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'returns a URI::HTTPS object' do
|
50
|
+
expect(Minfraud.uri).to be_instance_of(URI::HTTPS)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'returns URI::HTTPS object containing the minFraud service uri' do
|
54
|
+
expect(Minfraud.uri.to_s).to eq('https://minfraud.maxmind.com/app/ccv2r')
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'caches URI::HTTPS object' do
|
58
|
+
Minfraud.uri
|
59
|
+
expect(Minfraud.class_variable_defined?(:@@uri)).to be_true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '.has_required_configuration?' do
|
64
|
+
let(:license_key) { 'asdf' }
|
65
|
+
|
66
|
+
before do
|
67
|
+
Minfraud.remove_class_variable(:@@license_key) if Minfraud.class_variable_defined?(:@@license_key)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'returns true if license_key is set' do
|
71
|
+
Minfraud.class_variable_set(:@@license_key, license_key)
|
72
|
+
expect(Minfraud.has_required_configuration?).to be_true
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'returns false if license_key is not set' do
|
76
|
+
expect(Minfraud.has_required_configuration?).to be_false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Minfraud::Request do
|
4
|
+
subject(:request) { Minfraud::Request.new(transaction) }
|
5
|
+
let(:transaction) { double(Minfraud::Transaction, attributes: {}) }
|
6
|
+
let(:success_response) { double(Minfraud::Response, errored?: false, body: '') }
|
7
|
+
let(:exception) { Minfraud::ResponseError.new('Message from MaxMind: INVALID_LICENSE_KEY') }
|
8
|
+
|
9
|
+
describe '.new' do
|
10
|
+
it 'binds the @transaction instance variable' do
|
11
|
+
expect(request.instance_variable_get(:@transaction)).to eql(transaction)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#get' do
|
16
|
+
it 'sends appropriately encoded transaction data to minFraud service' do
|
17
|
+
Minfraud.stub(:license_key).and_return('6')
|
18
|
+
Minfraud::Response.stub(:new).and_return(success_response)
|
19
|
+
trans = Minfraud::Transaction.new do |t|
|
20
|
+
t.ip = '1'
|
21
|
+
t.city = '2'
|
22
|
+
t.state = '3'
|
23
|
+
t.postal = '4'
|
24
|
+
t.country = '5'
|
25
|
+
end
|
26
|
+
request_body = {
|
27
|
+
'i' => '1',
|
28
|
+
'city' => '2',
|
29
|
+
'region' => '3',
|
30
|
+
'postal' => '4',
|
31
|
+
'country' => '5',
|
32
|
+
'license_key' => '6'
|
33
|
+
}
|
34
|
+
expect(Net::HTTP).to receive(:get_response) do |uri|
|
35
|
+
expect(uri.to_s).to include(Minfraud.uri.to_s)
|
36
|
+
expect(uri.query).to include(URI.encode_www_form(request_body))
|
37
|
+
end
|
38
|
+
Minfraud::Request.new(trans).get
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'creates Response object out of raw response' do
|
42
|
+
expect(request).to receive(:send_get_request)
|
43
|
+
expect(Minfraud::Response).to receive(:new).and_return(success_response)
|
44
|
+
request.get
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns Response object' do
|
48
|
+
request.stub(:send_get_request)
|
49
|
+
Minfraud::Response.stub(:new).and_return(success_response)
|
50
|
+
expect(request.get).to eql(success_response)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Minfraud::Response do
|
4
|
+
let(:ok_response_double) { double(Net::HTTPOK, body: 'firstKey=first value;second_keyName=second value', is_a?: true) }
|
5
|
+
let(:warning_response_double) { double(Net::HTTPOK, body: 'err=COUNTRY_NOT_FOUND', is_a?: true) }
|
6
|
+
let(:error_response_double) { double(Net::HTTPOK, body: 'err=INVALID_LICENSE_KEY', is_a?: true) }
|
7
|
+
let(:server_error_response) { double(Net::HTTPInternalServerError) }
|
8
|
+
let(:err) { Faker::HipsterIpsum.word }
|
9
|
+
|
10
|
+
describe '.new' do
|
11
|
+
subject(:response) { Minfraud::Response.new(ok_response_double) }
|
12
|
+
|
13
|
+
it 'raises exception without an OK response' do
|
14
|
+
expect { Minfraud::Response.new(server_error_response)}
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'raises exception if minFraud returns an error' do
|
18
|
+
expect { Minfraud::Response.new(error_response_double) }.to raise_exception(Minfraud::ResponseError, /INVALID_LICENSE_KEY/)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'does not raise an exception if minFraud returns a warning' do
|
22
|
+
expect { Minfraud::Response.new(warning_response_double) }.not_to raise_exception
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'turns raw body keys and values into attributes on the object' do
|
26
|
+
expect(response.first_key).to eq('first value')
|
27
|
+
expect(response.second_key_name).to eq('second value')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Minfraud::Transaction do
|
4
|
+
|
5
|
+
describe '.new' do
|
6
|
+
it 'yields the current instance module' do
|
7
|
+
Minfraud::Transaction.new do |t|
|
8
|
+
t.stub(:has_required_attributes?).and_return(true)
|
9
|
+
t.stub(:validate_attributes).and_return(nil)
|
10
|
+
expect(t).to be_an_instance_of(Minfraud::Transaction)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'raises an exception if required attributes are not set' do
|
15
|
+
expect { Minfraud::Transaction.new { |c| true } }.to raise_exception(Minfraud::TransactionError, /required/)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'raises an exception if attributes are invalid' do
|
19
|
+
transaction = lambda do
|
20
|
+
Minfraud::Transaction.new do |t|
|
21
|
+
t.ip = ''
|
22
|
+
t.city = 2
|
23
|
+
t.state = ''
|
24
|
+
t.postal = ''
|
25
|
+
t.country = ''
|
26
|
+
end
|
27
|
+
end
|
28
|
+
expect { transaction.call }.to raise_exception(Minfraud::TransactionError, /city must me a string/)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#risk_score' do
|
33
|
+
subject(:transaction) do
|
34
|
+
Minfraud::Transaction.new do |t|
|
35
|
+
t.stub(:has_required_attributes?).and_return(true)
|
36
|
+
t.stub(:validate_attributes).and_return(nil)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
let(:response) { double(risk_score: risk_score) }
|
40
|
+
let(:risk_score) { 3.4 }
|
41
|
+
|
42
|
+
before do
|
43
|
+
Minfraud::Request.stub(:get).and_return(response)
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'transaction has not already been sent to MaxMind' do
|
47
|
+
it 'sends transaction to MaxMind' do
|
48
|
+
expect(Minfraud::Request).to receive(:get).with(transaction)
|
49
|
+
transaction.risk_score
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'caches response' do
|
53
|
+
transaction.risk_score
|
54
|
+
expect(transaction.instance_variable_get(:@response)).to eql(response)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'returns float containing risk score' do
|
58
|
+
transaction.risk_score
|
59
|
+
expect(transaction.risk_score).to eq(risk_score)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'transaction has already been sent to MaxMind' do
|
64
|
+
before { transaction.instance_variable_set(:@response, response) }
|
65
|
+
|
66
|
+
it 'does not send transaction to MaxMind' do
|
67
|
+
expect(Minfraud::Request).not_to receive(:get)
|
68
|
+
transaction.risk_score
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'returns float containing risk score' do
|
72
|
+
expect(transaction.risk_score).to eq(risk_score)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '#attributes' do
|
78
|
+
subject(:transaction) do
|
79
|
+
Minfraud::Transaction.new do |t|
|
80
|
+
t.ip = 'ip'
|
81
|
+
t.city = 'city'
|
82
|
+
t.state = 'state'
|
83
|
+
t.postal = 'postal'
|
84
|
+
t.country = 'country'
|
85
|
+
t.email = 'hughjass@example.com'
|
86
|
+
t.requested_type = 'standard'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
before { Minfraud.requested_type = 'premium' }
|
91
|
+
after { Minfraud.remove_class_variable(:@@requested_type) }
|
92
|
+
|
93
|
+
it 'returns a hash of attributes' do
|
94
|
+
expect(transaction.attributes[:ip]).to eq('ip')
|
95
|
+
expect(transaction.attributes[:city]).to eq('city')
|
96
|
+
expect(transaction.attributes[:state]).to eq('state')
|
97
|
+
expect(transaction.attributes[:postal]).to eq('postal')
|
98
|
+
expect(transaction.attributes[:country]).to eq('country')
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'derives email domain and an md5 hash of whole email from email attribute' do
|
102
|
+
expect(transaction.attributes[:email_domain]).to eq('example.com')
|
103
|
+
expect(transaction.attributes[:email_md5]).to eq('01ddb59d9bc1d1bfb3eb99a22578ce33')
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
describe '#requested_type' do
|
109
|
+
subject(:transaction) do
|
110
|
+
Minfraud::Transaction.new do |t|
|
111
|
+
t.ip = 'ip'
|
112
|
+
t.city = 'city'
|
113
|
+
t.state = 'state'
|
114
|
+
t.postal = 'postal'
|
115
|
+
t.country = 'country'
|
116
|
+
t.email = 'hughjass@example.com'
|
117
|
+
t.requested_type = 'standard'
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
before { Minfraud.requested_type = 'premium' }
|
122
|
+
after { Minfraud.remove_class_variable(:@@requested_type) }
|
123
|
+
|
124
|
+
it 'uses requested type as set on transaction if present' do
|
125
|
+
expect(transaction.attributes[:requested_type]).to eq('standard')
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'uses requested type as set during configuration if not present in transaction' do
|
129
|
+
transaction = Minfraud::Transaction.new do |t|
|
130
|
+
t.ip = 'ip'
|
131
|
+
t.city = 'city'
|
132
|
+
t.state = 'state'
|
133
|
+
t.postal = 'postal'
|
134
|
+
t.country = 'country'
|
135
|
+
t.email = 'hughjass@example.com'
|
136
|
+
end
|
137
|
+
expect(transaction.attributes[:requested_type]).to eq('premium')
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minfraud-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robbie Pitts
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ffaker
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- robbie@sweatypitts.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/minfraud-ruby.rb
|
97
|
+
- lib/minfraud/minfraud.rb
|
98
|
+
- lib/minfraud/request.rb
|
99
|
+
- lib/minfraud/response.rb
|
100
|
+
- lib/minfraud/transaction.rb
|
101
|
+
- lib/minfraud/version.rb
|
102
|
+
- minfraud-ruby.gemspec
|
103
|
+
- spec/minfraud/minfraud_spec.rb
|
104
|
+
- spec/minfraud/request_spec.rb
|
105
|
+
- spec/minfraud/response_spec.rb
|
106
|
+
- spec/minfraud/transaction_spec.rb
|
107
|
+
- spec/spec_helper.rb
|
108
|
+
homepage: https://github.com/rdpitts/minfraud-ruby
|
109
|
+
licenses:
|
110
|
+
- GNU GPL
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: 1.9.3
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.2.2
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Ruby interface to the MaxMind minFraud API service.
|
132
|
+
test_files:
|
133
|
+
- spec/minfraud/minfraud_spec.rb
|
134
|
+
- spec/minfraud/request_spec.rb
|
135
|
+
- spec/minfraud/response_spec.rb
|
136
|
+
- spec/minfraud/transaction_spec.rb
|
137
|
+
- spec/spec_helper.rb
|
138
|
+
has_rdoc:
|