bank-validator 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f2c187846e8a80fc532597a56c5e710cd9d4ec9d
4
- data.tar.gz: 5bc3fecb853c128ba0894b1a12453eab9427d926
3
+ metadata.gz: 3ce014a1251785320e4a3942f9509d9c5df8a167
4
+ data.tar.gz: 9434a9d5af76a0b4c6ca946b1a8b53b0a2eac0bc
5
5
  SHA512:
6
- metadata.gz: 66d2b86b4f11e58494f18f71357ec69332ad6b908420dfba40566fd951cb73e4d45bea5a4ed94b80ee46ad3784999e800b9692272ef41793e69acb87dbc17381
7
- data.tar.gz: 5edbbd0bcf9b03f54544c14fcb7f9a1650663e631fcdae2f50af93b2758c032c949357011ff41a6a220d8c5fab83585e6cc4054eb81571b2914050a7e6582d2b
6
+ metadata.gz: d683e2d10cdd64df4d0a0b37de9154abf5a5960d86ea50343bb8d8ab73a7786ca554a4dc0d970df0bdb66906b08d65115f78b4d611846ddee7db35cea6317e2e
7
+ data.tar.gz: e404e316bf5c14f03f3d6bafe9cd7387ec5de1d278894b1302b08824e1b70df9b1f65eb5dc968c96f0d1c5774271b3a4a4d0dae6bfacaeed62eb0851c29ba843
data/Gemfile CHANGED
@@ -2,13 +2,13 @@ source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
- gem "activesupport"
6
- gem "activerecord"
7
-
5
+ gem "activesupport", ">= 3.0"
6
+ gem "activerecord", ">= 3.0"
8
7
 
9
8
  # Add dependencies to develop your gem here.
10
9
  # Include everything needed to run rake, tests, features, etc.
11
10
  group :development do
11
+ gem "rspec"
12
12
  gem "shoulda", "~> 3.5"
13
13
  gem "rdoc", "~> 3.12"
14
14
  gem "bundler", "~> 1.0"
data/Gemfile.lock CHANGED
@@ -19,6 +19,7 @@ GEM
19
19
  builder (3.2.2)
20
20
  descendants_tracker (0.0.4)
21
21
  thread_safe (~> 0.3, >= 0.3.1)
22
+ diff-lcs (1.2.5)
22
23
  docile (1.1.5)
23
24
  faraday (0.9.1)
24
25
  multipart-post (>= 1.2, < 3)
@@ -62,6 +63,19 @@ GEM
62
63
  rake (10.4.2)
63
64
  rdoc (3.12.2)
64
65
  json (~> 1.4)
66
+ rspec (3.2.0)
67
+ rspec-core (~> 3.2.0)
68
+ rspec-expectations (~> 3.2.0)
69
+ rspec-mocks (~> 3.2.0)
70
+ rspec-core (3.2.2)
71
+ rspec-support (~> 3.2.0)
72
+ rspec-expectations (3.2.0)
73
+ diff-lcs (>= 1.2.0, < 2.0)
74
+ rspec-support (~> 3.2.0)
75
+ rspec-mocks (3.2.1)
76
+ diff-lcs (>= 1.2.0, < 2.0)
77
+ rspec-support (~> 3.2.0)
78
+ rspec-support (3.2.2)
65
79
  shoulda (3.5.0)
66
80
  shoulda-context (~> 1.0, >= 1.0.1)
67
81
  shoulda-matchers (>= 1.4.1, < 3.0)
@@ -81,10 +95,11 @@ PLATFORMS
81
95
  ruby
82
96
 
83
97
  DEPENDENCIES
84
- activerecord
85
- activesupport
98
+ activerecord (>= 3.0)
99
+ activesupport (>= 3.0)
86
100
  bundler (~> 1.0)
87
101
  jeweler (~> 2.0)
88
102
  rdoc (~> 3.12)
103
+ rspec
89
104
  shoulda (~> 3.5)
90
105
  simplecov (~> 0.9)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -2,11 +2,11 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: bank-validator 0.0.2 ruby lib
5
+ # stub: bank-validator 0.0.3 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "bank-validator"
9
- s.version = "0.0.2"
9
+ s.version = "0.0.3"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  "VERSION",
29
29
  "bank-validator.gemspec",
30
30
  "lib/active_model.rb",
31
- "lib/active_model/validations/iban_validator.rb",
31
+ "lib/active_model/iban_validator.rb",
32
32
  "lib/bank-validator.rb",
33
33
  "test/helper.rb",
34
34
  "test/test_bank-validator.rb"
@@ -42,16 +42,18 @@ Gem::Specification.new do |s|
42
42
  s.specification_version = 4
43
43
 
44
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
- s.add_runtime_dependency(%q<activesupport>, [">= 0"])
46
- s.add_runtime_dependency(%q<activerecord>, [">= 0"])
45
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0"])
46
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0"])
47
+ s.add_development_dependency(%q<rspec>, [">= 0"])
47
48
  s.add_development_dependency(%q<shoulda>, ["~> 3.5"])
48
49
  s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
49
50
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
50
51
  s.add_development_dependency(%q<jeweler>, ["~> 2.0"])
51
52
  s.add_development_dependency(%q<simplecov>, ["~> 0.9"])
52
53
  else
53
- s.add_dependency(%q<activesupport>, [">= 0"])
54
- s.add_dependency(%q<activerecord>, [">= 0"])
54
+ s.add_dependency(%q<activesupport>, [">= 3.0"])
55
+ s.add_dependency(%q<activerecord>, [">= 3.0"])
56
+ s.add_dependency(%q<rspec>, [">= 0"])
55
57
  s.add_dependency(%q<shoulda>, ["~> 3.5"])
56
58
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
57
59
  s.add_dependency(%q<bundler>, ["~> 1.0"])
@@ -59,8 +61,9 @@ Gem::Specification.new do |s|
59
61
  s.add_dependency(%q<simplecov>, ["~> 0.9"])
60
62
  end
61
63
  else
62
- s.add_dependency(%q<activesupport>, [">= 0"])
63
- s.add_dependency(%q<activerecord>, [">= 0"])
64
+ s.add_dependency(%q<activesupport>, [">= 3.0"])
65
+ s.add_dependency(%q<activerecord>, [">= 3.0"])
66
+ s.add_dependency(%q<rspec>, [">= 0"])
64
67
  s.add_dependency(%q<shoulda>, ["~> 3.5"])
65
68
  s.add_dependency(%q<rdoc>, ["~> 3.12"])
66
69
  s.add_dependency(%q<bundler>, ["~> 1.0"])
data/lib/active_model.rb CHANGED
@@ -1,5 +1,5 @@
1
- require_relative './active_model/validations/iban_validator'
1
+ require_relative './active_model/iban_validator'
2
2
 
3
3
  module ActiveModel
4
- autoload :IbanValidator, './active_model/validations/iban_validator'
4
+ autoload :IbanValidator, './active_model/iban_validator'
5
5
  end
@@ -0,0 +1,30 @@
1
+ require 'active_model'
2
+
3
+ class IbanValidator < ActiveModel::EachValidator
4
+
5
+ def validate_each(record, attribute, value)
6
+ record_error(record, attribute, value) unless value =~ regexp && valid_iban?(value)
7
+ end
8
+
9
+ private
10
+
11
+ def record_error(record, attribute, value)
12
+ record.errors.add(attribute, :invalid_iban)#, options.merge!(value: value))
13
+ end
14
+
15
+ def regexp
16
+ /[A-Z]{2}[a-zA-Z0-9]{14,}/
17
+ end
18
+
19
+ def valid_iban?(iban)
20
+ # Move first four characters to end of string
21
+ first_four_chars = iban.slice!(0..3)
22
+ iban += first_four_chars
23
+
24
+ # Substitute all letters with integers
25
+ iban.split(//).each { |char| iban.gsub!(char, (char.downcase.ord - 87).to_s) if (char =~ /[a-zA-Z]/).present? }
26
+
27
+ # Check if division by 97 yields a remainder of 1, in which case it could be a valid IBAN
28
+ (iban.to_i % 97) == 1
29
+ end
30
+ end
data/test/helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'active_model'
1
2
  require 'simplecov'
2
3
 
3
4
  module SimpleCov::Configuration
@@ -8,7 +9,7 @@ end
8
9
 
9
10
  SimpleCov.configure do
10
11
  clean_filters
11
- load_adapter 'test_frameworks'
12
+ load_profile 'test_frameworks'
12
13
  end
13
14
 
14
15
  ENV["COVERAGE"] && SimpleCov.start do
@@ -23,12 +24,16 @@ rescue Bundler::BundlerError => e
23
24
  $stderr.puts "Run `bundle install` to install missing gems"
24
25
  exit e.status_code
25
26
  end
26
- require 'test/unit'
27
+
28
+ require 'minitest/autorun'
27
29
  require 'shoulda'
28
30
 
29
31
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
30
32
  $LOAD_PATH.unshift(File.dirname(__FILE__))
31
33
  require 'bank-validator'
32
34
 
33
- class Test::Unit::TestCase
35
+ class MiniTest::Test
36
+ include ActiveModel
34
37
  end
38
+
39
+ MiniTest.autorun
@@ -1,7 +1,51 @@
1
+ require 'active_model'
1
2
  require 'helper'
2
3
 
3
- class TestBankValidator < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
4
+ class TestBankValidator < MiniTest::Test
5
+
6
+ class TestUser
7
+ include ActiveModel::Validations
8
+ validates :iban, iban: true
9
+
10
+ attr_accessor :iban
11
+
12
+ def initialize(attributes = {})
13
+ @iban = attributes[:iban]
14
+ end
15
+
16
+ def save
17
+ return false unless valid?
18
+ end
19
+
20
+ def self.create(attributes = {})
21
+ new(attributes).save
22
+ end
23
+ end
24
+
25
+ should "not save if the iban is too short" do
26
+ assert_equal TestUser.create(iban: "GB82WEST"), false
27
+ end
28
+
29
+ should "save if the iban is at least 16 characters" do
30
+ assert_equal TestUser.create(iban: "GB82WEST12345698765432"), nilk
31
+ end
32
+
33
+ should "returns false if the iban does not leave a remainder of 1 when divided by 97" do
34
+ assert_equal TestUser.create(iban: "GB82WEST123456987654Df"), false
35
+ end
36
+
37
+ should "returns true if the iban returns a remainder of 1 when divided by 97" do
38
+ assert_equal TestUser.create(iban: "GB82WEST12345698765432"), nil
39
+ end
40
+
41
+ should "work for different IBAN formats" do
42
+ #Belgium
43
+ assert_equal TestUser.create(iban: "BE62510007547061"), nil
44
+
45
+ #Bulgaria
46
+ assert_equal TestUser.create(iban: "BG80BNBG96611020345678"), nil
47
+
48
+ #Germany
49
+ assert_equal TestUser.create(iban: "DE89370400440532013000"), nil
6
50
  end
7
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bank-validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Bahlke
@@ -16,23 +16,37 @@ dependencies:
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '3.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '3.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activerecord
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '3.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
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - '>='
@@ -125,7 +139,7 @@ files:
125
139
  - VERSION
126
140
  - bank-validator.gemspec
127
141
  - lib/active_model.rb
128
- - lib/active_model/validations/iban_validator.rb
142
+ - lib/active_model/iban_validator.rb
129
143
  - lib/bank-validator.rb
130
144
  - test/helper.rb
131
145
  - test/test_bank-validator.rb
@@ -1,34 +0,0 @@
1
- require 'active_model'
2
- module ActiveModel
3
- module Validations
4
- class IbanValidator < EachValidator
5
-
6
- def validate_each(record, attribute, value)
7
- record_error(record, attribute, value) unless value =~ regexp && valid_iban?(value)
8
- end
9
-
10
- private
11
-
12
- def record_error(record, attribute, value)
13
- record.errors.add(attribute, :invalid_iban, options.merge!(value: value))
14
- end
15
-
16
- def regexp
17
- /[A-Z]{2}[a-zA-Z0-9]{14,}/
18
- end
19
-
20
- def valid_iban?(iban)
21
- # Move first four characters to end of string
22
- first_four_chars = iban.slice!(0..3)
23
- iban += first_four_chars
24
-
25
- # Substitute all letters with integers
26
- iban.split(//).each { |char| iban.gsub!(char, char.downcase.ord - 87) if (char =~ /[a-zA-Z]/).present? }
27
-
28
- # Check if division by 97 yields a remainder of 1, in which case it could be a valid IBAN
29
- (iban.to_i % 97) == 1
30
- end
31
-
32
- end
33
- end
34
- end