Module: GTIN
- Included in:
- String
- Defined in:
- lib/gtin_extras/gtin.rb
Overview
String validation and manipulation for GTIN/EAN/GS1
Instance Method Summary collapse
-
#calculate_check_digit ⇒ Object
FOR A UPC: Ref: www.gs1.org/services/how-calculate-check-digit-manually Ref: www.gtin.info/check-digit-calculator/.
- #coerce_to_upc ⇒ Object
- #ean? ⇒ Boolean (also: #gln?, #ucc?)
- #gs1? ⇒ Boolean
- #gtin_structure ⇒ Object
- #only_gtin_digits? ⇒ Boolean
- #to_gtin(size = 14) ⇒ Object
-
#to_gtin_12 ⇒ Object
(also: #to_upc, #to_upc_a)
GTIN-12 (UPC-A): this is a 12-digit number used primarily in North America.
-
#to_gtin_13 ⇒ Object
(also: #to_ean_13, #to_ucc_13, #to_ean)
GTIN-13 (EAN/UCC-13): this is a 13-digit number used predominately outside of North America.
-
#to_gtin_14 ⇒ Object
GTIN-14 (EAN/UCC-14 or ITF-14): this is a 14-digit number used to identify trade items at various packaging levels.
-
#to_gtin_8 ⇒ Object
(also: #to_ean_8, #to_ucc_8)
GTIN-8 (EAN/UCC-8): this is an 8-digit number used primarily outside of North America.
- #upc? ⇒ Boolean
- #us_upc_checksum ⇒ Object
Instance Method Details
#calculate_check_digit ⇒ Object
FOR A UPC: Ref: www.gs1.org/services/how-calculate-check-digit-manually Ref: www.gtin.info/check-digit-calculator/
40 41 42 43 44 45 46 47 48 |
# File 'lib/gtin_extras/gtin.rb', line 40 def calculate_check_digit factor = 3 sum = 0 (0..(length - 1)).reverse_each do |i| sum += self[i].chr.to_i * factor factor = 4 - factor end (1000 - sum) % 10 end |
#coerce_to_upc ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gtin_extras/gtin.rb', line 93 def coerce_to_upc return self if upc? return unless only_gtin_digits? if gtin_structure == :gs1 new_upc = to_gtin(12) return new_upc if new_upc.upc? end if length == 10 new_upc = "#{to_gtin(11)}#{us_upc_checksum}" return new_upc if new_upc.upc? end return unless length == 11 new_gtin_us = to_gtin(12) # assumes US-based UPC new_gtin_wc = "#{self}#{us_upc_checksum}" return nil if new_gtin_wc.upc? && new_gtin_us.upc? # avoid conflict return new_gtin_us if new_gtin_us.upc? return new_gtin_wc if new_gtin_wc.upc? nil end |
#ean? ⇒ Boolean Also known as: gln?, ucc?
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/gtin_extras/gtin.rb', line 10 def ean? return false unless only_gtin_digits? case length when 13 self[12] == self[0..11].calculate_check_digit.to_s when 8 self[7] == self[0..6].calculate_check_digit.to_s else false end end |
#gs1? ⇒ Boolean
24 25 26 27 28 |
# File 'lib/gtin_extras/gtin.rb', line 24 def gs1? return false if length != 14 return false unless only_gtin_digits? self[13] == self[0..12].calculate_check_digit.to_s end |
#gtin_structure ⇒ Object
3 4 5 6 7 8 |
# File 'lib/gtin_extras/gtin.rb', line 3 def gtin_structure return :upc if upc? return :ean if ean? return :gs1 if gs1? nil end |
#only_gtin_digits? ⇒ Boolean
85 86 87 88 89 90 91 |
# File 'lib/gtin_extras/gtin.rb', line 85 def only_gtin_digits? return unless length > 1 (0..(length - 1)).reverse_each do |i| return nil unless self[i].chr.to_i.to_s == self[i].chr end true end |
#to_gtin(size = 14) ⇒ Object
81 82 83 |
# File 'lib/gtin_extras/gtin.rb', line 81 def to_gtin(size = 14) "%0#{size.to_i}d" % to_s.gsub(/[\D]+/, '').to_i end |
#to_gtin_12 ⇒ Object Also known as: to_upc, to_upc_a
GTIN-12 (UPC-A): this is a 12-digit number used primarily in North America
55 56 57 |
# File 'lib/gtin_extras/gtin.rb', line 55 def to_gtin_12 to_gtin(12) end |
#to_gtin_13 ⇒ Object Also known as: to_ean_13, to_ucc_13, to_ean
GTIN-13 (EAN/UCC-13): this is a 13-digit number used predominately outside of North America
69 70 71 |
# File 'lib/gtin_extras/gtin.rb', line 69 def to_gtin_13 to_gtin(13) end |
#to_gtin_14 ⇒ Object
GTIN-14 (EAN/UCC-14 or ITF-14): this is a 14-digit number used to identify trade items at various packaging levels
77 78 79 |
# File 'lib/gtin_extras/gtin.rb', line 77 def to_gtin_14 to_gtin(14) end |
#to_gtin_8 ⇒ Object Also known as: to_ean_8, to_ucc_8
GTIN-8 (EAN/UCC-8): this is an 8-digit number used primarily outside of North America
62 63 64 |
# File 'lib/gtin_extras/gtin.rb', line 62 def to_gtin_8 to_gtin(8) end |
#upc? ⇒ Boolean
30 31 32 33 34 |
# File 'lib/gtin_extras/gtin.rb', line 30 def upc? return false if length != 12 return false unless only_gtin_digits? self[11] == self[0..10].calculate_check_digit.to_s end |
#us_upc_checksum ⇒ Object
50 51 52 |
# File 'lib/gtin_extras/gtin.rb', line 50 def us_upc_checksum to_gtin(11).calculate_check_digit end |