credy 0.1.1 → 0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25e3a3d043de72f875cd7a6405aa0f721cb00f61
4
+ data.tar.gz: 00f78e5960314b372e9d953b4ec546f991559d0a
5
+ SHA512:
6
+ metadata.gz: e2a2b74deef424b8025408786abae8b644df82c458cdd4a6ce5493ec2a4553f5c2135e1b29050dd82f2b9ee09f1d18845b8dd8c4388a3d68ac07a350cb87d97d
7
+ data.tar.gz: f4006c3b5a2fbaa74c51ea8b4bfee9d64208cd0b8eb0de86ec658d47be112e55065e4765b90de9520468f24ba0ea20281f578fe24833c477b8c80ce3c0b9945b
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
- *.gem
1
+ *.gem
2
+ .bundle
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.1
6
+ - ruby-head
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in credy.gemspec
4
- gemspec
4
+ gemspec
@@ -2,37 +2,27 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  credy (0.1.1)
5
- thor
5
+ thor (~> 0.18.1)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- columnize (0.3.6)
11
- debugger (1.3.0)
12
- columnize (>= 0.3.1)
13
- debugger-linecache (~> 1.1.1)
14
- debugger-ruby_core_source (~> 1.1.7)
15
- debugger-linecache (1.1.2)
16
- debugger-ruby_core_source (>= 1.1.1)
17
- debugger-ruby_core_source (1.1.7)
18
- diff-lcs (1.1.3)
10
+ diff-lcs (1.2.5)
19
11
  rake (10.0.3)
20
- rspec (2.12.0)
21
- rspec-core (~> 2.12.0)
22
- rspec-expectations (~> 2.12.0)
23
- rspec-mocks (~> 2.12.0)
24
- rspec-core (2.12.2)
25
- rspec-expectations (2.12.1)
26
- diff-lcs (~> 1.1.3)
27
- rspec-mocks (2.12.2)
28
- thor (0.17.0)
12
+ rspec (2.14.1)
13
+ rspec-core (~> 2.14.0)
14
+ rspec-expectations (~> 2.14.0)
15
+ rspec-mocks (~> 2.14.0)
16
+ rspec-core (2.14.7)
17
+ rspec-expectations (2.14.5)
18
+ diff-lcs (>= 1.1.3, < 2.0)
19
+ rspec-mocks (2.14.5)
20
+ thor (0.18.1)
29
21
 
30
22
  PLATFORMS
31
23
  ruby
32
24
 
33
25
  DEPENDENCIES
34
26
  credy!
35
- debugger
36
- rake
37
- rspec
38
- rspec-mocks
27
+ rake (~> 10.0.3)
28
+ rspec (~> 2.14.1)
data/Rakefile CHANGED
@@ -2,4 +2,4 @@ require 'rspec/core/rake_task'
2
2
 
3
3
  RSpec::Core::RakeTask.new(:spec)
4
4
 
5
- task :default => :spec
5
+ task default: :spec
data/bin/credy CHANGED
@@ -19,14 +19,14 @@ module Credy
19
19
  args[:country] = options[:country] unless options[:country] == 'all'
20
20
  args[:type] = options[:type] unless options[:type] == 'all'
21
21
 
22
- options[:number].times do
22
+ options[:number].times do
23
23
  number = CreditCard.generate args
24
24
 
25
25
  unless number
26
26
  puts 'No rule found for those criteria.'
27
27
  break
28
28
  end
29
-
29
+
30
30
  if details
31
31
  d = [number[:type], number[:country]].compact
32
32
  puts "#{number[:number]} (#{d.join(', ')})"
@@ -71,9 +71,9 @@ module Credy
71
71
  end
72
72
  puts "(#{details.join(', ')})"
73
73
  end
74
-
74
+
75
75
  end
76
76
 
77
77
  end
78
78
 
79
- Credy::CLI.start
79
+ Credy::CLI.start
@@ -6,7 +6,8 @@ Gem::Specification.new do |s|
6
6
  s.authors = ['Tim Petricola']
7
7
  s.email = ['hi@timpetricola.com']
8
8
  s.summary = %q{A simple (but powerful) credit card number generator}
9
- s.homepage = ''
9
+ s.license = 'MIT'
10
+ s.homepage = 'https://github.com/TimPetricola/Credy'
10
11
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
12
  s.files = `git ls-files`.split "\n"
12
13
  s.test_files = `git ls-files -- {test,spec,features}/*`.split "\n"
@@ -14,9 +15,7 @@ Gem::Specification.new do |s|
14
15
  s.require_paths = ['lib']
15
16
  s.version = Credy::VERSION
16
17
 
17
- s.add_development_dependency 'rspec'
18
- s.add_development_dependency 'rspec-mocks'
19
- s.add_development_dependency 'rake'
20
- s.add_development_dependency 'debugger'
21
- s.add_runtime_dependency 'thor'
22
- end
18
+ s.add_development_dependency 'rspec', '~> 2.14.1'
19
+ s.add_development_dependency 'rake', '~> 10.0.3'
20
+ s.add_runtime_dependency 'thor', '~> 0.18.1'
21
+ end
@@ -5,4 +5,4 @@ americanexpress:
5
5
  ca: '372301'
6
6
  fr: '374622'
7
7
  pl: '337941'
8
- uk: ['374283', '374288']
8
+ uk: ['374283', '374288']
@@ -1,3 +1,3 @@
1
1
  bankcard:
2
2
  length: 16
3
- prefix: ['5610', '560221', '560222', '560223', '560224', '560225']
3
+ prefix: ['5610', '560221', '560222', '560223', '560224', '560225']
@@ -1,3 +1,3 @@
1
1
  china-unionpay:
2
2
  length: [16, 17, 18, 19]
3
- prefix: ['5610', '560221', '560222', '560223', '560224', '560225']
3
+ prefix: ['5610', '560221', '560222', '560223', '560224', '560225']
@@ -1,3 +1,3 @@
1
1
  diners-club-carte-blanche:
2
2
  length: 14
3
- prefix: ['300', '301', '302', '303', '304', '305']
3
+ prefix: ['300', '301', '302', '303', '304', '305']
@@ -1,3 +1,3 @@
1
1
  diners-club-enroute:
2
2
  length: 15
3
- prefix: ['2014', '2015', '2016', '2017', '2018', '2019']
3
+ prefix: ['2014', '2015', '2016', '2017', '2018', '2019']
@@ -1,3 +1,3 @@
1
1
  diners-club-international:
2
2
  length: 14
3
- prefix: '36'
3
+ prefix: '36'
@@ -1,3 +1,3 @@
1
1
  diners-club-us-ca:
2
2
  length: 16
3
- prefix: ['54', '55']
3
+ prefix: ['54', '55']
@@ -1,3 +1,3 @@
1
1
  instapayment:
2
2
  length: 16
3
- prefix: ['637', '638', '639']
3
+ prefix: ['637', '638', '639']
@@ -1,3 +1,3 @@
1
1
  jcb:
2
2
  length: 16
3
- prefix: ['3528', '3529', '3530', '3531', '3532', '3533', '3534', '3535', '3536', '3537', '3538', '3539', '3540', '3541', '3542', '3543', '3544', '3545', '3546', '3547', '3548', '3549', '3550', '3551', '3552', '3553', '3554', '3555', '3556', '3557', '3558', '3559', '3560', '3561', '3562', '3563', '3564', '3565', '3566', '3567', '3568', '3569', '3570', '3571', '3572', '3573', '3574', '3575', '3576', '3577', '3578', '3579', '3580', '3581', '3582', '3583', '3584', '3585', '3586', '3587', '3588', '3589']
3
+ prefix: ['3528', '3529', '3530', '3531', '3532', '3533', '3534', '3535', '3536', '3537', '3538', '3539', '3540', '3541', '3542', '3543', '3544', '3545', '3546', '3547', '3548', '3549', '3550', '3551', '3552', '3553', '3554', '3555', '3556', '3557', '3558', '3559', '3560', '3561', '3562', '3563', '3564', '3565', '3566', '3567', '3568', '3569', '3570', '3571', '3572', '3573', '3574', '3575', '3576', '3577', '3578', '3579', '3580', '3581', '3582', '3583', '3584', '3585', '3586', '3587', '3588', '3589']
@@ -1,3 +1,3 @@
1
1
  laser:
2
2
  length: [16, 17, 18, 19]
3
- prefix: ['6304', '6706', '6771', '6709']
3
+ prefix: ['6304', '6706', '6771', '6709']
@@ -1,3 +1,3 @@
1
1
  maestro:
2
2
  length: [16, 17, 18, 19]
3
- prefix: ['5018', '5020', '5038', '5893', '6304', '6759', '6761', '6762', '6763', '0604']
3
+ prefix: ['5018', '5020', '5038', '5893', '6304', '6759', '6761', '6762', '6763', '0604']
@@ -4,4 +4,4 @@ mastercard:
4
4
  countries:
5
5
  ca: ['500235', '500766', '501012']
6
6
  es: ['510013', '510014', '510017']
7
- us: ['504837', '510875']
7
+ us: ['504837', '510875']
@@ -1,3 +1,3 @@
1
1
  solo:
2
2
  length: [16, 18, 19]
3
- prefix: ['6334', '6767']
3
+ prefix: ['6334', '6767']
@@ -1,3 +1,3 @@
1
1
  switch:
2
2
  length: [16, 18, 19]
3
- prefix: ['4903', '4905', '4911', '4936', '564182', '633110', '6333', '6759']
3
+ prefix: ['4903', '4905', '4911', '4936', '564182', '633110', '6333', '6759']
@@ -1,3 +1,3 @@
1
1
  visa-electron:
2
2
  length: 16
3
- prefix: ['4026', '417500', '4405', '4508', '4844', '4913', '4917']
3
+ prefix: ['4026', '417500', '4405', '4508', '4844', '4913', '4917']
@@ -3,4 +3,4 @@ visa:
3
3
  prefix: '4'
4
4
  countries:
5
5
  ch: '404159'
6
- au: ['401795', '404137']
6
+ au: ['401795', '404137']
@@ -13,40 +13,19 @@ module Credy
13
13
 
14
14
  # Generate a credit card number
15
15
  def self.generate(options = {})
16
-
17
- # Include global rules (not based on country)
18
- include_global_rules = options[:country].nil?
19
- rule = Rules.filter(options, include_global_rules).sample
20
-
21
- return nil unless rule
22
-
23
- length = rule[:length].is_a?(Array) ? rule[:length].sample : rule[:length]
24
- number = rule[:prefix]
25
-
26
- # Generates n-1 digits
27
- (length - number.length - 1).times do
28
- number = number + rand(10).to_s
29
- end
30
-
31
- # Generates the last digit according to luhn algorithm
32
- l = nil
33
- digits = (0..9).to_a.map(&:to_s)
34
- begin
35
- l = digits.delete digits.sample
36
- end while !Check.luhn number+l
37
-
38
- number = number+l
16
+ rule = find_rule(options) || return
17
+ number = generate_from_rule(rule)
39
18
 
40
19
  {
41
- number: number,
42
- type: rule[:type],
20
+ number: number,
21
+ type: rule[:type],
43
22
  country: rule[:country]
44
23
  }
45
24
  end
46
25
 
47
26
  # Returns information about a number
48
27
  def self.infos(number)
49
- rules = Rules.flatten(true).select do |rule|
28
+ rules = Rules.all.select do |rule|
50
29
  valid = true
51
30
 
52
31
  # Check number of digits
@@ -78,7 +57,37 @@ module Credy
78
57
  details: criterii
79
58
  }
80
59
  end
81
-
60
+
61
+ def self.find_rule(options = {})
62
+ Rules.filter(options).sample
63
+ end
64
+ private_class_method :find_rule
65
+
66
+ def self.complete_number(number)
67
+ # Generates the last digit according to luhn algorithm
68
+ digits = (0..9).map(&:to_s)
69
+ begin
70
+ full = number + digits.delete(digits.sample)
71
+ end while !Check.luhn(full)
72
+
73
+ full
74
+ end
75
+ private_class_method :complete_number
76
+
77
+ def self.generate_from_rule(rule)
78
+ length = Array(rule[:length]).sample
79
+ number = rule[:prefix]
80
+
81
+ # Generates n-1 digits
82
+ (length - number.length - 1).times do
83
+ number += rand(10).to_s
84
+ end
85
+
86
+ # Append last digit
87
+ complete_number(number)
88
+ end
89
+ private_class_method :generate_from_rule
90
+
82
91
  end
83
92
 
84
- end
93
+ end
@@ -4,9 +4,9 @@ module Credy
4
4
 
5
5
  def self.luhn(code)
6
6
  s1 = s2 = 0
7
- code.to_s.reverse.chars.each_slice(2) do |odd, even|
7
+ code.to_s.reverse.chars.each_slice(2) do |odd, even|
8
8
  s1 += odd.to_i
9
-
9
+
10
10
  double = even.to_i * 2
11
11
  double -= 9 if double >= 10
12
12
  s2 += double
@@ -16,4 +16,4 @@ module Credy
16
16
 
17
17
  end
18
18
 
19
- end
19
+ end
@@ -5,67 +5,46 @@ module Credy
5
5
  class Rules
6
6
 
7
7
  # Return all the rules from yml files
8
- def self.all
9
- @rules ||= begin
10
- rules = {}
11
- Dir.glob "#{Credy.root}/data/*.yml" do |filename|
12
- r = YAML::load IO.read(filename)
13
- rules.merge! r
14
- end
15
- rules
16
- end
8
+ def self.raw
9
+ @rules ||= load_rules("#{Credy.root}/data/*.yml")
17
10
  end
18
11
 
19
12
  # Change hash format to process rules
20
- def self.flatten(global_rules = false)
13
+ def self.all
21
14
  rules = []
22
15
 
23
- all.each do |type, details|
24
-
25
- if global_rules
26
- # Add general rules
27
- global_prefixes = details['prefix']
28
- if global_prefixes
29
- global_prefixes = [global_prefixes] unless global_prefixes.is_a? Array
30
-
31
- global_prefixes.each do |prefix|
32
- rules.push({
33
- prefix: prefix.to_s,
34
- length: details['length'],
35
- type: type
36
- })
37
- end
38
- end
16
+ raw.each do |type, details|
17
+ # Add general rules
18
+ Array(details['prefix']).each do |prefix|
19
+ rules.push({
20
+ prefix: prefix.to_s,
21
+ length: details['length'],
22
+ type: type
23
+ })
39
24
  end
40
25
 
41
26
  # Process each country
42
- if details['countries']
43
- details['countries'].each do |country, prefixes|
44
- prefixes = [prefixes] unless prefixes.is_a? Array
45
-
46
- # Add a rule for each prefix
47
- prefixes.each do |prefix|
48
- rules.push({
49
- prefix: prefix.to_s,
50
- length: details['length'],
51
- type: type,
52
- country: country,
53
- })
54
- end
27
+ Array(details['countries']).each do |country, prefixes|
28
+ # Add a rule for each prefix
29
+ Array(prefixes).each do |prefix|
30
+ rules.push({
31
+ prefix: prefix.to_s,
32
+ length: details['length'],
33
+ type: type,
34
+ country: country,
35
+ })
55
36
  end
56
37
  end
57
38
 
58
39
  end
59
40
 
60
- # Sort rules by prefix length
61
- rules.sort! { |x, y| y[:prefix].length <=> x[:prefix].length }
62
-
63
- rules
41
+ # Sort by prefix length
42
+ rules.sort { |x, y| y[:prefix].length <=> x[:prefix].length }
64
43
  end
65
44
 
66
45
  # Returns rules according to given filters
67
- def self.filter(filters = {}, global_rules = false)
68
- flatten(global_rules).select do |rule|
46
+ def self.filter(filters = {})
47
+ all.select do |rule|
69
48
  [:country, :type].each do |condition|
70
49
  break false if filters[condition] && filters[condition] != rule[condition]
71
50
  true
@@ -73,6 +52,15 @@ module Credy
73
52
  end
74
53
  end
75
54
 
55
+ def self.load_rules(files)
56
+ {}.tap do |rules|
57
+ Dir.glob(files) do |filename|
58
+ rules.merge! YAML::load IO.read(filename)
59
+ end
60
+ end
61
+ end
62
+ private_class_method :load_rules
63
+
76
64
  end
77
65
 
78
- end
66
+ end
@@ -10,4 +10,4 @@ class String
10
10
  def green
11
11
  colorize 32
12
12
  end
13
- end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Credy
2
- VERSION = '0.1.1'
3
- end
2
+ VERSION = '0.2.0'
3
+ end
data/readme.md CHANGED
@@ -1,8 +1,10 @@
1
- # Credy
1
+ # Credy [![Build Status](https://travis-ci.org/TimPetricola/Credy.png?branch=master)](https://travis-ci.org/TimPetricola/Credy) [![Gem Version](https://badge.fury.io/rb/credy.png)](http://badge.fury.io/rb/credy) [![Code Climate](https://codeclimate.com/github/TimPetricola/Credy.png)](https://codeclimate.com/github/TimPetricola/Credy)
2
2
 
3
- A simple credit card generator.
3
+ A simple credit card generator/validator.
4
+ Need a valid credit card number to sign up on a site but you don't want to use your real card?
5
+ Need to validate your customers credit card?
4
6
 
5
- ## Functionnalities
7
+ ## Functionalities
6
8
  * Generates a valid number (per country/type)
7
9
  * Get information for a number
8
10
  * Check validity of a number
@@ -33,8 +35,8 @@ credy validate 5108756163954799
33
35
  ### Generate
34
36
  ``` ruby
35
37
  options {
36
- :country => 'au',
37
- :type => 'mastercard'
38
+ country: 'au',
39
+ type: 'mastercard'
38
40
  }
39
41
  Credy::CreditCard.generate options
40
42
  ```
@@ -89,4 +91,4 @@ At the moment, only a few types and countries are (partially) supported.
89
91
  All data is coming from the [Bank card number](http://en.wikipedia.org/wiki/Bank_card_number) page and the [List of Issuer Identification Numbers](http://en.wikipedia.org/wiki/List_of_Issuer_Identification_Numbers) on [Wikipedia](http://wikipedia.org). I do not assume the responsibility for wrong data.
90
92
 
91
93
  ## License
92
- Credy is released under the [MIT License](http://opensource.org/licenses/MIT).
94
+ Credy is released under the [MIT License](http://opensource.org/licenses/MIT).
@@ -1,66 +1,61 @@
1
1
  require_relative '../spec_helper'
2
-
3
2
  silent { load 'bin/credy' }
4
3
 
5
4
  describe Credy::CLI do
6
5
 
7
- before :all do
8
- @stdout = $stdout
9
- $stdout = StringIO.new
6
+ before(:all) do
7
+ @stdout, $stdout = $stdout, StringIO.new
10
8
  end
11
9
 
12
10
  describe 'generate' do
13
11
 
14
12
  it 'works without options' do
15
- Credy::CreditCard.should_receive(:generate).with({})
13
+ expect(Credy::CreditCard).to receive(:generate).with({})
16
14
  r = Credy::CLI.start ['generate']
17
15
  end
18
16
 
19
- it '--country' do
20
- Credy::CreditCard.should_receive(:generate).with(country: 'au').twice
17
+ it '--country' do
18
+ expect(Credy::CreditCard).to receive(:generate).with(country: 'au').twice
21
19
  Credy::CLI.start ['generate', '--country', 'au']
22
20
  Credy::CLI.start ['generate', '-c', 'au']
23
21
  end
24
22
 
25
- it '--type' do
26
- Credy::CreditCard.should_receive(:generate).with(type: 'visa').twice
23
+ it '--type' do
24
+ expect(Credy::CreditCard).to receive(:generate).with(type: 'visa').twice
27
25
  Credy::CLI.start ['generate', '--type', 'visa']
28
26
  Credy::CLI.start ['generate', '-t', 'visa']
29
27
  end
30
28
 
31
29
  it '--number' do
32
- Credy::CreditCard.should_receive(:generate).exactly(20).and_return({number: '50076645747856835'})
30
+ expect(Credy::CreditCard).to receive(:generate).exactly(20).and_return({number: '50076645747856835'})
33
31
  Credy::CLI.start ['generate', '--number', 10]
34
32
  Credy::CLI.start ['generate', '-n', 10]
35
33
  end
36
34
 
37
35
  describe 'result' do
38
-
39
- before :all do
40
- $stdout = @stdout
41
- end
42
-
43
36
  it 'stops if no number is found' do
44
- Credy::CreditCard.should_receive(:generate).once.and_return(nil)
45
- STDOUT.should_receive(:puts).with('No rule found for those criteria.').once
46
- Credy::CLI.start ['generate', '-n', 10]
37
+ expect(Credy::CreditCard).to receive(:generate).once.and_return(nil)
38
+ output = silent { Credy::CLI.start ['generate', '-n', 10] }
39
+ expect(output).to eq "No rule found for those criteria.\n"
47
40
  end
48
41
 
49
42
  describe '--details' do
50
43
  before do
51
44
  number = {number: '50076645747856835', type: 'visa', country: 'au'}
52
- Credy::CreditCard.should_receive(:generate).any_number_of_times.and_return number
45
+ allow(Credy::CreditCard).to receive(:generate).and_return number
53
46
  end
54
47
 
55
48
  it 'only returns the number' do
56
- STDOUT.should_receive(:puts).with('50076645747856835').once
57
- Credy::CLI.start ['generate']
49
+ output = silent { Credy::CLI.start ['generate'] }
50
+ expect(output).to eq "50076645747856835\n"
58
51
  end
59
52
 
60
53
  it 'accepts to return more details' do
61
- STDOUT.should_receive(:puts).with('50076645747856835 (visa, au)').twice
62
- Credy::CLI.start ['generate', '--details']
63
- Credy::CLI.start ['generate', '-d']
54
+ output = silent do
55
+ Credy::CLI.start ['generate', '--details']
56
+ Credy::CLI.start ['generate', '-d']
57
+ end
58
+ expect(output).to eq "50076645747856835 (visa, au)\n"*2
64
59
  end
65
60
  end
66
61
 
@@ -71,28 +66,24 @@ describe Credy::CLI do
71
66
  describe 'infos' do
72
67
 
73
68
  it 'calls the infos function' do
74
- Credy::CreditCard.should_receive(:infos).with '5108756163954799'
75
- Credy::CLI.start ['infos', '5108756163954799']
69
+ expect(Credy::CreditCard).to receive(:infos).with '5108756163954799'
70
+ silent {
71
+ Credy::CLI.start ['infos', '5108756163954799']
72
+ }
76
73
  end
77
74
 
78
75
  describe 'result' do
79
- before :all do
80
- $stdout = @stdout
81
- end
82
-
83
76
  it 'shows error if nothing found' do
84
- Credy::CreditCard.should_receive(:infos).and_return nil
85
- STDOUT.should_receive(:puts).with 'No information available for this number.'
86
- Credy::CLI.start ['infos', '5108756163954799']
77
+ expect(Credy::CreditCard).to receive(:infos).and_return nil
78
+ output = silent { Credy::CLI.start ['infos', '5108756163954799'] }
79
+ expect(output).to eq "No information available for this number.\n"
87
80
  end
88
81
 
89
82
  it 'shows the card informations' do
90
83
  number = {number: '50076645747856835', type: 'visa', country: 'au'}
91
- Credy::CreditCard.should_receive(:infos).at_least(1).times.and_return number
92
- STDOUT.should_receive(:puts).with 'Type: visa'
93
- STDOUT.should_receive(:puts).with 'Country: au'
94
- STDOUT.should_receive(:puts).with 'Valid'
95
- Credy::CLI.start ['infos', '50076645747856835']
84
+ expect(Credy::CreditCard).to receive(:infos).at_least(1).times.and_return number
85
+ output = silent { Credy::CLI.start ['infos', '50076645747856835'] }
86
+ expect(output).to eq "Type: visa\nCountry: au\nValid\n"
96
87
  end
97
88
  end
98
89
 
@@ -100,32 +91,26 @@ describe Credy::CLI do
100
91
 
101
92
  describe 'validate' do
102
93
  it 'calls the validate function' do
103
- Credy::CreditCard.should_receive(:validate).with('5108756163954799').and_return({valid: false, details: {}})
94
+ expect(Credy::CreditCard).to receive(:validate).with('5108756163954799').and_return({valid: false, details: {}})
104
95
  Credy::CLI.start ['validate', '5108756163954799']
105
96
  end
106
97
 
107
98
  describe 'result' do
108
- before :all do
109
- $stdout = @stdout
110
- end
111
-
112
99
  it 'shows card validity and details' do
113
100
  validity = { valid: true, details: { luhn: true, type: true} }
114
- Credy::CreditCard.should_receive(:validate).and_return validity
115
- STDOUT.should_receive(:puts).with 'This number is valid.'
116
- STDOUT.should_receive(:puts).with '(luhn: v, type: v)'
117
- Credy::CLI.start ['validate', '50076645747856835']
101
+ expect(Credy::CreditCard).to receive(:validate).and_return validity
102
+ output = silent { Credy::CLI.start ['validate', '50076645747856835'] }
103
+ expect(output).to eq "This number is valid.\n(luhn: v, type: v)\n"
118
104
  end
119
105
 
120
106
  it 'shows card validity and details for invalid number' do
121
107
  validity = { valid: false, details: { luhn: false, type: false} }
122
- Credy::CreditCard.should_receive(:validate).and_return validity
123
- STDOUT.should_receive(:puts).with 'This number is not valid.'
124
- STDOUT.should_receive(:puts).with '(luhn: x, type: x)'
125
- Credy::CLI.start ['validate', '5108756163954799']
108
+ expect(Credy::CreditCard).to receive(:validate).and_return validity
109
+ output = silent { Credy::CLI.start ['validate', '5108756163954799'] }
110
+ expect(output).to eq "This number is not valid.\n(luhn: x, type: x)\n"
126
111
  end
127
112
 
128
113
  end
129
114
  end
130
115
 
131
- end
116
+ end
@@ -5,15 +5,15 @@ describe Credy::Check do
5
5
  describe '.luhn' do
6
6
 
7
7
  it 'returns true for correct data' do
8
- Credy::Check.luhn(49927398716).should be_true
9
- Credy::Check.luhn(1234567812345670).should be_true
8
+ expect(Credy::Check.luhn(49927398716)).to be_true
9
+ expect(Credy::Check.luhn(1234567812345670)).to be_true
10
10
  end
11
11
 
12
12
  it 'returns false for non correct data' do
13
- Credy::Check.luhn(49927398717).should be_false
14
- Credy::Check.luhn(1234567812345678).should be_false
13
+ expect(Credy::Check.luhn(49927398717)).to be_false
14
+ expect(Credy::Check.luhn(1234567812345678)).to be_false
15
15
  end
16
16
 
17
17
  end
18
18
 
19
- end
19
+ end
@@ -4,22 +4,17 @@ describe Credy::Rules do
4
4
 
5
5
  subject { Credy::Rules }
6
6
 
7
- describe '.all' do
8
-
9
- it { should respond_to :all }
10
- its(:all) { should be_a Hash }
11
-
12
- it 'should be tested more'
13
-
7
+ describe '.raw' do
8
+ it { should respond_to :raw }
9
+ its(:raw) { should be_a Hash }
14
10
  end
15
11
 
16
- describe '.flatten' do
12
+ describe '.all' do
17
13
 
18
- it { should respond_to :flatten }
19
- its(:flatten) { should be_a Array }
14
+ its(:all) { should be_a Array }
20
15
 
21
- it 'flattens a rules hash' do
22
- subject.stub(:all).and_return({
16
+ it 'contains a set of rules' do
17
+ subject.stub(:raw).and_return({
23
18
  'visa' => {
24
19
  'length' => [13, 16],
25
20
  'countries' => {
@@ -29,21 +24,19 @@ describe Credy::Rules do
29
24
  },
30
25
  'mastercard' => {
31
26
  'length' => 16,
32
- 'countries' => {
33
- 'us' => '504837'
34
- }
35
- },
27
+ 'prefix' => '51'
28
+ }
36
29
  })
37
30
 
38
- subject.flatten.should == [
39
- {:prefix=>"404159", :length=>[13, 16], :type=>"visa", :country=>"ch"},
40
- {:prefix=>"401795", :length=>[13, 16], :type=>"visa", :country=>"au"},
41
- {:prefix=>"504837", :length=>16, :type=>"mastercard", :country=>"us"}
31
+ expect(subject.all).to eq [
32
+ {prefix:"404159", length:[13, 16], type:"visa", country:"ch"},
33
+ {prefix:"401795", length:[13, 16], type:"visa", country:"au"},
34
+ {prefix:"51", length:16, type:"mastercard"}
42
35
  ]
43
36
  end
44
37
 
45
38
  it 'works with string prefixes' do
46
- subject.stub(:all).and_return({
39
+ subject.stub(:raw).and_return({
47
40
  'visa' => {
48
41
  'length' => [13, 16],
49
42
  'countries' => {
@@ -52,13 +45,13 @@ describe Credy::Rules do
52
45
  }
53
46
  })
54
47
 
55
- subject.flatten.should == [
56
- {:prefix=>"401795", :length=>[13, 16], :type=>"visa", :country=>"au"}
48
+ expect(subject.all).to eq [
49
+ {prefix:"401795", length:[13, 16], type:"visa", country:"au"}
57
50
  ]
58
51
  end
59
52
 
60
53
  it 'works with integer prefixes' do
61
- subject.stub(:all).and_return({
54
+ subject.stub(:raw).and_return({
62
55
  'visa' => {
63
56
  'length' => [13, 16],
64
57
  'countries' => {
@@ -67,13 +60,13 @@ describe Credy::Rules do
67
60
  }
68
61
  })
69
62
 
70
- subject.flatten.should == [
71
- {:prefix=>"401795", :length=>[13, 16], :type=>"visa", :country=>"au"}
63
+ expect(subject.all).to eq [
64
+ {prefix:"401795", length:[13, 16], type:"visa", country:"au"}
72
65
  ]
73
66
  end
74
67
 
75
68
  it 'works with an array of prefixes' do
76
- subject.stub(:all).and_return({
69
+ subject.stub(:raw).and_return({
77
70
  'visa' => {
78
71
  'length' => [13, 16],
79
72
  'countries' => {
@@ -82,42 +75,18 @@ describe Credy::Rules do
82
75
  }
83
76
  })
84
77
 
85
- subject.flatten.should == [
86
- {:prefix=>"401795", :length=>[13, 16], :type=>"visa", :country=>"au"},
87
- {:prefix=>"404137", :length=>[13, 16], :type=>"visa", :country=>"au"}
88
- ]
89
- end
90
-
91
- it 'includes global rules' do
92
- subject.stub(:all).and_return({
93
- 'visa' => {
94
- 'length' => [13, 16],
95
- 'prefix' => '4',
96
- 'countries' => {
97
- 'ch' => '404159',
98
- 'au' => '401'
99
- }
100
- },
101
- 'mastercard' => {
102
- 'length' => 16,
103
- 'prefix' => '51'
104
- },
105
- })
106
-
107
- subject.flatten(true).should == [
108
- {:prefix=>"404159", :length=>[13, 16], :type=>"visa", :country=>"ch"},
109
- {:prefix=>"401", :length=>[13, 16], :type=>"visa", :country=>"au"},
110
- {:prefix=>"51", :length=>16, :type=>"mastercard"},
111
- {:prefix=>"4", :length=>[13, 16], :type=>"visa"}
78
+ expect(subject.all).to eq [
79
+ {prefix:"401795", length:[13, 16], type:"visa", country:"au"},
80
+ {prefix:"404137", length:[13, 16], type:"visa", country:"au"}
112
81
  ]
113
82
  end
114
83
 
115
84
  end
116
85
 
117
- describe '.filter' do
86
+ describe '.filter' do
118
87
 
119
- before do
120
- subject.stub(:all).and_return({
88
+ before do
89
+ subject.stub(:raw).and_return({
121
90
  'visa' => {
122
91
  'length' => [13, 16],
123
92
  'countries' => {
@@ -137,31 +106,31 @@ describe Credy::Rules do
137
106
  it { should respond_to :filter }
138
107
 
139
108
  it 'returns everything if no filter is provided' do
140
- subject.filter.should be_a Array
141
- subject.filter.should == subject.flatten
109
+ expect(subject.filter).to be_a Array
110
+ expect(subject.filter).to eq subject.all
142
111
  end
143
112
 
144
113
  it 'filters by type' do
145
- subject.filter(type: 'visa').length.should == 2
146
- subject.filter(type: 'mastercard').length.should == 1
114
+ expect(subject.filter(type: 'visa')).to have(2).items
115
+ expect(subject.filter(type: 'mastercard')).to have(1).item
147
116
  end
148
117
 
149
118
  it 'accepts the :country option' do
150
- subject.filter(country: 'ch').length.should == 1
151
- subject.filter(country: 'au').length.should == 2
119
+ expect(subject.filter(country: 'ch')).to have(1).item
120
+ expect(subject.filter(country: 'au')).to have(2).items
152
121
  end
153
122
 
154
123
  it 'accepts several options at the same time' do
155
124
  rules = subject.filter type: 'visa', country: 'au'
156
- rules.length.should == 1
125
+ expect(rules).to have(1).item
157
126
  end
158
127
 
159
128
  it 'returns an empty array if nothing is found' do
160
129
  rules = subject.filter type: 'foo', country: 'bar'
161
- rules.should be_a Array
162
- rules.should be_empty
130
+ expect(rules).to be_a Array
131
+ expect(rules).to be_empty
163
132
  end
164
133
 
165
134
  end
166
135
 
167
- end
136
+ end
@@ -1,7 +1,7 @@
1
1
  describe Credy::VERSION do
2
2
 
3
3
  it 'is defined' do
4
- Credy::VERSION.should_not be_nil
4
+ expect(Credy::VERSION).not_to be_nil
5
5
  end
6
6
 
7
- end
7
+ end
@@ -24,46 +24,46 @@ describe Credy::CreditCard do
24
24
  end
25
25
 
26
26
  before(:each) do
27
- Credy::Rules.stub(:all).and_return rules
27
+ Credy::Rules.stub(:raw).and_return rules
28
28
  end
29
29
 
30
- describe '.generate' do
30
+ describe '.generate' do
31
31
 
32
32
  it 'returns a number, type and country' do
33
- subject.generate[:number].should_not be_nil
34
- subject.generate[:type].should_not be_nil
35
- subject.generate[:country].should_not be_nil
33
+ expect(subject.generate[:number]).not_to be_nil
34
+ expect(subject.generate[:type]).not_to be_nil
35
+ expect(subject.generate[:country]).not_to be_nil
36
36
  end
37
37
 
38
38
  it 'accepts the :type option' do
39
39
  number = subject.generate type: 'visa'
40
- number[:type].should == 'visa'
40
+ expect(number[:type]).to eq 'visa'
41
41
  end
42
42
 
43
43
  it 'accepts the :country option' do
44
44
  number = subject.generate country: 'ch'
45
- number[:country].should == 'ch'
45
+ expect(number[:country]).to eq 'ch'
46
46
  end
47
47
 
48
48
  it 'accepts several options at the same time' do
49
49
  number = subject.generate type: 'mastercard', country: 'au'
50
- number[:type].should == 'mastercard'
51
- number[:country].should == 'au'
50
+ expect(number[:type]).to eq 'mastercard'
51
+ expect(number[:country]).to eq 'au'
52
52
  end
53
53
 
54
54
  it 'generates the right number of digits' do
55
55
  number = subject.generate type: 'mastercard'
56
- number[:number].length.should == 16
56
+ expect(number[:number].length).to be 16
57
57
  end
58
58
 
59
59
  it 'generate a number with the right prefix' do
60
60
  number = subject.generate type: 'mastercard', country: 'au'
61
- number[:number].should =~ /^401795/
61
+ expect(number[:number]).to match /^401795/
62
62
  end
63
63
 
64
64
  it 'returns nil if nothing is found' do
65
65
  number = subject.generate type: 'foo', country: 'bar'
66
- number.should be_nil
66
+ expect(number).to be_nil
67
67
  end
68
68
 
69
69
  end
@@ -72,8 +72,8 @@ describe Credy::CreditCard do
72
72
 
73
73
  it 'returns the correct information according to the card number' do
74
74
  infos = subject.infos '5108756163954792'
75
- infos[:type].should == 'mastercard'
76
- infos[:country].should == 'us'
75
+ expect(infos[:type]).to eq 'mastercard'
76
+ expect(infos[:country]).to eq 'us'
77
77
  end
78
78
 
79
79
  end
@@ -82,23 +82,23 @@ describe Credy::CreditCard do
82
82
 
83
83
  it 'returns a hash' do
84
84
  r = subject.validate '5108756163954792'
85
- r[:valid].should be_true
86
- r[:details][:luhn].should be_true
87
- r[:details][:type].should be_true
85
+ expect(r[:valid]).to be_true
86
+ expect(r[:details][:luhn]).to be_true
87
+ expect(r[:details][:type]).to be_true
88
88
  end
89
89
 
90
90
  it 'checks against luhn algorithm' do
91
91
  r = subject.validate '5108756163954791'
92
- r[:valid].should be_false
93
- r[:details][:luhn].should be_false
92
+ expect(r[:valid]).to be_false
93
+ expect(r[:details][:luhn]).to be_false
94
94
  end
95
95
 
96
96
  it 'checks against card type' do
97
97
  r = subject.validate '99999999999999999999992'
98
- r[:valid].should be_false
99
- r[:details][:type].should be_false
98
+ expect(r[:valid]).to be_false
99
+ expect(r[:details][:type]).to be_false
100
100
  end
101
101
 
102
102
  end
103
103
 
104
- end
104
+ end
@@ -5,12 +5,13 @@ require 'credy'
5
5
 
6
6
  def silent
7
7
  _stdout = $stdout
8
- $stdout = StringIO.new
8
+ $stdout = fake = StringIO.new
9
9
  begin
10
10
  yield
11
11
  ensure
12
12
  $stdout = _stdout
13
13
  end
14
+ fake.string
14
15
  end
15
16
 
16
17
  # Don't colorize strings in test environment
@@ -21,4 +22,7 @@ class String
21
22
  end
22
23
 
23
24
  RSpec.configure do |config|
25
+ config.expect_with :rspec do |c|
26
+ c.syntax = :expect
27
+ end
24
28
  end
metadata CHANGED
@@ -1,96 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tim Petricola
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-04 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: '0'
19
+ version: 2.14.1
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: rspec-mocks
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
26
+ version: 2.14.1
46
27
  - !ruby/object:Gem::Dependency
47
28
  name: rake
48
29
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: debugger
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
30
  requirements:
67
- - - ! '>='
31
+ - - "~>"
68
32
  - !ruby/object:Gem::Version
69
- version: '0'
33
+ version: 10.0.3
70
34
  type: :development
71
35
  prerelease: false
72
36
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
37
  requirements:
75
- - - ! '>='
38
+ - - "~>"
76
39
  - !ruby/object:Gem::Version
77
- version: '0'
40
+ version: 10.0.3
78
41
  - !ruby/object:Gem::Dependency
79
42
  name: thor
80
43
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
44
  requirements:
83
- - - ! '>='
45
+ - - "~>"
84
46
  - !ruby/object:Gem::Version
85
- version: '0'
47
+ version: 0.18.1
86
48
  type: :runtime
87
49
  prerelease: false
88
50
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
51
  requirements:
91
- - - ! '>='
52
+ - - "~>"
92
53
  - !ruby/object:Gem::Version
93
- version: '0'
54
+ version: 0.18.1
94
55
  description:
95
56
  email:
96
57
  - hi@timpetricola.com
@@ -99,7 +60,8 @@ executables:
99
60
  extensions: []
100
61
  extra_rdoc_files: []
101
62
  files:
102
- - .gitignore
63
+ - ".gitignore"
64
+ - ".travis.yml"
103
65
  - Gemfile
104
66
  - Gemfile.lock
105
67
  - Rakefile
@@ -133,30 +95,29 @@ files:
133
95
  - spec/lib/credy/version_spec.rb
134
96
  - spec/lib/credy_spec.rb
135
97
  - spec/spec_helper.rb
136
- - travis.yml
137
- homepage: ''
138
- licenses: []
98
+ homepage: https://github.com/TimPetricola/Credy
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
139
102
  post_install_message:
140
103
  rdoc_options: []
141
104
  require_paths:
142
105
  - lib
143
106
  required_ruby_version: !ruby/object:Gem::Requirement
144
- none: false
145
107
  requirements:
146
- - - ! '>='
108
+ - - ">="
147
109
  - !ruby/object:Gem::Version
148
110
  version: '0'
149
111
  required_rubygems_version: !ruby/object:Gem::Requirement
150
- none: false
151
112
  requirements:
152
- - - ! '>='
113
+ - - ">="
153
114
  - !ruby/object:Gem::Version
154
115
  version: '0'
155
116
  requirements: []
156
117
  rubyforge_project:
157
- rubygems_version: 1.8.24
118
+ rubygems_version: 2.2.0
158
119
  signing_key:
159
- specification_version: 3
120
+ specification_version: 4
160
121
  summary: A simple (but powerful) credit card number generator
161
122
  test_files:
162
123
  - spec/bin/credy_spec.rb
@@ -165,4 +126,3 @@ test_files:
165
126
  - spec/lib/credy/version_spec.rb
166
127
  - spec/lib/credy_spec.rb
167
128
  - spec/spec_helper.rb
168
- has_rdoc:
data/travis.yml DELETED
@@ -1,10 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 1.9.3
4
- - 1.9.2
5
- - jruby-18mode
6
- - jruby-19mode
7
- - ruby-head
8
- - jruby-head
9
- - 1.8.7
10
- - ree