jan 0.0.1 → 0.0.2

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: ebad61806458a86c36f94a97d38b74ccb93f45c4
4
- data.tar.gz: 7724c79d26b5bdb8a4154d0a809f29f21a858cc7
3
+ metadata.gz: 783fb22083cdee79514404d9dfce2db04e3f98f4
4
+ data.tar.gz: 53bbdb8e75c9d5bfb0503908bda34c4260ba0af0
5
5
  SHA512:
6
- metadata.gz: 053b3a6c5784718be8f10a6d79f9e14af43fa6974ce34547f86defb8f4c735a185f40a5e20e89c22b8ff340970e277ee5b86752d7667299cf6d623c52c78ceda
7
- data.tar.gz: 28adbb792d78c7152dcf1daf6bb6f7bd8afc4fa45bd628808616881fab69cb298675d0f268a4f24d7286b3c46632f2a2d9ee23876e2cdf7b84981a54aca7d34a
6
+ metadata.gz: ec3814c7ebb6239c3fd3fe08988150719e71b55e5335627030d175cfb20b8f32d7a4ed59f42e2513a1271e991c33ce4fa2038663bff15c6f94bd883991b00a47
7
+ data.tar.gz: 2ce916f461b1c40cdfaf22461da46a57abd53a7ffa5bf2f18a9d6a417a79902ea1e3c402f77bad79c93d5a63d3148727021313a15e2f85908046573316b294cf
data/README.md CHANGED
@@ -37,6 +37,15 @@ jan.valid? # => true
37
37
  Jan::Validator.validate("4901277241126") # => true
38
38
  ```
39
39
 
40
+ ### Calculate check digit
41
+
42
+ ```ruby
43
+ # Correct code: "4901277241126"
44
+
45
+ Jan::CheckDigitCalculator.calculate("490127724112") # => 6
46
+ ```
47
+
48
+
40
49
  ### Get elements
41
50
 
42
51
  ```ruby
data/lib/jan/validator.rb CHANGED
@@ -6,8 +6,14 @@ class Jan
6
6
  module_function
7
7
 
8
8
  def validate(code)
9
- code = code.to_s
10
- return false unless [8,13].include?(code.size)
9
+ validate_size(code.to_s) && validate_check_digit(code.to_s)
10
+ end
11
+
12
+ def validate_size(code)
13
+ [8,13].include?(code.to_s.size)
14
+ end
15
+
16
+ def validate_check_digit(code)
11
17
  Parser.check_digit(code) == CheckDigitCalculator.calculate(Parser.body(code))
12
18
  end
13
19
  end
data/lib/jan/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Jan
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -20,4 +20,57 @@ describe Jan::Validator do
20
20
  end
21
21
  end
22
22
  end
23
+
24
+ describe ".validate_size" do
25
+ context "valid codes" do
26
+ valid_size_codes = [8, 13].map{|n| "x" * n }
27
+
28
+ valid_size_codes.each do |code|
29
+ it "#{code} should be valid" do
30
+ actual = Jan::Validator.validate_size(code)
31
+ expect(actual).to be true
32
+ end
33
+ end
34
+ end
35
+
36
+ context "invalid codes" do
37
+ invalid_size_codes = [1,2,3,4,5,6,7,9,10,11,12,14,15].map{|n| "x" * n }
38
+
39
+ invalid_size_codes.each do |code|
40
+ it "#{code} should be invalid" do
41
+ actual = Jan::Validator.validate_size(code)
42
+ expect(actual).to be false
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ describe ".validate_check_digit" do
49
+ context "valid codes" do
50
+ valid_check_digit_codes = (3..15).to_a.map{|n| Jan::Random.code(n) }
51
+
52
+ valid_check_digit_codes.each do |code|
53
+ it "#{code} should be valid" do
54
+ actual = Jan::Validator.validate_check_digit(code)
55
+ expect(actual).to be true
56
+ end
57
+ end
58
+ end
59
+
60
+ context "invalid codes" do
61
+ invalid_check_digit_codes = []
62
+ (3..15).each do |n|
63
+ code = Jan::Random.code(n)
64
+ code[-1] = ((Jan::Parser.check_digit(code) + 1 ) % 10).to_s
65
+ invalid_check_digit_codes << code
66
+ end
67
+
68
+ invalid_check_digit_codes.each do |code|
69
+ it "#{code} should be invalid" do
70
+ actual = Jan::Validator.validate_check_digit(code)
71
+ expect(actual).to be false
72
+ end
73
+ end
74
+ end
75
+ end
23
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - OSA Shunsuke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-12 00:00:00.000000000 Z
11
+ date: 2014-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler