barcodevalidation 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/lib/barcodevalidation.rb +5 -0
- data/lib/barcodevalidation/version.rb +1 -1
- data/test/test_barcodevalidation.rb +13 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dac677100702afe680ec1161c2ecbfa9426df4ba
|
4
|
+
data.tar.gz: c2d7360b4dacc32286a59ffa6f67dccc1f04ebb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c388ac3ad8bad464cc111c1a5f2e8fe6a4de37d978fb4fdc87f7dbf5439849acc0b218f213a222d820cce8f43bff37bdb7ef8ea42b94e072c3eaf19b4afc7f5
|
7
|
+
data.tar.gz: 98a5889fea125fff15c609b272a810d1b8b553ce0fc6b2a24e121478ffda6d5f86d2f624fb5e8a5328cc660304d543611b4cb833f4ae9a44bb7ea6ec876d776d
|
data/lib/barcodevalidation.rb
CHANGED
@@ -2,6 +2,8 @@ require "barcodevalidation/version"
|
|
2
2
|
|
3
3
|
module Barcodevalidation
|
4
4
|
def self.valid?(barcode)
|
5
|
+
Integer(barcode)
|
6
|
+
|
5
7
|
parts = ('%018d' % barcode).to_s.chars.map(&:to_i)
|
6
8
|
checksum = parts.pop
|
7
9
|
parts
|
@@ -17,5 +19,8 @@ module Barcodevalidation
|
|
17
19
|
|
18
20
|
calculated_checksum = ((calculated_checksum.to_f / 10).ceil * 10) - calculated_checksum
|
19
21
|
checksum == calculated_checksum
|
22
|
+
|
23
|
+
rescue ArgumentError
|
24
|
+
false #we only accept numeric barcodes
|
20
25
|
end
|
21
26
|
end
|
@@ -8,6 +8,7 @@ class TestBarcodevalidation < MiniTest::Test
|
|
8
8
|
def test_valid_barcodes
|
9
9
|
[
|
10
10
|
937179004167,
|
11
|
+
'937179004167',
|
11
12
|
9312631133233
|
12
13
|
].each { |barcode| assert Barcodevalidation.valid?(barcode), barcode }
|
13
14
|
end
|
@@ -18,7 +19,18 @@ class TestBarcodevalidation < MiniTest::Test
|
|
18
19
|
1234567890123, #invalid check digit
|
19
20
|
50140424, #invalid check digit
|
20
21
|
5420056646861,
|
21
|
-
10004336
|
22
|
+
10004336,
|
23
|
+
'48cm',
|
24
|
+
1, #make sure only valid length barcodes are accepted
|
25
|
+
22,
|
26
|
+
333,
|
27
|
+
4444,
|
28
|
+
55555,
|
29
|
+
666666,
|
30
|
+
777777,
|
31
|
+
99999999,
|
32
|
+
12345678901,
|
33
|
+
123456789012345
|
22
34
|
].each { |barcode| refute Barcodevalidation.valid?(barcode), barcode }
|
23
35
|
end
|
24
36
|
end
|