bank_card 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bank_card.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,19 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec',
5
+ :version => 2,
6
+ :cli => "--color --format nested",
7
+ :all_after_pass => false,
8
+ :all_on_start => false,
9
+ :keep_failed => false do
10
+ watch(%r{^spec/.+_spec\.rb$})
11
+ watch(%r{^lib/(.+)\.rb$}) { "spec/" }
12
+ watch('spec/spec_helper.rb') { "spec/" }
13
+ end
14
+
15
+
16
+ guard 'bundler' do
17
+ watch('Gemfile')
18
+ watch(/^.+\.gemspec/)
19
+ end
data/README.rdoc ADDED
@@ -0,0 +1,23 @@
1
+ = BankCard / CreditCard
2
+
3
+ * Source: http://github.com/alovak/bank_card
4
+ * Bugs: http://github.com/alovak/bank_card/issues
5
+
6
+ == Description
7
+
8
+ This gem contains validation of card number with Luhn method, card brand detection (Visa, MasterCard, etc.)
9
+
10
+ == Examples
11
+
12
+ class CreditCard < ActiveRecord::Base
13
+ include BankCard::Validations
14
+
15
+ validates :number, :luhn => true
16
+ validates_expiration_of :date
17
+ end
18
+
19
+ You can get more info from specs
20
+
21
+ = Licence
22
+
23
+ released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bank_card.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bank_card/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bank_card"
7
+ s.version = BankCard::VERSION
8
+ s.authors = ["Pavel Gabriel"]
9
+ s.email = ["alovak@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Bunch of methods to work with credit cards}
12
+ s.description = %q{Gem includes validations, card brand detection method, etc.}
13
+
14
+ s.rubyforge_project = "bank_card"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ s.add_development_dependency "guard"
23
+ s.add_development_dependency "guard-rspec"
24
+ s.add_development_dependency "guard-bundler"
25
+ s.add_development_dependency "ruby-debug19"
26
+
27
+ s.add_runtime_dependency "activemodel"
28
+ s.add_runtime_dependency "activesupport"
29
+ end
@@ -0,0 +1,6 @@
1
+ en:
2
+ errors:
3
+ # The values :model, :attribute and :value are always available for interpolation
4
+ # The value :count is available when applicable. Can be used for pluralization.
5
+ messages:
6
+ expired: "is expired"
@@ -0,0 +1,16 @@
1
+ module BankCard
2
+ module Validations
3
+ class ExpirationValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ record.errors.add(attribute, :expired, options) unless value && value.end_of_month.future?
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+ def validates_expiration_of(*attr_names)
11
+ validates_with ExpirationValidator, _merge_attributes(attr_names)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,19 @@
1
+ module BankCard
2
+ module Validations
3
+ class LuhnValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ relative_number = {'0' => 0, '1' => 2, '2' => 4, '3' => 6, '4' => 8, '5' => 1, '6' => 3, '7' => 5, '8' => 7, '9' => 9}
6
+
7
+ sum = 0
8
+
9
+ value.to_s.reverse.split("").each_with_index do |n, i|
10
+ sum += (i % 2 == 0) ? n.to_i : relative_number[n]
11
+ end
12
+
13
+ unless sum % 10 == 0
14
+ record.errors.add(attribute, :invalid, options)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module BankCard
2
+ VERSION = "0.0.1"
3
+ end
data/lib/bank_card.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'active_model'
2
+ require 'active_support'
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext/date/calculations'
5
+ require 'active_support/core_ext/time/calculations'
6
+ require 'active_support/i18n'
7
+
8
+ require "bank_card/version"
9
+
10
+ module BankCard
11
+ module Validations
12
+ extend ActiveSupport::Concern
13
+ end
14
+ end
15
+
16
+ Dir[File.dirname(__FILE__) + "/bank_card/validations/*.rb"].sort.each do |path|
17
+ filename = File.basename(path)
18
+ require "bank_card/validations/#{filename}"
19
+ end
20
+
21
+ I18n.load_path << File.dirname(__FILE__) + '/bank_card/locale/en.yml'
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ class CreditCard
4
+ include ActiveModel::Validations
5
+ include BankCard::Validations
6
+
7
+ attr_accessor :number, :date
8
+ end
9
+
10
+ describe CreditCard do
11
+ before do
12
+ CreditCard.reset_callbacks(:validate)
13
+ end
14
+
15
+ it "should validate number with Luhn algorithm" do
16
+ CreditCard.validates :number, :luhn => true
17
+ card = CreditCard.new
18
+
19
+ card.number = '123'
20
+ card.should be_invalid
21
+ card.errors[:number].should include("is invalid")
22
+
23
+ card.number = '4200000000000000'
24
+ card.should be_valid
25
+ end
26
+
27
+ it "should validate card expiration" do
28
+ CreditCard.validates_expiration_of :date
29
+ card = CreditCard.new
30
+
31
+ card.date = Date.today.prev_month
32
+ card.should be_invalid
33
+ card.errors[:date].should include("is expired")
34
+
35
+ card.date = Date.today
36
+ card.should be_valid
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
+
3
+ require 'bank_card'
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bank_card
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pavel Gabriel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-16 00:00:00.000000000 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &2153323800 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2153323800
26
+ - !ruby/object:Gem::Dependency
27
+ name: guard
28
+ requirement: &2153323380 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2153323380
37
+ - !ruby/object:Gem::Dependency
38
+ name: guard-rspec
39
+ requirement: &2153322960 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *2153322960
48
+ - !ruby/object:Gem::Dependency
49
+ name: guard-bundler
50
+ requirement: &2153322540 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *2153322540
59
+ - !ruby/object:Gem::Dependency
60
+ name: ruby-debug19
61
+ requirement: &2153322120 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *2153322120
70
+ - !ruby/object:Gem::Dependency
71
+ name: activemodel
72
+ requirement: &2153321700 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: *2153321700
81
+ - !ruby/object:Gem::Dependency
82
+ name: activesupport
83
+ requirement: &2153321280 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :runtime
90
+ prerelease: false
91
+ version_requirements: *2153321280
92
+ description: Gem includes validations, card brand detection method, etc.
93
+ email:
94
+ - alovak@gmail.com
95
+ executables: []
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - .gitignore
100
+ - Gemfile
101
+ - Guardfile
102
+ - README.rdoc
103
+ - Rakefile
104
+ - bank_card.gemspec
105
+ - lib/bank_card.rb
106
+ - lib/bank_card/locale/en.yml
107
+ - lib/bank_card/validations/expiration_validator.rb
108
+ - lib/bank_card/validations/luhn_validator.rb
109
+ - lib/bank_card/version.rb
110
+ - spec/lib/validations_spec.rb
111
+ - spec/spec_helper.rb
112
+ has_rdoc: true
113
+ homepage: ''
114
+ licenses: []
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project: bank_card
133
+ rubygems_version: 1.6.2
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Bunch of methods to work with credit cards
137
+ test_files:
138
+ - spec/lib/validations_spec.rb
139
+ - spec/spec_helper.rb