jancode 0.0.1
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 +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +17 -0
- data/.travis.yml +10 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +90 -0
- data/Rakefile +9 -0
- data/jancode.gemspec +24 -0
- data/lib/jancode.rb +4 -0
- data/lib/jancode/gtin.rb +38 -0
- data/lib/jancode/gtin13.rb +7 -0
- data/lib/jancode/gtin8.rb +7 -0
- data/lib/jancode/version.rb +3 -0
- data/spec/jancode/gtin13_spec.rb +16 -0
- data/spec/jancode/gtin8_spec.rb +16 -0
- data/spec/jancode/gtin_spec.rb +87 -0
- data/spec/spec_helper.rb +19 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd604de64f4448051cdb6769f25a3edc437bccaf
|
4
|
+
data.tar.gz: 29d2859c63ca020a36b8536a073698382fd674f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d19bb7fff293e0a8c4ab6144f150a48204746ff0cc34c26ce32dc2fbdedcbd6a0cb227d60f375fe40e4e4fe6be252107aa65333aad111fe76dd195587b135dc
|
7
|
+
data.tar.gz: 47eb9cb61f30a8ad52af8b0a0e37482dfb6bb9724079b854edb4c1c809a4cd1aac750e352cbab790b605bca1230f6496fd8331f1bc59492968c878478078db05
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
repo_token: GLVOxmX7c87TUUYnsfPdh1JcvxwjSlFjP
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 i2bskn
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Jancode
|
2
|
+
|
3
|
+
[](https://travis-ci.org/i2bskn/jancode)
|
4
|
+
[](https://coveralls.io/r/i2bskn/jancode?branch=master)
|
5
|
+
[](https://codeclimate.com/github/i2bskn/jancode)
|
6
|
+
|
7
|
+
Japanese Article Number
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'jancode'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install jancode
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### GTIN13
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require "jancode"
|
29
|
+
|
30
|
+
# Jancode::GTIN13.new("GS1 Company Prefix", "Item Code")
|
31
|
+
jancode = Jancode::GTIN13.new("456995111", "617")
|
32
|
+
jancode.check_digit # => 9
|
33
|
+
jancode.create # => "4569951116179"
|
34
|
+
|
35
|
+
# jancode.company_prefix = "456995111"
|
36
|
+
jancode.item_code = "618"
|
37
|
+
jancode.create # => "4569951116186"
|
38
|
+
|
39
|
+
(1..10).each do |item_code|
|
40
|
+
jancode.item_code = "%03d" % item_code
|
41
|
+
puts jancode.create
|
42
|
+
end
|
43
|
+
# 4569951110016
|
44
|
+
# 4569951110023
|
45
|
+
# 4569951110030
|
46
|
+
# 4569951110047
|
47
|
+
# 4569951110054
|
48
|
+
# 4569951110061
|
49
|
+
# 4569951110078
|
50
|
+
# 4569951110085
|
51
|
+
# 4569951110092
|
52
|
+
# 4569951110108
|
53
|
+
```
|
54
|
+
|
55
|
+
### GTIN8
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
require "jancode"
|
59
|
+
|
60
|
+
# Jancode::GTIN8.new("GS1 Company Prefix", "Item Code")
|
61
|
+
jancode = Jancode::GTIN8.new("491234", "5")
|
62
|
+
jancode.check_digit # => 6
|
63
|
+
jancode.create # => "49123456"
|
64
|
+
|
65
|
+
# jancode.company_prefix = "491234"
|
66
|
+
jancode.item_code = "6"
|
67
|
+
jancode.create # => "49123463"
|
68
|
+
|
69
|
+
(1..9).each do |item_code|
|
70
|
+
jancode.item_code = item_code.to_s
|
71
|
+
puts jancode.create
|
72
|
+
end
|
73
|
+
# 49123418
|
74
|
+
# 49123425
|
75
|
+
# 49123432
|
76
|
+
# 49123449
|
77
|
+
# 49123456
|
78
|
+
# 49123463
|
79
|
+
# 49123470
|
80
|
+
# 49123487
|
81
|
+
# 49123494
|
82
|
+
```
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
1. Fork it
|
87
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
88
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
89
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
90
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/jancode.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jancode/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jancode"
|
8
|
+
spec.version = Jancode::VERSION
|
9
|
+
spec.authors = ["i2bskn"]
|
10
|
+
spec.email = ["i2bskn@gmail.com"]
|
11
|
+
spec.description = %q{Japanese Article Number}
|
12
|
+
spec.summary = %q{Japanese Article Number}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/jancode.rb
ADDED
data/lib/jancode/gtin.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Jancode
|
2
|
+
class GTIN
|
3
|
+
attr_accessor :company_prefix, :item_code
|
4
|
+
|
5
|
+
def initialize(company_prefix = nil, item_code = nil)
|
6
|
+
@company_prefix = company_prefix
|
7
|
+
@item_code = item_code
|
8
|
+
end
|
9
|
+
|
10
|
+
def check_digit
|
11
|
+
validation
|
12
|
+
val = ((sum_numbers(:even) * 3) + sum_numbers(:odd)).to_s[-1].to_i
|
13
|
+
val == 0 ? 0 : 10 - val
|
14
|
+
end
|
15
|
+
|
16
|
+
def create
|
17
|
+
[@company_prefix, @item_code, check_digit].join
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def sum_numbers(type)
|
22
|
+
case type
|
23
|
+
when :even then start = 0
|
24
|
+
when :odd then start = 1
|
25
|
+
else
|
26
|
+
raise "Unknown type: #{type}"
|
27
|
+
end
|
28
|
+
|
29
|
+
code = code_array.reverse
|
30
|
+
(start..(code.size - 1)).step(2).inject(0){|i,n| i += code[n].to_i}
|
31
|
+
end
|
32
|
+
|
33
|
+
def code_array
|
34
|
+
# [@company_prefix, @item_code].join.chars
|
35
|
+
[@company_prefix, @item_code].join.each_char.to_a
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Jancode::GTIN13 do
|
4
|
+
let(:jancode) {Jancode::GTIN13.new("456995111", "617")}
|
5
|
+
|
6
|
+
it "create GTIN13 code" do
|
7
|
+
expect(jancode.create).to eq("4569951116179")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should generate exception if not 12 characters" do
|
11
|
+
jancode.company_prefix = "451"
|
12
|
+
expect{
|
13
|
+
jancode.create
|
14
|
+
}.to raise_error
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Jancode::GTIN8 do
|
4
|
+
let(:jancode) {Jancode::GTIN8.new("491234", "5")}
|
5
|
+
|
6
|
+
it "create GTIN8 code" do
|
7
|
+
expect(jancode.create).to eq("49123456")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should generate exception if not 7 characters" do
|
11
|
+
jancode.company_prefix = "491"
|
12
|
+
expect{
|
13
|
+
jancode.create
|
14
|
+
}.to raise_error
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Jancode::GTIN do
|
4
|
+
let(:gtin13) {Jancode::GTIN.new("456995111", "617")}
|
5
|
+
let(:gtin8) {Jancode::GTIN8.new("491234", "5")}
|
6
|
+
|
7
|
+
describe "#initialize" do
|
8
|
+
context "default" do
|
9
|
+
let(:gtin) {Jancode::GTIN.new}
|
10
|
+
|
11
|
+
it "company_prefix should be a nil" do
|
12
|
+
expect(gtin.company_prefix).to be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "item_code should be a nil" do
|
16
|
+
expect(gtin.item_code).to be_nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "with arguments" do
|
21
|
+
it "company_prefix should be a specified value" do
|
22
|
+
expect(gtin13.company_prefix).to eq("456995111")
|
23
|
+
expect(gtin8.company_prefix).to eq("491234")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "item_code should be a nil" do
|
27
|
+
expect(gtin13.item_code).to eq("617")
|
28
|
+
expect(gtin8.item_code).to eq("5")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#check_digit" do
|
34
|
+
before {Jancode::GTIN.any_instance.stub(:validation)}
|
35
|
+
|
36
|
+
it "returns check digit" do
|
37
|
+
expect(gtin13.check_digit).to eq(9)
|
38
|
+
expect(gtin8.check_digit).to eq(6)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should call #validation" do
|
42
|
+
Jancode::GTIN.any_instance.should_receive(:validation)
|
43
|
+
gtin13.check_digit
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#create" do
|
48
|
+
before {Jancode::GTIN.any_instance.stub(:validation)}
|
49
|
+
|
50
|
+
it "returns jancode" do
|
51
|
+
expect(gtin13.create).to eq("4569951116179")
|
52
|
+
expect(gtin8.create).to eq("49123456")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#sum_numbers" do
|
57
|
+
it "returns total even number" do
|
58
|
+
expect(gtin13.send(:sum_numbers,:even)).to eq(33)
|
59
|
+
expect(gtin8.send(:sum_numbers,:even)).to eq(13)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "returns total odd number" do
|
63
|
+
expect(gtin13.send(:sum_numbers, :odd)).to eq(22)
|
64
|
+
expect(gtin8.send(:sum_numbers, :odd)).to eq(15)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should generate exception if unknown type" do
|
68
|
+
expect {
|
69
|
+
gtin13.send(:sum_numbers, :unknown)
|
70
|
+
}.to raise_error
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should call #code_array" do
|
74
|
+
Jancode::GTIN.any_instance.should_receive(:code_array).and_return(["4", "9", "1", "2", "3", "4", "5"])
|
75
|
+
gtin8.send(:sum_numbers, :even)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#code_array" do
|
80
|
+
it "returns character array" do
|
81
|
+
expected1 = ["4", "5", "6", "9", "9", "5", "1", "1", "1", "6", "1", "7"]
|
82
|
+
expected2 = ["4", "9", "1", "2", "3", "4", "5"]
|
83
|
+
expect(gtin13.send(:code_array)).to eq(expected1)
|
84
|
+
expect(gtin8.send(:code_array)).to eq(expected2)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "simplecov"
|
2
|
+
require "coveralls"
|
3
|
+
Coveralls.wear!
|
4
|
+
|
5
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
6
|
+
SimpleCov::Formatter::HTMLFormatter,
|
7
|
+
Coveralls::SimpleCov::Formatter
|
8
|
+
]
|
9
|
+
|
10
|
+
SimpleCov.start do
|
11
|
+
add_filter "spec"
|
12
|
+
add_filter ".bundle"
|
13
|
+
end
|
14
|
+
|
15
|
+
require "jancode"
|
16
|
+
|
17
|
+
RSpec.configure do |config|
|
18
|
+
config.order = "random"
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jancode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- i2bskn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-11 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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: rspec
|
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
|
+
description: Japanese Article Number
|
56
|
+
email:
|
57
|
+
- i2bskn@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .coveralls.yml
|
63
|
+
- .gitignore
|
64
|
+
- .travis.yml
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- jancode.gemspec
|
70
|
+
- lib/jancode.rb
|
71
|
+
- lib/jancode/gtin.rb
|
72
|
+
- lib/jancode/gtin13.rb
|
73
|
+
- lib/jancode/gtin8.rb
|
74
|
+
- lib/jancode/version.rb
|
75
|
+
- spec/jancode/gtin13_spec.rb
|
76
|
+
- spec/jancode/gtin8_spec.rb
|
77
|
+
- spec/jancode/gtin_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
homepage: ''
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.0.6
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Japanese Article Number
|
103
|
+
test_files:
|
104
|
+
- spec/jancode/gtin13_spec.rb
|
105
|
+
- spec/jancode/gtin8_spec.rb
|
106
|
+
- spec/jancode/gtin_spec.rb
|
107
|
+
- spec/spec_helper.rb
|