gs1 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 24b04c0b10ceed0d1bc8745d597110030cdd3d61
4
+ data.tar.gz: 6960554532b52b1b2e83caa82eed49d01288b356
5
+ SHA512:
6
+ metadata.gz: edd1c509c1cee9d40ce143f2da1a398af424edeb52d87068d70e78bac40ecac8e8c6fdd16bc6583d55c05f7cfd86d5853d8b7d9d9123b1442d5749796f57779b
7
+ data.tar.gz: c5bb3a874be97053f38a957030da6d163da6f3ac69c083b9404f7bcbcf24bfdf125d0bce3b9049ef0129101109f800f5dd6861b8229409bc31b53d2126ca6067
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - 'spec/**/*'
7
+ - 'gs1.gemspec'
8
+
9
+ Metrics/LineLength:
10
+ Max: 120
11
+
12
+ Style/FrozenStringLiteralComment:
13
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.0
7
+ - 2.5.1
8
+ before_install: gem install bundler -v 1.16.5
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at stefan.ahman@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in gs1.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Stefan Åhman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # GS1
2
+
3
+ GS1 provides the tools to make your code GS1 compliant.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gs1'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gs1
20
+
21
+ ## Usage
22
+
23
+ Follow the examples below to start using the gem.
24
+
25
+ ### Application identifiers
26
+
27
+ To access the defined application identifier:
28
+
29
+ ```ruby
30
+ GS1::AI::GTIN # => "01"
31
+ ```
32
+
33
+ or via the class:
34
+
35
+ ```ruby
36
+ GS1::ExpirationDate::AI # => "17"
37
+ GS1::ExpirationDate.ai # => "17"
38
+ ```
39
+
40
+ To get the class from a numeric id:
41
+
42
+ ```ruby
43
+ GS1::AI_CLASSES[GS1::AI::BATCH_LOT] # => "GS1::Batch"
44
+ ```
45
+
46
+ ### Healthcare barcodes
47
+
48
+ To initialize a healthcare barcode from scan input:
49
+
50
+ ```ruby
51
+ GS1::Barcode::Healthcare.from_scan("01034531200000111719112510ABCD1234\u001E2110")
52
+ # => #<GS1::Barcode::Healthcare:0x007fe1b99ea280
53
+ # @gtin=#<GS1::GTIN:0x007fe1b99e9a10 @errors=[], @data="03453120000011">,
54
+ # @expiration_date=#<GS1::ExpirationDate:0x007fe1b99e8700 @errors=[], @data="191125">,
55
+ # @batch=#<GS1::Batch:0x007fe1b99e3b10 @errors=[], @data="ABCD1234">,
56
+ # @serial_number=#<GS1::SerialNumber:0x007fe1b99e2670 @errors=[], @data="10">,
57
+ # @errors=[]>
58
+ ```
59
+
60
+ or just get the attributes
61
+
62
+ ```ruby
63
+ GS1::Barcode::Healthcare.from_scan("01034531200000111719112510ABCD1234\u001E2110")
64
+ # => {:gtin=>"03453120000011", :expiration_date=>"191125", :batch=>"ABCD1234", :serial_number=>"10"}
65
+ ```
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/apoex/gs1. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
80
+
81
+ ## Code of Conduct
82
+
83
+ Everyone interacting in the Gs1 project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/apoex/gs1/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'gs1'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/gs1.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'gs1/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'gs1'
7
+ spec.version = GS1::VERSION
8
+ spec.authors = ['Stefan Åhman']
9
+ spec.email = ['stefan.ahman@apoex.se']
10
+
11
+ spec.summary = 'A ruby gem for implementing GS1 standards'
12
+ spec.description = 'GS1 defines a set of GS1 standards directly from the documentation'
13
+ spec.homepage = 'https://github.com/apoex/gs1'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ else
21
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
22
+ 'public gem pushes.'
23
+ end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+ spec.required_ruby_version = '>= 2.3.0'
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.16'
36
+ spec.add_development_dependency 'byebug', '~> 10.0'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'rb-readline', '~> 0.1'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ spec.add_development_dependency 'rubocop', '~> 0.1'
41
+ spec.add_development_dependency 'simplecov', '~> 0.1'
42
+ end
data/lib/gs1/ai.rb ADDED
@@ -0,0 +1,294 @@
1
+ # Application identifiers constants.
2
+ #
3
+ # Version 18.0
4
+ # Date: Jan 2018
5
+ # Read more: https://www.gs1.org/standards/barcodes-epcrfid-id-keys/gs1-general-specifications
6
+ # Change log: https://www.gs1.org/genspecs/gscn_archive
7
+ #
8
+ module GS1
9
+ module AI
10
+ #
11
+ # GS1 Application Identifiers starting with digit 0
12
+
13
+ # Serial Shipping Container Code (SSCC)
14
+ SSCC = '00'.freeze
15
+
16
+ # Global Trade Item Number (GTIN)
17
+ GTIN = '01'.freeze
18
+
19
+ # GTIN of contained trade items
20
+ CONTENT = '02'.freeze
21
+
22
+ #
23
+ # GS1 Application Identifiers starting with digit 1
24
+
25
+ # Batch or lot number
26
+ BATCH_LOT = '10'.freeze
27
+
28
+ # Production date (YYMMDD)
29
+ PROD_DATE = '11'.freeze
30
+
31
+ # Due date (YYMMDD)
32
+ DUE_DATE = '12'.freeze
33
+
34
+ # Packaging date (YYMMDD)
35
+ PACK_DATE = '13'.freeze
36
+
37
+ # Best before date (YYMMDD)
38
+ BEST_BY = '15'.freeze
39
+
40
+ # Sell by date (YYMMDD)
41
+ SELL_BY = '16'.freeze
42
+
43
+ # Expiration date (YYMMDD)
44
+ USE_BY = '17'.freeze
45
+
46
+ #
47
+ # GS1 Application Identifiers starting with digit 2
48
+
49
+ # Variant number
50
+ VARIANT = '20'.freeze
51
+
52
+ # Serial number
53
+ SERIAL = '21'.freeze
54
+
55
+ # Consumer product variant
56
+ CPV = '22'.freeze
57
+
58
+ # Additional item identification
59
+ ADDITIONAL_ID = '240'.freeze
60
+
61
+ # Customer part number
62
+ CUST_PART_NO = '241'.freeze
63
+
64
+ # Made-to-Order variation number
65
+ MTO_VARIANT = '242'.freeze
66
+
67
+ # Packaging component number
68
+ PCN = '243'.freeze
69
+
70
+ # Secondary serial number
71
+ SECONDARY_SERIAL = '250'.freeze
72
+
73
+ # Reference to source entity
74
+ REF_TO_SOURCE = '251'.freeze
75
+
76
+ # Global Document Type Identifier (GDTI)
77
+ GDTI = '253'.freeze
78
+
79
+ # GLN extension component
80
+ GLN_EXTENSION_COMPONENT = '254'.freeze
81
+
82
+ # Global Coupon Number (GCN)
83
+ GCN = '255'.freeze
84
+
85
+ #
86
+ # GS1 Application Identifiers starting with digit 3
87
+
88
+ # Count of items (variable measure trade item)
89
+ VAR_COUNT = '30'.freeze
90
+
91
+ #
92
+ # GS1 Application Identifiers starting with digit 4
93
+
94
+ # Customer's purchase order number
95
+ ORDER_NUMBER = '400'.freeze
96
+
97
+ # Global Identification Number for Consignment (GINC)
98
+ GINC = '401'.freeze
99
+
100
+ # Global Shipment Identification Number (GSIN)
101
+ GSIN = '402'.freeze
102
+
103
+ # Routing code
104
+ ROUTE = '403'.freeze
105
+
106
+ # Ship to - Deliver to Global Location Number
107
+ SHIP_TO_LOC = '410'.freeze
108
+
109
+ # Bill to - Invoice to Global Location Number
110
+ BILL_TO = '411'.freeze
111
+
112
+ # Purchased from Global Location Number
113
+ PURCHASE_FROM = '412'.freeze
114
+
115
+ # Ship for - Deliver for - Forward to Global Location Number
116
+ SHIP_FOR_LOC = '413'.freeze
117
+
118
+ # Identification of a physical location - Global Location Number
119
+ LOC_NO = '414'.freeze
120
+
121
+ # Global Location Number of the invoicing party
122
+ PAY_TO = '415'.freeze
123
+
124
+ # GLN of the production or service location
125
+ PROD_SERV_LOC = '416'.freeze
126
+
127
+ # Ship to - Deliver to postal code within a single postal authority
128
+ SHIP_TO_POST = '420'.freeze
129
+
130
+ # Ship to - Deliver to postal code with ISO country code
131
+ SHIP_TO_POST_ISO = '421'.freeze
132
+
133
+ # Country of origin of a trade item
134
+ ORIGIN = '422'.freeze
135
+
136
+ # Country of initial processing
137
+ COUNTRY_INITIAL_PROCESS = '423'.freeze
138
+
139
+ # Country of processing
140
+ COUNTRY_PROCESS = '424'.freeze
141
+
142
+ # Country of disassembly
143
+ COUNTRY_DISASSEMBLY = '425'.freeze
144
+
145
+ # Country covering full process chain
146
+ COUNTRY_FULL_PROCESS = '426'.freeze
147
+
148
+ # Country subdivision Of origin
149
+ ORIGIN_SUBDIVISION = '427'.freeze
150
+
151
+ #
152
+ # GS1 Application Identifiers starting with digit 7
153
+
154
+ # NATO Stock Number (NSN)
155
+ NSN = '7001'.freeze
156
+
157
+ # UN/ECE meat carcasses and cuts classification
158
+ MEAT_CUT = '7002'.freeze
159
+
160
+ # Expiration date and time
161
+ EXPIRY_TIME = '7003'.freeze
162
+
163
+ # Active potency
164
+ ACTIVE_POTENCY = '7004'.freeze
165
+
166
+ # Catch area
167
+ CATCH_AREA = '7005'.freeze
168
+
169
+ # First freeze date
170
+ FIRST_FREEZE_DATE = '7006'.freeze
171
+
172
+ # Harvest date
173
+ HARVEST_DATE = '7007'.freeze
174
+
175
+ # Species for fishery purposes
176
+ AQUATIC_SPECIES = '7008'.freeze
177
+
178
+ # Fishing gear type
179
+ FISHING_GEAR_TYPE = '7009'.freeze
180
+
181
+ # Production method
182
+ PROD_METHOD = '7010'.freeze
183
+
184
+ # Refurbishment lot ID
185
+ REFURB_LOT = '7020'.freeze
186
+
187
+ # Functional status
188
+ FUNC_STAT = '7021'.freeze
189
+
190
+ # Revision status N4+X..20 (FNC1)
191
+ REV_STAT = '7022'.freeze
192
+
193
+ # Global Individual Asset Identifier (GIAI) of an assembly
194
+ GIAI_ASSEMBLY = '7023'.freeze
195
+
196
+ # Number of processor with ISO Country Code N4+N3+X..27 (FNC1)
197
+ PROCESSOR = '703'.freeze
198
+
199
+ #
200
+ # GS1 Application Identifiers starting with digit 8
201
+
202
+ # Roll products (width, length, core diameter, direction, splices)
203
+ DIMENSIONS = '8001'.freeze
204
+
205
+ # Cellular mobile telephone identifier
206
+ CMT_NO = '8002'.freeze
207
+
208
+ # Global Returnable Asset Identifier (GRAI)
209
+ GRAI = '8003'.freeze
210
+
211
+ # Global Individual Asset Identifier (GIAI)
212
+ GIAI = '8004'.freeze
213
+
214
+ # Price per unit of measure
215
+ PRICE_PER_UNIT = '8005'.freeze
216
+
217
+ # Identification of the components of a trade item
218
+ GCTIN = '8006'.freeze
219
+
220
+ # International Bank Account Number (IBAN)
221
+ IBAN = '8007'.freeze
222
+
223
+ # Date and time of production
224
+ PROD_TIME = '8008'.freeze
225
+
226
+ # Component / Part Identifier (CPID)
227
+ CPID = '8010'.freeze
228
+
229
+ # Component / Part Identifier serial number (CPID SERIAL)
230
+ CPID_SERIAL = '8011'.freeze
231
+
232
+ # Software version
233
+ VERSION = '8012'.freeze
234
+
235
+ # Global Model Number
236
+ GMN = '8013'.freeze
237
+
238
+ # Global Service Relation Number to identify the relationship between an organisation offering
239
+ # services and the provider of services
240
+ GSRN_PROVIDER = '8017'.freeze
241
+
242
+ # Global Service Relation Number to identify the relationship between an organisation offering
243
+ # services and the recipient of services
244
+ GSRN_RECIPIENT = '8018'.freeze
245
+
246
+ # Service Relation Instance Number (SRIN)
247
+ SRIN = '8019'.freeze
248
+
249
+ # Payment slip reference number
250
+ REF_NO = '8020'.freeze
251
+
252
+ # Coupon code identification for use in North America
253
+ COUPON_CODE = '8110'.freeze
254
+
255
+ # Loyalty points of a coupon
256
+ POINTS = '8111'.freeze
257
+
258
+ # Extended Packaging URL
259
+ PRODUCT_URL = '8200'.freeze
260
+
261
+ #
262
+ # GS1 Application Identifiers starting with digit 9
263
+
264
+ # Information mutually agreed between trading partners
265
+ INTERNAL = '90'.freeze
266
+
267
+ # Company internal information
268
+ INTERNAL1 = '91'.freeze
269
+
270
+ # Company internal information
271
+ INTERNAL2 = '92'.freeze
272
+
273
+ # Company internal information
274
+ INTERNAL3 = '93'.freeze
275
+
276
+ # Company internal information
277
+ INTERNAL4 = '94'.freeze
278
+
279
+ # Company internal information
280
+ INTERNAL5 = '95'.freeze
281
+
282
+ # Company internal information
283
+ INTERNAL6 = '96'.freeze
284
+
285
+ # Company internal information
286
+ INTERNAL7 = '97'.freeze
287
+
288
+ # Company internal information
289
+ INTERNAL8 = '98'.freeze
290
+
291
+ # Company internal information
292
+ INTERNAL9 = '99'.freeze
293
+ end
294
+ end
@@ -0,0 +1,83 @@
1
+ module GS1
2
+ module Barcode
3
+ # Base class for a barcode.
4
+ #
5
+ class Base
6
+ include Definitions
7
+
8
+ class UnknownRecordError < StandardError; end
9
+
10
+ attr_reader :errors
11
+
12
+ def initialize(options = {})
13
+ self.class.records.each do |record|
14
+ data = options.fetch(record.underscore_name, nil)
15
+
16
+ instance_variable_set("@#{record.underscore_name}", record.new(data))
17
+ end
18
+
19
+ @errors = []
20
+ end
21
+
22
+ class << self
23
+ def from_scan(barcode, separator: "\u001E")
24
+ new(scan_to_params(barcode, separator: separator))
25
+ end
26
+
27
+ def scan_to_params(barcode, separator: "\u001E")
28
+ data = barcode.chars
29
+
30
+ {}.tap do |params|
31
+ params.merge!(scan_to_param(data, separator)) until data.empty?
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def scan_to_param(data, separator)
38
+ record = record_from_data(data)
39
+
40
+ { record.underscore_name => shift_length(data, record, separator) }
41
+ end
42
+
43
+ # Fetch the two first characters (interpreted as AI) from the remaining
44
+ # data and try to find record class. If no record class was found, fetch
45
+ # a third character and try again, and then finally a forth, as no AI
46
+ # currently have more then 4 characters.
47
+ def record_from_data(data)
48
+ ai_variants = []
49
+
50
+ record = process_ai_variants(ai_variants, data, 2) ||
51
+ process_ai_variants(ai_variants, data, 1) ||
52
+ process_ai_variants(ai_variants, data, 1)
53
+
54
+ return record if record
55
+
56
+ raise UnknownRecordError, "Unable to retrieve record from application identifier(s) #{ai_variants.join(', ')}"
57
+ end
58
+
59
+ def process_ai_variants(ai_variants, data, shifts)
60
+ ai_variants << ai_variants.last.to_s + data.shift(shifts).join
61
+
62
+ AI_CLASSES[ai_variants.last]
63
+ end
64
+
65
+ def shift_length(data, record, separator)
66
+ return data.shift(record.barcode_length).join if record.barcode_length
67
+
68
+ if data.find_index(separator) && data.find_index(separator) < record.barcode_max_length
69
+ shift_variable_length(data, separator)
70
+ else
71
+ data.shift(record.barcode_max_length).join
72
+ end
73
+ end
74
+
75
+ def shift_variable_length(data, separator)
76
+ data.shift(data.find_index(separator)).join.tap do
77
+ data.shift unless data.empty? # Shift separator character
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end