gtin_extras 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +6 -1
- data/lib/gtin_extras.rb +1 -0
- data/lib/gtin_extras/isbn.rb +49 -0
- data/lib/gtin_extras/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d873b82fd80b9df00c2af68cf35ac00d8a96dbb53371115f6e31b30e2fc47681
|
4
|
+
data.tar.gz: 387b7d1adf99ab45e9b1c7e934d6b74d2febcb2939e198745e1f1dacfc034a8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cef10c20b04b2f646e026c39967840756413deff78a36905e946afe82101d012dbb97303c2e3a300fc9e79c9c3fdf8834c937cf6387e8cd5d40e220ace8945bb
|
7
|
+
data.tar.gz: 6385ceab43dfdc5b8104be33957f123dfbdcb96185d0b3331e40278152ab3e8bc2915b63d11a7d0be84b5f09d61e64594f52d4f8c3a74ed349039a6490216b78
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# gtin_extras
|
2
|
-
Ruby String extensions for validating GTIN/UPC/EAN/
|
2
|
+
Ruby String extensions for validating and formatting GTIN/UPC/EAN/GS1 _and_ PLU _and_ ASIN _and_ ISBN numbers. Use these to clean up and validate your ecommerce data!
|
3
3
|
|
4
4
|
## Usage
|
5
5
|
|
@@ -16,6 +16,9 @@ require 'gtin_extras'
|
|
16
16
|
|
17
17
|
'3400'.plu? # true
|
18
18
|
'THIS00AN53'.asin? # true
|
19
|
+
'960-425-059-0'.isbn? # true
|
20
|
+
'960 425 059 0'.isbn? # true
|
21
|
+
'9781234567897'.isbn? # true
|
19
22
|
```
|
20
23
|
|
21
24
|
## Installation
|
@@ -56,6 +59,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
56
59
|
- https://www.gtin.info/check-digit-calculator/
|
57
60
|
- https://www.amazon.com/gp/seller/asin-upc-isbn-info.html
|
58
61
|
- https://www.ifpsglobal.com/Identification/PLU-Codes
|
62
|
+
- https://www.instructables.com/id/How-to-verify-a-ISBN/
|
63
|
+
- https://en.wikipedia.org/wiki/International_Standard_Book_Number
|
59
64
|
|
60
65
|
## Contributing
|
61
66
|
|
data/lib/gtin_extras.rb
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
# String validation for ISBN codes
|
2
|
+
# Ref: https://www.instructables.com/id/How-to-verify-a-ISBN/
|
3
|
+
# some more refs @: https://en.wikipedia.org/wiki/International_Standard_Book_Number
|
4
|
+
module ISBN
|
5
|
+
def isbn?
|
6
|
+
#removes any non-digits
|
7
|
+
str = to_s.gsub(/[\D]+/, '')
|
8
|
+
self.replace(str)
|
9
|
+
return false unless [10, 13].include?(length)
|
10
|
+
case length
|
11
|
+
when 10
|
12
|
+
return false unless (calculate_10_dig % 11 == 0)
|
13
|
+
when 13
|
14
|
+
return false unless (calculate_13_dig % 10 == 0)
|
15
|
+
end
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
# ISBN's assigned between 1970 - 2007 have 10 digits
|
20
|
+
def calculate_10_dig
|
21
|
+
sum = 0
|
22
|
+
reversed_string = reverse!
|
23
|
+
reversed_string.each_char.with_index do |char, index|
|
24
|
+
index_plus_one = index + 1
|
25
|
+
sum += char.to_i * index_plus_one
|
26
|
+
end
|
27
|
+
sum
|
28
|
+
end
|
29
|
+
|
30
|
+
# ISBN's became 13 digits after Jan 1, 2007
|
31
|
+
def calculate_13_dig
|
32
|
+
sum = 0
|
33
|
+
reversed_string = reverse!
|
34
|
+
reversed_string.each_char.with_index do |char, index|
|
35
|
+
times_one_or_three = (index + 1) % 2
|
36
|
+
if (times_one_or_three == 1)
|
37
|
+
sum += (char.to_i * 1)
|
38
|
+
else
|
39
|
+
sum += (char.to_i * 3)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
sum
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Extend String with these methods
|
47
|
+
class String
|
48
|
+
include ISBN
|
49
|
+
end
|
data/lib/gtin_extras/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gtin_extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Beckman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- lib/gtin_extras.rb
|
73
73
|
- lib/gtin_extras/asin.rb
|
74
74
|
- lib/gtin_extras/gtin.rb
|
75
|
+
- lib/gtin_extras/isbn.rb
|
75
76
|
- lib/gtin_extras/plu.rb
|
76
77
|
- lib/gtin_extras/version.rb
|
77
78
|
homepage: https://github.com/officeluv/gtin_extras
|