Class: Barkick::GTIN

Inherits:
Object
  • Object
show all
Defined in:
lib/barkick/gtin.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, type: nil) ⇒ GTIN

Returns a new instance of GTIN.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/barkick/gtin.rb', line 3

def initialize(number, type: nil)
  @number = number.to_s

  if @number.length == 8
    raise ArgumentError, "Must specify type for 8-digit codes" unless type

    if type == :upc_e
      upc_a =
        case @number[-2]
        when "0"
          "#{@number[1..2]}00000#{@number[3..5]}"
        when "1", "2"
          "#{@number[1..2]}#{@number[-2]}0000#{@number[3..5]}"
        when "3"
          "#{@number[1..3]}00000#{@number[4..5]}"
        when "4"
          "#{@number[1..4]}00000#{@number[5]}"
        else
          "#{@number[1..5]}0000#{@number[-2]}"
        end

      upc_a = "0#{upc_a}#{@number[-1]}"

      if self.class.check_digit(upc_a[0..-2]) == @number[-1]
        @number = upc_a
      end
    end
  end

  @type = type
end

Class Method Details

.check_digit(number) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/barkick/gtin.rb', line 242

def self.check_digit(number)
  number = number.to_s
  if [7, 11, 12, 13].include?(number.length)
    # https://www.gs1.org/barcodes/support/check_digit_calculator#how
    digits = number.rjust(13, "0").split("").map(&:to_i)
    # digit at position 0 is odd (first digit) for the purpose of this calculation
    odd_digits, even_digits = digits.partition.each_with_index { |_digit, i| i.even? }
    ((10 - (odd_digits.sum * 3 + even_digits.sum) % 10) % 10).to_s
  else
    nil
  end
end

Instance Method Details

#base_gtin14Object



61
62
63
64
65
66
67
68
# File 'lib/barkick/gtin.rb', line 61

def base_gtin14
  if variable?
    base = gtin14[0..-7] + "00000"
    base + self.class.check_digit(base)
  else
    gtin14
  end
end

#book?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/barkick/gtin.rb', line 220

def book?
  prefix_name == "Bookland"
end

#check_digitObject



53
54
55
# File 'lib/barkick/gtin.rb', line 53

def check_digit
  gtin14[-1]
end

#country_codeObject



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/barkick/gtin.rb', line 208

def country_code
  case prefix_name
  when "GS1 US" then "US"
  when "GS1 UK" then "GB"
  when "GS1 Germany" then "DE"
  when "GS1 Netherlands" then "NL"
  when "GS1 Schweiz, Suisse, Svizzera" then "CH"
  when "GS1 Italy" then "IT"
  when "GS1 France" then "FR"
  end
end

#gtin12Object Also known as: upc



48
49
50
# File 'lib/barkick/gtin.rb', line 48

def gtin12
  gtin14[0..1] == "00" ? gtin14[2..-1] : nil
end

#gtin13Object Also known as: ean13



43
44
45
# File 'lib/barkick/gtin.rb', line 43

def gtin13
  gtin14[0] == "0" ? gtin14[1..-1] : nil
end

#gtin14Object



35
36
37
38
39
40
41
# File 'lib/barkick/gtin.rb', line 35

def gtin14
  if valid?
    @number.rjust(14, "0")
  else
    nil
  end
end

#prefixObject

prefix



72
73
74
# File 'lib/barkick/gtin.rb', line 72

def prefix
  gtin14[1..3]
end

#prefix_nameObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/barkick/gtin.rb', line 77

def prefix_name
  case prefix.to_i
  when 0..19, 30..39, 60..139
    if @type == :ean8
      nil # GTIN-8
    else
      "GS1 US"
    end
  when 20..29, 40..49, 200..299 then "Restricted distribution"
  when 50..59 then "Coupons"
  when 300..379 then "GS1 France"
  when 380 then "GS1 Bulgaria"
  when 383 then "GS1 Slovenija"
  when 385 then "GS1 Croatia"
  when 387 then "GS1 BIH (Bosnia-Herzegovina)"
  when 389 then "GS1 Montenegro"
  when 400..440 then "GS1 Germany"
  when 450..459, 490..499 then "GS1 Japan"
  when 460..469 then "GS1 Russia"
  when 470 then "GS1 Kyrgyzstan"
  when 471 then "GS1 Taiwan"
  when 474 then "GS1 Estonia"
  when 475 then "GS1 Latvia"
  when 476 then "GS1 Azerbaijan"
  when 477 then "GS1 Lithuania"
  when 478 then "GS1 Uzbekistan"
  when 479 then "GS1 Sri Lanka"
  when 480 then "GS1 Philippines"
  when 481 then "GS1 Belarus"
  when 482 then "GS1 Ukraine"
  when 484 then "GS1 Moldova"
  when 485 then "GS1 Armenia"
  when 486 then "GS1 Georgia"
  when 487 then "GS1 Kazakstan"
  when 488 then "GS1 Tajikistan"
  when 489 then "GS1 Hong Kong"
  when 500..509 then "GS1 UK"
  when 520..521 then "GS1 Association Greece"
  when 528 then "GS1 Lebanon"
  when 529 then "GS1 Cyprus"
  when 530 then "GS1 Albania"
  when 531 then "GS1 MAC (FYR Macedonia)"
  when 535 then "GS1 Malta"
  when 539 then "GS1 Ireland"
  when 540..549 then "GS1 Belgium & Luxembourg"
  when 560 then "GS1 Portugal"
  when 569 then "GS1 Iceland"
  when 570..579 then "GS1 Denmark"
  when 590 then "GS1 Poland"
  when 594 then "GS1 Romania"
  when 599 then "GS1 Hungary"
  when 600..601 then "GS1 South Africa"
  when 603 then "GS1 Ghana"
  when 604 then "GS1 Senegal"
  when 608 then "GS1 Bahrain"
  when 609 then "GS1 Mauritius"
  when 611 then "GS1 Morocco"
  when 613 then "GS1 Algeria"
  when 615 then "GS1 Nigeria"
  when 616 then "GS1 Kenya"
  when 618 then "GS1 Ivory Coast"
  when 619 then "GS1 Tunisia"
  when 620 then "GS1 Tanzania"
  when 621 then "GS1 Syria"
  when 622 then "GS1 Egypt"
  when 623 then "GS1 Brunei"
  when 624 then "GS1 Libya"
  when 625 then "GS1 Jordan"
  when 626 then "GS1 Iran"
  when 627 then "GS1 Kuwait"
  when 628 then "GS1 Saudi Arabia"
  when 629 then "GS1 Emirates"
  when 640..649 then "GS1 Finland"
  when 690..699 then "GS1 China"
  when 700..709 then "GS1 Norway"
  when 729 then "GS1 Israel"
  when 730..739 then "GS1 Sweden"
  when 740 then "GS1 Guatemala"
  when 741 then "GS1 El Salvador"
  when 742 then "GS1 Honduras"
  when 743 then "GS1 Nicaragua"
  when 744 then "GS1 Costa Rica"
  when 745 then "GS1 Panama"
  when 746 then "GS1 Republica Dominicana"
  when 750 then "GS1 Mexico"
  when 754..755 then "GS1 Canada"
  when 759 then "GS1 Venezuela"
  when 760..769 then "GS1 Schweiz, Suisse, Svizzera"
  when 770..771 then "GS1 Colombia"
  when 773 then "GS1 Uruguay"
  when 775 then "GS1 Peru"
  when 777 then "GS1 Bolivia"
  when 778..779 then "GS1 Argentina"
  when 780 then "GS1 Chile"
  when 784 then "GS1 Paraguay"
  when 786 then "GS1 Ecuador"
  when 789..790 then "GS1 Brasil"
  when 800..839 then "GS1 Italy"
  when 840..849 then "GS1 Spain"
  when 850 then "GS1 Cuba"
  when 858 then "GS1 Slovakia"
  when 859 then "GS1 Czech"
  when 860 then "GS1 Serbia"
  when 865 then "GS1 Mongolia"
  when 867 then "GS1 North Korea"
  when 868..869 then "GS1 Turkey"
  when 870..879 then "GS1 Netherlands"
  when 880 then "GS1 South Korea"
  when 884 then "GS1 Cambodia"
  when 885 then "GS1 Thailand"
  when 888 then "GS1 Singapore"
  when 890 then "GS1 India"
  when 893 then "GS1 Vietnam"
  when 896 then "GS1 Pakistan"
  when 899 then "GS1 Indonesia"
  when 900..919 then "GS1 Austria"
  when 930..939 then "GS1 Australia"
  when 940..949 then "GS1 New Zealand"
  when 950 then "GS1 Global Office"
  when 951 then "GS1 Global Office (EPCglobal)"
  when 955 then "GS1 Malaysia"
  when 958 then "GS1 Macau"
  when 960..969 then "Global Office (GTIN-8s)"
  when 977 then "Serial publications (ISSN)"
  when 978..979 then "Bookland"
  when 980 then "Refund receipts"
  when 981..984 then "GS1 coupon identification for common currency areas"
  when 990..999 then "GS1 coupon identification"
  end
end

#priceObject



234
235
236
237
238
239
240
# File 'lib/barkick/gtin.rb', line 234

def price
  if variable?
    gtin14[-5..-2].to_f / 100
  else
    nil
  end
end

#restricted?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/barkick/gtin.rb', line 224

def restricted?
  prefix_name == "Restricted distribution"
end

#valid?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/barkick/gtin.rb', line 57

def valid?
  @number =~ /\A\d{8}(\d{4,6})?\z/ && self.class.check_digit(@number.rjust(14, "0")[0..-2]) == @number[-1]
end

#variable?Boolean

variable weight

Returns:

  • (Boolean)


230
231
232
# File 'lib/barkick/gtin.rb', line 230

def variable?
  (20..29).cover?(prefix.to_i)
end