gtin_extras 0.2.0 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3301ae1f366df2aa97d893800a6be6b01920e557281510a6db88931b07ad7742
4
- data.tar.gz: 536b57205aa386fba0c7187187ed317fdfa99245a38fc2cebe6a3c2c0b180e7a
3
+ metadata.gz: d873b82fd80b9df00c2af68cf35ac00d8a96dbb53371115f6e31b30e2fc47681
4
+ data.tar.gz: 387b7d1adf99ab45e9b1c7e934d6b74d2febcb2939e198745e1f1dacfc034a8e
5
5
  SHA512:
6
- metadata.gz: 4f2f06a740d8d9f401c2974b07ad34debd4f7bc9c52812d5b20d1e236b83782b9e88ef29ee5be8f8ae93ae6ad9696f8f5cd9812fe42b123982afd2a6ba41e049
7
- data.tar.gz: 4510d84c24dd0ccef13214fc60dc508a89bc4971747f71c51df2f47be9aeab0b22a63f00aa9d624e68e0b37b49f2397c92d8785ebc01cc23756fc898570b09fd
6
+ metadata.gz: cef10c20b04b2f646e026c39967840756413deff78a36905e946afe82101d012dbb97303c2e3a300fc9e79c9c3fdf8834c937cf6387e8cd5d40e220ace8945bb
7
+ data.tar.gz: 6385ceab43dfdc5b8104be33957f123dfbdcb96185d0b3331e40278152ab3e8bc2915b63d11a7d0be84b5f09d61e64594f52d4f8c3a74ed349039a6490216b78
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gtin_extras (0.2.0)
4
+ gtin_extras (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # gtin_extras
2
- Ruby String extensions for validating GTIN/UPC/EAN/ASIN/GS1 numbers and formatting.
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
@@ -3,4 +3,5 @@ module GTINExtras
3
3
  require 'gtin_extras/gtin'
4
4
  require 'gtin_extras/asin'
5
5
  require 'gtin_extras/plu'
6
+ require 'gtin_extras/isbn'
6
7
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module GTINExtras
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
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.2.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-07-30 00:00:00.000000000 Z
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