faker_credit_card 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/faker_credit_card.gemspec +23 -0
- data/lib/faker_credit_card/american_express.rb +14 -0
- data/lib/faker_credit_card/dinners_club.rb +14 -0
- data/lib/faker_credit_card/discover.rb +14 -0
- data/lib/faker_credit_card/generator.rb +33 -0
- data/lib/faker_credit_card/master_card.rb +14 -0
- data/lib/faker_credit_card/version.rb +5 -0
- data/lib/faker_credit_card/visa.rb +16 -0
- data/lib/faker_credit_card.rb +22 -0
- data/test/credit_card_number_validator.rb +26 -0
- data/test/test_faker_credit_card.rb +53 -0
- data/test/test_helper.rb +4 -0
- metadata +63 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "faker_credit_card/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "faker_credit_card"
|
7
|
+
s.version = Faker::CreditCard::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Vinicius Akio Higa"]
|
10
|
+
s.email = ["viniciushiga@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{}
|
13
|
+
s.description = %q{}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
# specify any dependencies here; for example:
|
21
|
+
# s.add_development_dependency "rspec"
|
22
|
+
# s.add_runtime_dependency "rest-client"
|
23
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Faker
|
2
|
+
module CreditCard
|
3
|
+
class DinnersClub
|
4
|
+
extend Generator
|
5
|
+
|
6
|
+
PREFIXES = ["300", "301", "302", "303", "304", "305", "36", "38"]
|
7
|
+
LENGTH = 14
|
8
|
+
|
9
|
+
def self.number(options = {})
|
10
|
+
generate_number(PREFIXES.sample, LENGTH)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Faker
|
2
|
+
module CreditCard
|
3
|
+
module Generator
|
4
|
+
def generate_number(prefix, max_length)
|
5
|
+
ccnum = prefix.to_s
|
6
|
+
|
7
|
+
while ccnum.size < (max_length - 1)
|
8
|
+
ccnum += Random.rand(10).to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
sum = 0
|
12
|
+
reversed_ccnum = ccnum.reverse
|
13
|
+
|
14
|
+
reversed_ccnum.size.times.each do |index|
|
15
|
+
if index.odd?
|
16
|
+
sum += reversed_ccnum[index].to_i
|
17
|
+
else
|
18
|
+
double = reversed_ccnum[index].to_i * 2
|
19
|
+
|
20
|
+
double.fdiv(10).to_s.split(".").each do |n|
|
21
|
+
sum += n.to_i
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
mod = sum % 10
|
27
|
+
check_digit = mod.zero? ? mod : (10 - mod)
|
28
|
+
|
29
|
+
ccnum += check_digit.to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Faker
|
2
|
+
module CreditCard
|
3
|
+
class Visa
|
4
|
+
extend Generator
|
5
|
+
|
6
|
+
PREFIX = "4"
|
7
|
+
LENGTHS = [13, 16]
|
8
|
+
|
9
|
+
def self.number(options = {})
|
10
|
+
length = options.fetch(:length, LENGTHS.sample)
|
11
|
+
fail "visa credit card can't have #{length} digits" unless LENGTHS.member? length
|
12
|
+
generate_number(PREFIX, length)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "faker_credit_card/generator"
|
2
|
+
require "faker_credit_card/american_express"
|
3
|
+
require "faker_credit_card/dinners_club"
|
4
|
+
require "faker_credit_card/discover"
|
5
|
+
require "faker_credit_card/master_card"
|
6
|
+
require "faker_credit_card/visa"
|
7
|
+
|
8
|
+
require "faker_credit_card/version"
|
9
|
+
|
10
|
+
module Faker
|
11
|
+
module CreditCard
|
12
|
+
def self.number(type, options = {})
|
13
|
+
case type
|
14
|
+
when :american_express then AmericanExpress.number(options)
|
15
|
+
when :dinners_club then DinnersClub.number(options)
|
16
|
+
when :discover then Discover.number(options)
|
17
|
+
when :master_card then MasterCard.number(options)
|
18
|
+
when :visa then Visa.number(options)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module CreditCardNumberValidator
|
2
|
+
def valid_credit_card?(number)
|
3
|
+
return false unless valid_association?(number)
|
4
|
+
|
5
|
+
reversed_ccnum = number.reverse
|
6
|
+
relative_number = {'0' => 0, '1' => 2, '2' => 4, '3' => 6, '4' => 8, '5' => 1, '6' => 3, '7' => 5, '8' => 7, '9' => 9}
|
7
|
+
sum = 0
|
8
|
+
|
9
|
+
reversed_ccnum.split("").each_with_index do |n, i|
|
10
|
+
sum += (i % 2 == 0) ? n.to_i : relative_number[n]
|
11
|
+
end
|
12
|
+
|
13
|
+
sum % 10 == 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid_association?(number)
|
17
|
+
ccnum = number.to_s.gsub(/\D/, "")
|
18
|
+
|
19
|
+
return :dinners if ccnum.length == 14 && ccnum =~ /^3(0[0-5]|[68])/ # 300xxx-305xxx, 36xxxx, 38xxxx
|
20
|
+
return :amex if ccnum.length == 15 && ccnum =~ /^3[47]/ # 34xxxx, 37xxxx
|
21
|
+
return :visa if [13,16].include?(ccnum.length) && ccnum =~ /^4/ # 4xxxxx
|
22
|
+
return :master if ccnum.length == 16 && ccnum =~ /^5[1-5]/ # 51xxxx-55xxxx
|
23
|
+
return :discover if ccnum.length == 16 && ccnum =~ /^6011/ # 6011xx
|
24
|
+
return nil
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class TestFakerCreditCard < Test::Unit::TestCase
|
4
|
+
include CreditCardNumberValidator
|
5
|
+
|
6
|
+
def test_american_express
|
7
|
+
number = Faker::CreditCard.number(:american_express)
|
8
|
+
assert_not_nil number
|
9
|
+
assert_equal number.size, 15
|
10
|
+
assert valid_credit_card?(number)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_dinners_club
|
14
|
+
number = Faker::CreditCard.number(:dinners_club)
|
15
|
+
assert_not_nil number
|
16
|
+
assert_equal number.size, 14
|
17
|
+
assert valid_credit_card?(number)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_discover
|
21
|
+
number = Faker::CreditCard.number(:discover)
|
22
|
+
assert_not_nil number
|
23
|
+
assert_equal number.size, 16
|
24
|
+
assert valid_credit_card?(number)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_master_card
|
28
|
+
number = Faker::CreditCard.number(:master_card)
|
29
|
+
assert_not_nil number
|
30
|
+
assert_equal number.size, 16
|
31
|
+
assert valid_credit_card?(number)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_visa
|
35
|
+
number = Faker::CreditCard.number(:visa)
|
36
|
+
assert_not_nil number
|
37
|
+
assert valid_credit_card?(number)
|
38
|
+
|
39
|
+
number = Faker::CreditCard.number(:visa, length: 13)
|
40
|
+
assert_not_nil number
|
41
|
+
assert_equal number.size, 13
|
42
|
+
assert valid_credit_card?(number)
|
43
|
+
|
44
|
+
number = Faker::CreditCard.number(:visa, length: 16)
|
45
|
+
assert_not_nil number
|
46
|
+
assert_equal number.size, 16
|
47
|
+
assert valid_credit_card?(number)
|
48
|
+
|
49
|
+
assert_raise RuntimeError do
|
50
|
+
Faker::CreditCard.number(:visa, length: 20)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faker_credit_card
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Vinicius Akio Higa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ''
|
15
|
+
email:
|
16
|
+
- viniciushiga@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- Rakefile
|
24
|
+
- faker_credit_card.gemspec
|
25
|
+
- lib/faker_credit_card.rb
|
26
|
+
- lib/faker_credit_card/american_express.rb
|
27
|
+
- lib/faker_credit_card/dinners_club.rb
|
28
|
+
- lib/faker_credit_card/discover.rb
|
29
|
+
- lib/faker_credit_card/generator.rb
|
30
|
+
- lib/faker_credit_card/master_card.rb
|
31
|
+
- lib/faker_credit_card/version.rb
|
32
|
+
- lib/faker_credit_card/visa.rb
|
33
|
+
- test/credit_card_number_validator.rb
|
34
|
+
- test/test_faker_credit_card.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
homepage: ''
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.10
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: ''
|
60
|
+
test_files:
|
61
|
+
- test/credit_card_number_validator.rb
|
62
|
+
- test/test_faker_credit_card.rb
|
63
|
+
- test/test_helper.rb
|