salsify-gtin 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: 0a8c676f5728c7d682a37f142e9c33bb315d85513eb1a5638fe51a5c072c2e0a
4
+ data.tar.gz: ffb738ff12692ab87efe8763e1869eeafcdde038d22942026f3c546bd585e252
5
+ SHA512:
6
+ metadata.gz: '09f94b7bac8f4c3bba93150bb2dcf4f09550578ce1556713b0ba835678b3706aadb5529f74228b16833817c620cdc57d134a5d0a7fa8d86c43f66b3ae793b707'
7
+ data.tar.gz: 7f2bc78e96b9cd8e9680eb5ecd890706394123571ea10da790003514de77eb808b9fddf6d9c5e531805b639fc48f5a05e49fdbd8085fec070b0402d86f3a4021
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.init ADDED
File without changes
data/.overcommit.yml ADDED
@@ -0,0 +1,13 @@
1
+ PreCommit:
2
+ RuboCop:
3
+ enabled: true
4
+ required: false
5
+ on_warn: fail
6
+
7
+ HardTabs:
8
+ enabled: true
9
+ required: false
10
+
11
+ CommitMsg:
12
+ TrailingPeriod:
13
+ enabled: false
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ inherit_gem:
2
+ salsify_rubocop: conf/rubocop.yml
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - 'vendor/**/*'
7
+ - 'gemfiles/vendor/**/*'
8
+
9
+ Style/WordArray:
10
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.5.1
4
+ before_install: gem install bundler -v 1.12.4
5
+ script:
6
+ - bundle exec rubocop
7
+ - bundle exec rspec
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
6
+
7
+ ## Unreleased
8
+
9
+ ## 0.1.0 - 2019-01-30
10
+ ### Added
11
+ - Initial version
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # override the :github shortcut to be secure by using HTTPS
4
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}.git" }
5
+
6
+ # Specify your gem's dependencies in gtin.gemspec
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Salsify, 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.
22
+
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # GTIN
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'gtin'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gtin
18
+
19
+ ## Usage
20
+
21
+ This gem is all about validating GTIN-compatible identifiers and converting
22
+ them to a standardized GTIN-14 representation. Supported input types include
23
+ GTIN, EAN-13, UPC-A, UPC-E, ISBN-10, ISBN-13, and ISSN. Inputs must be strings
24
+ containing only digits and include the check digit (see examples below).
25
+
26
+ ### Examples:
27
+
28
+ ```
29
+ # UPC-A
30
+ GTIN.to_gtin('UPC', '123412341230') => '00123412341230'
31
+
32
+ # UPC-E
33
+ GTIN.to_gtin('UPC', '1236432') => '00012300000642'
34
+
35
+ # ISBN-10
36
+ GTIN.to_gtin('ISBN', '0306406152') => '09780306406157'
37
+
38
+ # ISBN-13 with an incorrect check digit
39
+ GTIN.to_gtin('ISBN', '9780306406150') => GtinValidationError (invalid checksum)
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
45
+ run `rake spec` to run the tests. You can also run `bin/console` for an
46
+ interactive prompt that will allow you to experiment.
47
+
48
+ To install this gem onto your local machine, run `bundle exec rake install`.
49
+
50
+ To release a new version, update the version number in `version.rb`, and then
51
+ run `bundle exec rake release`, which will create a git tag for the version,
52
+ push git commits and tags, and push the `.gem` file to
53
+ [rubygems.org](https://rubygems.org)
54
+ .
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at
59
+ https://github.com/salsify/gtin.## License
60
+
61
+ The gem is available as open source under the terms of the
62
+ [MIT License](http://opensource.org/licenses/MIT).
63
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'gtin'
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -v
5
+
6
+ bundle update
7
+
8
+ overcommit --install
data/gtin.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'gtin/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'salsify-gtin'
7
+ spec.version = GTIN::VERSION
8
+ spec.authors = ['Salsify, Inc']
9
+ spec.email = ['engineering@salsify.com']
10
+
11
+ spec.summary = 'Validates and converts GTIN variants to standardized GTIN-14 representation'
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/salsify/gtin'
14
+
15
+ spec.license = 'MIT'
16
+
17
+
18
+ # Set 'allowed_push_post' to control where this gem can be published.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+
22
+ else
23
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ spec.bindir = 'bin'
28
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_development_dependency 'bundler', '~> 1.12'
32
+ spec.add_development_dependency 'overcommit'
33
+ spec.add_development_dependency 'pry-byebug'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.4'
36
+
37
+ spec.add_development_dependency 'salsify_rubocop', '~> 0.52.1'
38
+ end
data/lib/gtin.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'gtin/version'
2
+ require 'gtin/gtin'
data/lib/gtin/gtin.rb ADDED
@@ -0,0 +1,116 @@
1
+ module GTIN
2
+ extend self
3
+
4
+ Converter = Struct.new(:standardizer, :checksum_computer)
5
+
6
+ GtinConverter = Converter.new(:standardize_gtin, :compute_checksum_gtin)
7
+ UpcEConverter = Converter.new(:standardize_upce, :compute_checksum_upce)
8
+ IsbnConverter = Converter.new(:standardize_isbn10, :compute_checksum_isbn10)
9
+ IssnConverter = Converter.new(:standardize_issn8, :compute_checksum_issn8)
10
+
11
+ CONVERTER_MAP = {
12
+ [/(GTIN|EAN)(-8)?/i, 8] => GtinConverter,
13
+ [/(GTIN|EAN)(-12)?/i, 12] => GtinConverter,
14
+ [/(GTIN|EAN|ISBN|ISSN)(-13)?/i, 13] => GtinConverter,
15
+ [/(GTIN|EAN)(-14)?/i, 14] => GtinConverter,
16
+ [/UPC(-A)?/i, 12] => GtinConverter,
17
+ [/UPC(-E)?/i, 7] => UpcEConverter,
18
+ [/ISBN(-10)?/i, 10] => IsbnConverter,
19
+ [/ISSN(-8)?/i, 8] => IssnConverter
20
+ }.freeze
21
+
22
+ GTIN_COMPATIBLE_ID_TYPES = %w(UPC GTIN EAN ISSN ISBN).freeze
23
+
24
+ GtinValidationError = Class.new(StandardError)
25
+
26
+ # raises GtinValidationError if the provided value has an invalid checksum
27
+ def to_gtin(id_type, value)
28
+ zero_pad(standardize(id_type, value))
29
+ end
30
+
31
+ def zero_pad(gtin)
32
+ gtin.rjust(14, '0')
33
+ end
34
+
35
+ def standardize(id_type, value, validate_checksum: true)
36
+ fail_on_invalid_checksum(id_type, value) if validate_checksum
37
+ converter = get_converter(id_type, value.length)
38
+ send(converter.standardizer, value)
39
+ end
40
+
41
+ def get_converter(id_type, value_length)
42
+ CONVERTER_MAP.each do |(type_matcher, length), converter|
43
+ return converter if type_matcher.match?(id_type) && value_length == length
44
+ end
45
+
46
+ raise GtinValidationError.new("No gtin converter found for #{id_type} with length #{value_length}")
47
+ end
48
+
49
+ def standardize_gtin(gtin)
50
+ gtin
51
+ end
52
+
53
+ def standardize_upce(upce)
54
+ # http://www.taltech.com/barcodesoftware/symbologies/upc
55
+ case upce[5]
56
+ when '0', '1', '2'
57
+ with_checksum('0' + upce.slice(0, 2) + upce[5] + '0000' + upce.slice(2, 3))
58
+ when '3'
59
+ with_checksum('0' + upce.slice(0, 3) + '00000' + upce.slice(3, 2))
60
+ when '4'
61
+ with_checksum('0' + upce.slice(0, 4) + '00000' + upce[4])
62
+ else
63
+ with_checksum('0' + upce.slice(0, 5) + '0000' + upce[5])
64
+ end
65
+ end
66
+
67
+ def standardize_issn8(issn)
68
+ with_checksum('977' + issn[0..-2] + '00')
69
+ end
70
+
71
+ def standardize_isbn10(isbn)
72
+ with_checksum('978' + isbn[0..-2])
73
+ end
74
+
75
+ def with_checksum(unchecked)
76
+ unchecked + compute_checksum_gtin(unchecked)
77
+ end
78
+
79
+ def valid_checksum?(id_type, identifier)
80
+ converter = get_converter(id_type, identifier.length)
81
+ expected_checksum = send(converter.checksum_computer, identifier[0..-2])
82
+ identifier[-1] == expected_checksum
83
+ end
84
+
85
+ def fail_on_invalid_checksum(id_type, identifier)
86
+ converter = get_converter(id_type, identifier.length)
87
+ expected_checksum = send(converter.checksum_computer, identifier[0..-2])
88
+ actual_checksum = identifier[-1]
89
+ return if actual_checksum == expected_checksum
90
+
91
+ raise GtinValidationError.new("#{id_type} #{identifier} has invalid checksum #{actual_checksum} -- expected #{expected_checksum}")
92
+ end
93
+
94
+ def compute_checksum_gtin(unchecked)
95
+ reversed_digits = unchecked.reverse.chars.map(&:to_i)
96
+ sum = reversed_digits.each_with_index.sum { |digit, idx| digit * (idx.odd? ? 1 : 3) }
97
+ ((10 - sum) % 10).to_s
98
+ end
99
+
100
+ def compute_checksum_isbn10(unchecked_isbn)
101
+ digits = unchecked_isbn.chars.map(&:to_i)
102
+ sum = digits.each_with_index.sum { |digit, idx| digit * (idx + 1) }
103
+ (sum % 11).to_s
104
+ end
105
+
106
+ def compute_checksum_issn8(unchecked_issn)
107
+ reversed_digits = unchecked_issn.reverse.chars.map(&:to_i)
108
+ sum = reversed_digits.each_with_index.sum { |digit, idx| digit * (idx + 2) }
109
+ ((11 - sum) % 11).to_s
110
+ end
111
+
112
+ # can't be done directly; convert to upc-a first
113
+ def compute_checksum_upce(unchecked_upce)
114
+ standardize('UPC', unchecked_upce + '0', validate_checksum: false)[-1]
115
+ end
116
+ end
@@ -0,0 +1,3 @@
1
+ module GTIN
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: salsify-gtin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Salsify, Inc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: overcommit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: salsify_rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.52.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.52.1
97
+ description: Validates and converts GTIN variants to standardized GTIN-14 representation
98
+ email:
99
+ - engineering@salsify.com
100
+ executables:
101
+ - console
102
+ - setup
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".init"
108
+ - ".overcommit.yml"
109
+ - ".rspec"
110
+ - ".rubocop.yml"
111
+ - ".travis.yml"
112
+ - CHANGELOG.md
113
+ - Gemfile
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - bin/console
118
+ - bin/setup
119
+ - gtin.gemspec
120
+ - lib/gtin.rb
121
+ - lib/gtin/gtin.rb
122
+ - lib/gtin/version.rb
123
+ homepage: https://github.com/salsify/gtin
124
+ licenses:
125
+ - MIT
126
+ metadata:
127
+ allowed_push_host: https://rubygems.org
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubygems_version: 3.0.2
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Validates and converts GTIN variants to standardized GTIN-14 representation
147
+ test_files: []