gtiny 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3ad87eddcdf736731b8e49e2eef1478b57d19145123dc615e2e8a194d7421bce
4
+ data.tar.gz: b894ad68e5e3f8e572a9199f0f48b2e006b8c3cafacf6ff44e302a0acc15e3b4
5
+ SHA512:
6
+ metadata.gz: 6795f9ab893d2d68907fe5f7d8fca39ab9ce66b8c41bf64df439b0a86eb9b23e65607a92feda80be3518fe63b89e91e075b78e841644efbf76ae92b45e601632
7
+ data.tar.gz: a7e5bf1d437244da76ef62a92a670602bc93b6fd1a9daca620431a997e8ffbb965e5acb0d6e9a0e3c053511644dfb37347cc1b0c7d54e6f9a5a4e9089aeeba2b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.0
7
+ NewCops: enable
8
+
9
+ RSpec/NestedGroups:
10
+ AllowedGroups:
11
+ - describe
12
+
13
+ Style/StringLiterals:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Style/StringLiteralsInInterpolation:
18
+ Enabled: true
19
+ EnforcedStyle: double_quotes
20
+
21
+ Layout/LineLength:
22
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-01-16
4
+
5
+ - Initial release!
6
+ - Support for all GTIN formats: GTIN-8, GTIN-12, GTIN-13, and GTIN-14
7
+ - Support for all ISBN formats: ISBN-10 and ISBN-13
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at clayton@juniphq.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in gtiny.gemspec
6
+ gemspec
7
+
8
+ gem "faker" # Lorem ipsum and more!
9
+ gem "rake", "~> 13.0"
10
+ gem "rspec", "~> 3.0"
11
+ gem "rubocop", "~> 1.21"
12
+ gem "rubocop-rake"
13
+ gem "rubocop-rspec"
14
+ gem "steep" # Type checking with .rbs files
data/Gemfile.lock ADDED
@@ -0,0 +1,109 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gtiny (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (7.0.4)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ ast (2.4.2)
15
+ concurrent-ruby (1.1.10)
16
+ csv (3.2.6)
17
+ diff-lcs (1.5.0)
18
+ faker (3.1.0)
19
+ i18n (>= 1.8.11, < 2)
20
+ ffi (1.15.5)
21
+ fileutils (1.7.0)
22
+ i18n (1.12.0)
23
+ concurrent-ruby (~> 1.0)
24
+ json (2.6.2)
25
+ language_server-protocol (3.17.0.2)
26
+ listen (3.8.0)
27
+ rb-fsevent (~> 0.10, >= 0.10.3)
28
+ rb-inotify (~> 0.9, >= 0.9.10)
29
+ logger (1.5.3)
30
+ minitest (5.17.0)
31
+ parallel (1.22.1)
32
+ parser (3.1.3.0)
33
+ ast (~> 2.4.1)
34
+ rainbow (3.1.1)
35
+ rake (13.0.6)
36
+ rb-fsevent (0.11.2)
37
+ rb-inotify (0.10.1)
38
+ ffi (~> 1.0)
39
+ rbs (2.8.3)
40
+ regexp_parser (2.6.1)
41
+ rexml (3.2.5)
42
+ rspec (3.12.0)
43
+ rspec-core (~> 3.12.0)
44
+ rspec-expectations (~> 3.12.0)
45
+ rspec-mocks (~> 3.12.0)
46
+ rspec-core (3.12.0)
47
+ rspec-support (~> 3.12.0)
48
+ rspec-expectations (3.12.0)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.12.0)
51
+ rspec-mocks (3.12.0)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.12.0)
54
+ rspec-support (3.12.0)
55
+ rubocop (1.39.0)
56
+ json (~> 2.3)
57
+ parallel (~> 1.10)
58
+ parser (>= 3.1.2.1)
59
+ rainbow (>= 2.2.2, < 4.0)
60
+ regexp_parser (>= 1.8, < 3.0)
61
+ rexml (>= 3.2.5, < 4.0)
62
+ rubocop-ast (>= 1.23.0, < 2.0)
63
+ ruby-progressbar (~> 1.7)
64
+ unicode-display_width (>= 1.4.0, < 3.0)
65
+ rubocop-ast (1.23.0)
66
+ parser (>= 3.1.1.0)
67
+ rubocop-rake (0.6.0)
68
+ rubocop (~> 1.0)
69
+ rubocop-rspec (2.17.1)
70
+ rubocop (~> 1.33)
71
+ ruby-progressbar (1.11.0)
72
+ securerandom (0.2.2)
73
+ steep (1.3.0)
74
+ activesupport (>= 5.1)
75
+ csv (>= 3.0.9)
76
+ fileutils (>= 1.1.0)
77
+ json (>= 2.1.0)
78
+ language_server-protocol (>= 3.15, < 4.0)
79
+ listen (~> 3.0)
80
+ logger (>= 1.3.0)
81
+ parallel (>= 1.0.0)
82
+ parser (>= 3.1)
83
+ rainbow (>= 2.2.2, < 4.0)
84
+ rbs (>= 2.8.0)
85
+ securerandom (>= 0.1)
86
+ strscan (>= 1.0.0)
87
+ terminal-table (>= 2, < 4)
88
+ strscan (3.0.5)
89
+ terminal-table (3.0.2)
90
+ unicode-display_width (>= 1.1.1, < 3)
91
+ tzinfo (2.0.5)
92
+ concurrent-ruby (~> 1.0)
93
+ unicode-display_width (2.3.0)
94
+
95
+ PLATFORMS
96
+ ruby
97
+
98
+ DEPENDENCIES
99
+ faker
100
+ gtiny!
101
+ rake (~> 13.0)
102
+ rspec (~> 3.0)
103
+ rubocop (~> 1.21)
104
+ rubocop-rake
105
+ rubocop-rspec
106
+ steep
107
+
108
+ BUNDLED WITH
109
+ 2.3.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Junip Inc.
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,57 @@
1
+ [![CI](https://github.com/junip-inc/gtiny/actions/workflows/main.yml/badge.svg)](https://github.com/junip-inc/gtiny/actions/workflows/main.yml)
2
+
3
+ # Gtiny
4
+
5
+ A tiny gem for identifying and validating GTINs in the wild.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'gtiny'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install gtiny
22
+
23
+ ## Usage
24
+
25
+ Parse barcodes:
26
+
27
+ ```ruby
28
+ id = Gtiny.parser.parse!('30152700')
29
+ id.type # => "GTIN-8"
30
+ id.check_digit # => "0"
31
+ id.valid? # => true
32
+ ```
33
+
34
+ Validate barcode check digits:
35
+
36
+ ```ruby
37
+ id = Gtiny::ISBN10.new('0-13-445647-8')
38
+ id.valid? # => false
39
+ ```
40
+
41
+ ## Development
42
+
43
+ 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.
44
+
45
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gtiny. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/gtiny/blob/main/CODE_OF_CONDUCT.md).
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
54
+
55
+ ## Code of Conduct
56
+
57
+ Everyone interacting in the Gtiny project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/gtiny/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
13
+
14
+ # Steep doesn't have a legit rake task class, so we're just invoking the CLI
15
+ # class directly.
16
+ require "steep"
17
+ require "steep/cli"
18
+
19
+ Steep::CLI.new(
20
+ argv: ["check"],
21
+ stdout: $stdout,
22
+ stderr: $stderr,
23
+ stdin: $stdin
24
+ ).run
data/Steepfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ D = Steep::Diagnostic
4
+
5
+ target :lib do
6
+ signature "sig"
7
+ check "lib"
8
+
9
+ configure_code_diagnostics(D::Ruby.lenient)
10
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gtiny
4
+ # An abstract class that defines the base for various types of text-based
5
+ # identifier encodings like GTIN-8, ISBN-10, etc.
6
+ class Encoding
7
+ def self.matches?(candidate)
8
+ regex.match?(candidate)
9
+ end
10
+
11
+ def self.normalize(string)
12
+ string
13
+ end
14
+
15
+ def self.regex
16
+ raise(NotImplementedError, "not implemented")
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :string
22
+
23
+ public
24
+
25
+ def initialize(string, check: true)
26
+ raise(EncodingError, "not a valid encoding") if check && !self.class.matches?(string)
27
+
28
+ @string = self.class.normalize(string.freeze)
29
+ end
30
+
31
+ def to_s
32
+ string
33
+ end
34
+
35
+ def type; end
36
+ def valid?; end
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gtiny
4
+ class Error < StandardError; end
5
+ class EncodingError < Error; end
6
+ class InsufficientData < Error; end
7
+ class ParseError < EncodingError; end
8
+ end
data/lib/gtiny/gtin.rb ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "encoding"
4
+
5
+ module Gtiny
6
+ # Any of GTIN 8, 12, 13, and 14 may be used in an ITF-14 or GS1-128, as long
7
+ # as they are formatted in 14 digits.
8
+ #
9
+ # Any GTIN can be formatted as 14 digits by padding the left hand side with
10
+ # zeroes.
11
+ #
12
+ # A GTIN may be encoded in EAN/UPC, ITF-14, GS1-128, GS1 DataBar, and GS1
13
+ # DataMatrix. The GTIN may also be encoded in EPC scheme for RFID tags or in
14
+ # QR Code and Data Matrix when using GS1 Digital Link URI.
15
+ class GTIN < Encoding
16
+ def valid?
17
+ self.class.regex.match?(string) && valid_check_digit?
18
+ end
19
+
20
+ def check_digit
21
+ string[-1]
22
+ end
23
+
24
+ def company_prefix_and_item_reference
25
+ string[0..-2]
26
+ end
27
+
28
+ private
29
+
30
+ def valid_check_digit?
31
+ sum = string.reverse.each_char.with_index.sum do |digit, idx|
32
+ multiplier = (idx % 2).zero? ? 1 : 3
33
+ multiplier * digit.to_i
34
+ end
35
+
36
+ (sum % 10).zero?
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gtin"
4
+
5
+ module Gtiny
6
+ # GTIN-12: Used for UPC-A barcodes
7
+ class GTIN12 < GTIN
8
+ def self.regex
9
+ /\A[0-9]{12}\z/
10
+ end
11
+
12
+ def type
13
+ "GTIN-12"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gtin"
4
+
5
+ module Gtiny
6
+ # GTIN-13: Used for EAN-13 barcodes.
7
+ class GTIN13 < GTIN
8
+ # GS1 company prefix starts with 1-9.
9
+ def self.regex
10
+ /\A[1-9][0-9]{12}\z/
11
+ end
12
+
13
+ def type
14
+ "GTIN-13"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gtin"
4
+
5
+ module Gtiny
6
+ # GTIN-14: Cannot be used in EAN/UPC barcodes
7
+ class GTIN14 < GTIN
8
+ def self.regex
9
+ /\A[0-9]{14}\z/
10
+ end
11
+
12
+ def type
13
+ "GTIN-14"
14
+ end
15
+
16
+ # Indicator digit indicates packagaing level.
17
+ # -> A value of 0 indicates absence of information.
18
+ # -> A value of 9 indicates variable measure.
19
+ def indicator_digit
20
+ string[0]
21
+ end
22
+
23
+ def company_prefix_and_item_reference
24
+ string[1..-2]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gtin"
4
+
5
+ module Gtiny
6
+ # GTIN-8: Used for EAN-8 barcodes.
7
+ class GTIN8 < GTIN
8
+ def self.regex
9
+ /\A[0-9]{8}\z/
10
+ end
11
+
12
+ def type
13
+ "GTIN-8"
14
+ end
15
+ end
16
+ end
data/lib/gtiny/isbn.rb ADDED
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "encoding"
4
+
5
+ module Gtiny
6
+ # It seems like GTINs can also be encoded as ISBNs, which are either 10 or 13
7
+ # digits.
8
+ #
9
+ # A 10 digit ISBN can be converted to 13 digits by prefixing it with 978.
10
+ #
11
+ # ISBN-13s always have a 978 or 979 prefix, though that may be expanded in
12
+ # the future.
13
+ #
14
+ # Prior to ISBNs, "SBNs" were used and were either 9 or 12 digits. A 9 digit
15
+ # SBN can be converted to an ISBN by prefixing it with a single zero. A 12
16
+ # digit SBN can be converted to a 9 digit SBN by dropping the last three
17
+ # digits, which were often the price of the book.
18
+ class ISBN < Encoding
19
+ def self.normalize(string)
20
+ string.downcase.gsub(" ", "-")
21
+ end
22
+
23
+ def check_digit
24
+ string[-1]
25
+ end
26
+
27
+ def group_identifier
28
+ check_number_of_groups!
29
+
30
+ groups[-4]
31
+ end
32
+
33
+ def publisher_identifier
34
+ check_number_of_groups!
35
+
36
+ groups[-3]
37
+ end
38
+
39
+ def title_identifier
40
+ check_number_of_groups!
41
+
42
+ groups[-2]
43
+ end
44
+
45
+ def valid?
46
+ self.class.regex.match?(string) &&
47
+ digits.size == canonical_digits_size &&
48
+ valid_check_digit?
49
+ end
50
+
51
+ private
52
+
53
+ def digits
54
+ @digits ||= string.gsub("-", "")
55
+ end
56
+
57
+ def groups
58
+ @groups ||= string.split("-")
59
+ end
60
+
61
+ def canonical_digits_size; end
62
+ def canonical_groups_size; end
63
+
64
+ def check_number_of_groups!
65
+ return if groups.size == canonical_groups_size
66
+
67
+ raise(InsufficientData, "initialized with ambiguous format")
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "isbn"
4
+
5
+ module Gtiny
6
+ # ISBN-10: Used for books published before 2007.
7
+ class ISBN10 < ISBN
8
+ # The ISBN-10 format consists of 4 groups, separated by hyphens (or spaces).
9
+ # 1. The group identifier, up to five digits
10
+ # 2. The publisher identifier, up to seven digits
11
+ # 3. The title identifier, up to 6 digits
12
+ # 4. The check digit
13
+ def self.regex
14
+ /\A[0-9]{1,5}[- ]?[0-9]{1,7}[- ]?[0-9]{1,6}[- ]?[0-9xX]\z/
15
+ end
16
+
17
+ def type
18
+ "ISBN-10"
19
+ end
20
+
21
+ private
22
+
23
+ # Since ISBN-10 uses mod 11 to calculate the check digit, the last digit
24
+ # can be 10, which is represented with an 'x'.
25
+ def valid_check_digit?
26
+ sum = digits.reverse.each_char.with_index.sum do |digit, idx|
27
+ multiplier = idx + 1
28
+ value = digit == "x" ? 10 : digit.to_i
29
+
30
+ multiplier * value
31
+ end
32
+
33
+ (sum % 11).zero?
34
+ end
35
+
36
+ def canonical_digits_size
37
+ 10
38
+ end
39
+
40
+ def canonical_groups_size
41
+ 4
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "isbn"
4
+
5
+ module Gtiny
6
+ # ISBN-13: Used for all books published on or after January 1st, 2007.
7
+ class ISBN13 < ISBN
8
+ # The ISBN-13 format consists of 5 groups, separated by hyphens (or spaces).
9
+ # 1. The prefix element, 3 digits - so far only 978 and 979 are used.
10
+ # 2. The group identifier, up to five digits
11
+ # 3. The publisher identifier, up to seven digits
12
+ # 4. The title identifier, up to 6 digits
13
+ # 5. The check digit
14
+ def self.regex
15
+ /\A97(?:8|9)[- ]?[0-9]{1,5}[- ]?[0-9]{1,7}[- ]?[0-9]{1,6}[- ]?[0-9]\z/
16
+ end
17
+
18
+ def type
19
+ "ISBN-13"
20
+ end
21
+
22
+ def prefix_element
23
+ check_number_of_groups!
24
+
25
+ groups[0]
26
+ end
27
+
28
+ private
29
+
30
+ # This is effectively a GTIN-13, so the check digit algorithm is the same.
31
+ def valid_check_digit?
32
+ sum = digits.reverse.each_char.with_index.sum do |digit, idx|
33
+ multiplier = (idx % 2).zero? ? 1 : 3
34
+ multiplier * digit.to_i
35
+ end
36
+
37
+ (sum % 10).zero?
38
+ end
39
+
40
+ def canonical_digits_size
41
+ 13
42
+ end
43
+
44
+ def canonical_groups_size
45
+ 5
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gtiny
4
+ # A class that will check a string against registered encoding classes.
5
+ class Parser
6
+ def initialize
7
+ @registrants = []
8
+ end
9
+
10
+ def register(klass)
11
+ @registrants << klass
12
+ end
13
+
14
+ def parse(string)
15
+ @registrants.each do |registrant|
16
+ next unless registrant.matches?(string)
17
+
18
+ identifier = registrant.new(string, check: false)
19
+ next unless identifier.valid?
20
+
21
+ return identifier
22
+ end
23
+
24
+ nil
25
+ end
26
+
27
+ def parse!(string)
28
+ identifier = parse(string)
29
+ return identifier unless identifier.nil?
30
+
31
+ raise(ParseError, "not a known identifier")
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gtiny
4
+ VERSION = "0.1.0"
5
+ end
data/lib/gtiny.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gtiny/errors"
4
+ require_relative "gtiny/parser"
5
+ require_relative "gtiny/version"
6
+
7
+ require_relative "gtiny/gtin8"
8
+ require_relative "gtiny/gtin12"
9
+ require_relative "gtiny/gtin13"
10
+ require_relative "gtiny/gtin14"
11
+ require_relative "gtiny/isbn10"
12
+ require_relative "gtiny/isbn13"
13
+
14
+ # A tiny gem for identifying and validating GTINs in the wild.
15
+ module Gtiny
16
+ def self.parser
17
+ @parser ||= Parser.new.tap do |parser|
18
+ parser.register(ISBN10)
19
+ parser.register(ISBN13)
20
+ parser.register(GTIN8)
21
+ parser.register(GTIN12)
22
+ parser.register(GTIN13)
23
+ parser.register(GTIN14)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ module Gtiny
2
+ # An abstract class that defines the base for various types of text-based
3
+ # identifier encodings like GTIN-8, ISBN-10, etc.
4
+ class Encoding
5
+ def self.matches?: (String candidate) -> bool
6
+
7
+ def self.normalize: (String string) -> String
8
+
9
+ def self.regex: () -> Regexp
10
+
11
+ private
12
+
13
+ attr_reader string: String
14
+
15
+ public
16
+
17
+ def initialize: (String string, ?check: bool) -> void
18
+
19
+ def to_s: () -> String
20
+
21
+ def type: () -> nil
22
+
23
+ def valid?: () -> nil
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ module Gtiny
2
+ class Error < StandardError
3
+ end
4
+
5
+ class EncodingError < Error
6
+ end
7
+
8
+ class InsufficientData < Error
9
+ end
10
+ end
@@ -0,0 +1,22 @@
1
+ module Gtiny
2
+ # Any of GTIN 8, 12, 13, and 14 may be used in an ITF-14 or GS1-128, as long
3
+ # as they are formatted in 14 digits.
4
+ #
5
+ # Any GTIN can be formatted as 14 digits by padding the left hand side with
6
+ # zeroes.
7
+ #
8
+ # A GTIN may be encoded in EAN/UPC, ITF-14, GS1-128, GS1 DataBar, and GS1
9
+ # DataMatrix. The GTIN may also be encoded in EPC scheme for RFID tags or in
10
+ # QR Code and Data Matrix when using GS1 Digital Link URI.
11
+ class GTIN < Encoding
12
+ def valid?: () -> bool
13
+
14
+ def check_digit: () -> (String | nil)
15
+
16
+ def company_prefix_and_item_reference: () -> (String | nil)
17
+
18
+ private
19
+
20
+ def valid_check_digit?: () -> bool
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ module Gtiny
2
+ # GTIN-12: Used for UPC-A barcodes
3
+ class GTIN12 < GTIN
4
+ def self.regex: () -> ::Regexp
5
+
6
+ def type: () -> "GTIN-12"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Gtiny
2
+ # GTIN-13: Used for EAN-13 barcodes.
3
+ class GTIN13 < GTIN
4
+ def self.regex: () -> ::Regexp
5
+
6
+ def type: () -> "GTIN-13"
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ module Gtiny
2
+ # GTIN-14: Cannot be used in EAN/UPC barcodes
3
+ class GTIN14 < GTIN
4
+ def self.regex: () -> ::Regexp
5
+
6
+ def type: () -> "GTIN-14"
7
+
8
+ def indicator_digit: () -> (String | nil)
9
+
10
+ def company_prefix_and_item_reference: () -> (String | nil)
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Gtiny
2
+ # GTIN-8: Used for EAN-8 barcodes.
3
+ class GTIN8 < GTIN
4
+ def self.regex: () -> ::Regexp
5
+
6
+ def type: () -> "GTIN-8"
7
+ end
8
+ end
@@ -0,0 +1,42 @@
1
+ module Gtiny
2
+ # It seems like GTINs can also be encoded as ISBNs, which are either 10 or 13
3
+ # digits.
4
+ #
5
+ # A 10 digit ISBN can be converted to 13 digits by prefixing it with 978.
6
+ #
7
+ # ISBN-13s always have a 978 or 979 prefix, though that may be expanded in
8
+ # the future.
9
+ #
10
+ # Prior to ISBNs, "SBNs" were used and were either 9 or 12 digits. A 9 digit
11
+ # SBN can be converted to an ISBN by prefixing it with a single zero. A 12
12
+ # digit SBN can be converted to a 9 digit SBN by dropping the last three
13
+ # digits, which were often the price of the book.
14
+ class ISBN < Encoding
15
+ def self.normalize: (String string) -> String
16
+
17
+ def check_digit: () -> (String | nil)
18
+
19
+ def group_identifier: () -> (String | nil)
20
+
21
+ def publisher_identifier: () -> (String | nil)
22
+
23
+ def title_identifier: () -> (String | nil)
24
+
25
+ def valid?: () -> bool
26
+
27
+ private
28
+
29
+ @digits: String
30
+ @groups: Array[String]
31
+
32
+ def digits: () -> String
33
+
34
+ def groups: () -> Array[String]
35
+
36
+ def canonical_digits_size: () -> nil
37
+
38
+ def canonical_groups_size: () -> nil
39
+
40
+ def check_number_of_groups!: () -> untyped
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ module Gtiny
2
+ # ISBN-10: Used for books published before 2007.
3
+ class ISBN10 < ISBN
4
+ def self.regex: () -> ::Regexp
5
+
6
+ def type: () -> "ISBN-10"
7
+
8
+ private
9
+
10
+ def valid_check_digit?: () -> bool
11
+
12
+ def canonical_digits_size: () -> 10
13
+
14
+ def canonical_groups_size: () -> 4
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module Gtiny
2
+ # ISBN-13: Used for all books published on or after January 1st, 2007.
3
+ class ISBN13 < ISBN
4
+ def self.regex: () -> ::Regexp
5
+
6
+ def type: () -> "ISBN-13"
7
+
8
+ def prefix_element: () -> (String | nil)
9
+
10
+ private
11
+
12
+ def valid_check_digit?: () -> bool
13
+
14
+ def canonical_digits_size: () -> 13
15
+
16
+ def canonical_groups_size: () -> 5
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ module Gtiny
2
+ # A class that will check a string against registered encoding classes.
3
+ class Parser
4
+ @registrants: Array[singleton(Encoding)]
5
+
6
+ def initialize: () -> void
7
+
8
+ def register: (singleton(Encoding) klass) -> untyped
9
+
10
+ def parse: (String string) -> (Encoding | nil)
11
+
12
+ def parse!: (String string) -> Encoding
13
+ end
14
+ end
data/sig/gtiny.rbs ADDED
@@ -0,0 +1,9 @@
1
+ module Gtiny
2
+ VERSION: String
3
+
4
+ # This syntax was a guess and steep seems to be accepting it. Not sure if
5
+ # this was a lucky guess or if the type checker is just choking on it.
6
+ self.@parser: Parser
7
+
8
+ def self.parser: () -> Parser
9
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gtiny
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Clayton Passmore
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - clayton@juniphq.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - Steepfile
30
+ - lib/gtiny.rb
31
+ - lib/gtiny/encoding.rb
32
+ - lib/gtiny/errors.rb
33
+ - lib/gtiny/gtin.rb
34
+ - lib/gtiny/gtin12.rb
35
+ - lib/gtiny/gtin13.rb
36
+ - lib/gtiny/gtin14.rb
37
+ - lib/gtiny/gtin8.rb
38
+ - lib/gtiny/isbn.rb
39
+ - lib/gtiny/isbn10.rb
40
+ - lib/gtiny/isbn13.rb
41
+ - lib/gtiny/parser.rb
42
+ - lib/gtiny/version.rb
43
+ - sig/gtiny.rbs
44
+ - sig/gtiny/encoding.rbs
45
+ - sig/gtiny/errors.rbs
46
+ - sig/gtiny/gtin.rbs
47
+ - sig/gtiny/gtin12.rbs
48
+ - sig/gtiny/gtin13.rbs
49
+ - sig/gtiny/gtin14.rbs
50
+ - sig/gtiny/gtin8.rbs
51
+ - sig/gtiny/isbn.rbs
52
+ - sig/gtiny/isbn10.rbs
53
+ - sig/gtiny/isbn13.rbs
54
+ - sig/gtiny/parser.rbs
55
+ homepage: https://github.com/junip-inc/gtiny
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ allowed_push_host: https://rubygems.org
60
+ rubygems_mfa_required: 'true'
61
+ homepage_uri: https://github.com/junip-inc/gtiny
62
+ source_code_uri: https://github.com/junip-inc/gtiny
63
+ changelog_uri: https://github.com/junip-inc/gtiny/blob/main/CHANGELOG.md
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.0.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.3.7
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: A tiny gem for identifying and validating GTINs in the wild.
83
+ test_files: []