jan 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 27d8461aa61bc71f62faacef846e292e94f62b57
4
- data.tar.gz: 96ad5d0bae7837790a0dd9e2e97c19ee05abad5f
2
+ SHA256:
3
+ metadata.gz: b55db866341d1d1e57aea0a7de67a02f276be1390d825a5807038031a2c306e3
4
+ data.tar.gz: 545ee260f625ceda5ba1f9cb2c143affaab7ea7c87b81f7da040920085b41b8a
5
5
  SHA512:
6
- metadata.gz: 26ea0e512c0639e59b6c4ed09455baf7fbef3c9035cd50dc528a19752ee47e9c868e8cc91f4dc805585347cb137802acb4b78c08cb376f6a2d6a1dc1eeec10f4
7
- data.tar.gz: 83aecac3b86704af0403090b161303a79cfa7cbe87cab14ef91410f44f42e1e7a1e0d5353c0cc1a0c758645f35aa90ab096a811cf3f1514dd0bf273d5c164478
6
+ metadata.gz: af8c1e536e522fa7b59d01c5177dd80b725f4e12dd4a9aa033a794d8d65cdc19c6e1a96a4e1d6d2d1cc0c653a7597189e947e1f3554ceb273bed7b028bfac967
7
+ data.tar.gz: 3decd703b98afe0855a13e5911484ba5ddb2e8b28c3be13b2993236bf2a248d83bd82a734c133d3c4e6e0bea14ba57969e0f22c48ebf8df4b971cbef8a34c518
@@ -1,5 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.2
4
- - 2.0.0
5
- - 1.9.3
3
+ - 2.6.1
4
+ - 2.5.3
data/README.md CHANGED
@@ -3,8 +3,13 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/jan.svg)](http://badge.fury.io/rb/jan)
4
4
  [![Build Status](https://travis-ci.org/s-osa/jan.svg?branch=master)](https://travis-ci.org/s-osa/jan)
5
5
 
6
- A small utility for JAN code.
6
+ [jan](https://rubygems.org/gems/jan) is a small utility gem for JAN code.
7
7
 
8
+ ## Supported versions
9
+
10
+ - Ruby
11
+ - 2.6.x
12
+ - 2.5.x
8
13
 
9
14
  ## Installation
10
15
 
@@ -26,63 +31,44 @@ Or install it yourself as:
26
31
  $ gem install jan
27
32
  ```
28
33
 
29
-
30
34
  ## Usage
31
35
 
32
- ### Basic usage
33
-
34
- ```ruby
35
- jan = Jan.new("4901277241126")
36
- jan.valid? # => true
37
-
38
- Jan::Validator.validate("4901277241126") # => true
39
- ```
40
-
41
- ### Calculate check digit
36
+ ### Basic
42
37
 
43
38
  ```ruby
44
- # Correct code: "4901277241126"
45
-
46
- Jan::CheckDigitCalculator.calculate("490127724112") # => 6
47
- ```
39
+ code = Jan::Code.new('4901277241126')
48
40
 
41
+ code.valid? # => true
49
42
 
50
- ### Get elements
51
-
52
- ```ruby
53
- jan = Jan.new("4901277241126")
54
- jan.check_digit # => 6
55
- jan.body # => "490127724112"
43
+ code.body # => '490127724112'
44
+ code.check_digit # => '6'
56
45
 
57
- Jan::Parser.check_digit("4901277241126") # => 6
58
- Jan::Parser.body("4901277241126") # => "490127724112"
46
+ code.instore_code? # => false
59
47
  ```
60
48
 
61
- ### Distinct in-store code
49
+ ### Calculate check digit
62
50
 
63
51
  ```ruby
64
- jan = Jan.new("4901277241126")
65
- jan.instore_code? # => false
52
+ body = Jan::CodeBody.new('490127724112')
66
53
 
67
- Jan::Paser.instore_code?("2163179230340") # => true
54
+ body.calculate_check_digit # => '6'
55
+ body.generage_code # => '4901277241126'
68
56
  ```
69
57
 
70
58
  ### Generate random code
71
59
 
72
60
  ```ruby
73
- Jan::Random.code # => "5689450935688"
74
- Jan::Random.code(8) # => "11774853"
75
-
76
- Jan::Random.instore_code # => "2799375754394"
77
- Jan::Random.instore_code(8) # => "27393086"
61
+ random = Jan::Random.new
78
62
 
63
+ random.code # => '5689450935688'
64
+ random.instore_code # => '2799375754394'
79
65
  ```
80
66
 
81
-
82
67
  ## Contributing
83
68
 
84
- 1. Fork it ( https://github.com/[my-github-username]/jan/fork )
85
- 2. Create your feature branch (`git checkout -b my-new-feature`)
86
- 3. Commit your changes (`git commit -am 'Add some feature'`)
87
- 4. Push to the branch (`git push origin my-new-feature`)
88
- 5. Create a new Pull Request
69
+ Bug reports and pull requests are welcome.
70
+
71
+ - Install dependencies
72
+ - `bundle install`
73
+ - Run tests
74
+ - `rake spec`
@@ -1,26 +1,23 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'jan/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "jan"
6
+ spec.name = 'jan'
8
7
  spec.version = Jan::VERSION
9
- spec.authors = ["OSA Shunsuke"]
10
- spec.email = ["hhelibebcnofnenamg@gmail.com"]
11
- spec.summary = %q{JAN utility gem}
12
- spec.description = %q{JAN utility gem}
13
- spec.homepage = "https://github.com/s-osa/jan"
14
- spec.license = "MIT"
8
+ spec.authors = ['OSA Shunsuke']
9
+ spec.email = ['hhelibebcnofnenamg@gmail.com']
10
+ spec.summary = %q{Simple gem for JAN code}
11
+ spec.description = %q{Validate and generate JAN code}
12
+ spec.homepage = 'https://github.com/s-osa/jan'
13
+ spec.license = 'MIT'
15
14
 
16
15
  spec.files = `git ls-files -z`.split("\x0")
17
16
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
18
+ spec.require_paths = ['lib']
20
19
 
21
- spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec"
24
- spec.add_development_dependency "guard"
25
- spec.add_development_dependency "guard-rspec"
20
+ spec.add_development_dependency 'bundler', '~> 2.0'
21
+ spec.add_development_dependency 'rake', '~> 12.3'
22
+ spec.add_development_dependency 'rspec', '~> 3.8'
26
23
  end
data/lib/jan.rb CHANGED
@@ -1,37 +1,7 @@
1
- require "jan/check_digit_calculator"
2
- require "jan/parser"
3
- require "jan/random"
4
- require "jan/validator"
5
- require "jan/version"
1
+ require 'jan/code'
2
+ require 'jan/random'
3
+ require 'jan/version'
6
4
 
7
- class Jan < ::String
8
- InstoreCodePrefixes = %w(02 20 21 22 23 24 25 26 27 28 29)
9
-
10
- def initialize(code)
11
- super(code.to_s)
12
- end
13
-
14
- def valid?
15
- Validator.validate(self)
16
- end
17
-
18
- def check_digit
19
- Parser.check_digit(self)
20
- end
21
-
22
- def body
23
- Parser.body(self)
24
- end
25
-
26
- def even_digits
27
- Parser.even_digits(self)
28
- end
29
-
30
- def odd_digits
31
- Parser.odd_digits(self)
32
- end
33
-
34
- def instore_code?
35
- Parser.instore_code?(self)
36
- end
5
+ module Jan
6
+ class FormatError < StandardError ; end
37
7
  end
@@ -0,0 +1,14 @@
1
+ module Jan
2
+ class CheckDigit < String
3
+ # @param check_digit [String]
4
+ def initialize(check_digit)
5
+ @check_digit = check_digit
6
+ super(check_digit)
7
+ end
8
+
9
+ # @return [boolean]
10
+ def valid?
11
+ @check_digit.match?(/\A\d\z/)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ require 'jan/code_body'
2
+ require 'jan/check_digit'
3
+
4
+ module Jan
5
+ class Code < String
6
+ # @param code [String]
7
+ def initialize(code)
8
+ @body = Jan::CodeBody.new(code[0..-2])
9
+ @check_digit = Jan::CheckDigit.new(code[-1])
10
+ super(code)
11
+ end
12
+
13
+ attr_reader :body, :check_digit
14
+
15
+ # @return [boolean]
16
+ def valid?
17
+ @body.valid? && @check_digit.valid? && @body.calculate_check_digit == @check_digit
18
+ end
19
+
20
+ # @return [boolean]
21
+ def instore_code?
22
+ @body.start_with?('2')
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ require 'jan/code'
2
+
3
+ module Jan
4
+ class CodeBody < String
5
+ # @param code_body [String]
6
+ def initialize(code_body)
7
+ @code_body = code_body
8
+ super(code_body)
9
+ end
10
+
11
+ # @return [boolean]
12
+ def valid?
13
+ @code_body.match?(/\A\d{12}\z/)
14
+ end
15
+
16
+ # @return [Jan::CheckDigit]
17
+ def calculate_check_digit
18
+ # http://www.dsri.jp/jan/check_digit.html
19
+ even_position_digits, odd_position_digits = @code_body.reverse.each_char.partition.with_index(2) {|_chr, idx| idx.even? }
20
+ sum = even_position_digits.map{|d| Integer(d) }.sum * 3 + odd_position_digits.map{|d| Integer(d) }.sum
21
+ digit = (10 - sum % 10).to_s[-1]
22
+ Jan::CheckDigit.new(digit)
23
+ end
24
+
25
+ # @return [Jan::Code]
26
+ def generate_code
27
+ Jan::Code.new(@code_body + calculate_check_digit)
28
+ end
29
+ end
30
+ end
@@ -1,23 +1,20 @@
1
- require "jan/parser"
2
- require "jan/check_digit_calculator"
3
-
4
- class Jan < ::String
5
- module Random
6
- module_function
7
-
8
- Digits = %w(1 2 3 4 5 6 7 8 9 0)
9
-
10
- def code(size=13)
11
- build(size)
1
+ module Jan
2
+ class Random
3
+ # @param generator [Random] Random-like object which respond to #rand(max)
4
+ def initialize(generator: ::Random.new)
5
+ @generator = generator
12
6
  end
13
7
 
14
- def instore_code(size=13)
15
- build(size - 2, InstoreCodePrefixes.sample)
8
+ # @return [Jan::Code]
9
+ def code
10
+ twelve_digits_str = sprintf('%012d', @generator.rand(1_000_000_000_000))
11
+ Jan::CodeBody.new(twelve_digits_str).generate_code
16
12
  end
17
13
 
18
- def build(size, code="")
19
- size.times{ code += Digits.sample }
20
- Parser.body(code) + CheckDigitCalculator.calculate(Parser.body(code)).to_s
14
+ # @return [Jan::Code]
15
+ def instore_code
16
+ twelve_digits_str = '2' + sprintf('%011d', @generator.rand(100_000_000_000))
17
+ Jan::CodeBody.new(twelve_digits_str).generate_code
21
18
  end
22
19
  end
23
20
  end
@@ -1,3 +1,3 @@
1
- class Jan < ::String
2
- VERSION = "0.1.0"
1
+ module Jan
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jan::CodeBody do
4
+ describe '#calculate_check_digit' do
5
+ it('generates and returns check digit') do
6
+ res = described_class.new('490127724112').calculate_check_digit
7
+ expect(res.class).to eq(Jan::CheckDigit)
8
+ expect(res).to eq('6')
9
+
10
+ end
11
+ end
12
+
13
+ describe '#generate_code' do
14
+ it('generates and returns check digit') do
15
+ res = described_class.new('490127724112').generate_code
16
+ expect(res.class).to eq(Jan::Code)
17
+ expect(res).to eq('4901277241126')
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jan::Code do
4
+ describe '#valid?' do
5
+ context 'valid codes' do
6
+ valid_codes = %w[
7
+ 0000000000000
8
+ 4901277241126
9
+ 2000000000015
10
+ ]
11
+
12
+ valid_codes.each do |valid_code|
13
+ it("#{valid_code} is valid"){ expect(described_class.new(valid_code)).to be_valid }
14
+ end
15
+ end
16
+
17
+ context 'invalid codes' do
18
+ invalid_codes = [
19
+ '11',
20
+ '123abc',
21
+ '4901277 241126',
22
+ '4901277 24112',
23
+ ]
24
+
25
+ invalid_codes.each do |invalid_code|
26
+ it("#{invalid_code} is invalid"){ expect(described_class.new(invalid_code)).not_to be_valid }
27
+ end
28
+ end
29
+ end
30
+
31
+ describe '#body' do
32
+ it('returns body digits'){ expect(described_class.new('4901277241126').body).to eq('490127724112') }
33
+ end
34
+
35
+ describe '#check_digit' do
36
+ it('returns check digit'){ expect(described_class.new('4901277241126').check_digit).to eq('6') }
37
+ end
38
+
39
+ describe '#instore_code?' do
40
+ context 'instore code' do
41
+ it('is true'){ expect(described_class.new('2101085089347')).to be_instore_code }
42
+ end
43
+
44
+ context 'global code' do
45
+ it('is false'){ expect(described_class.new('4901277241126')).not_to be_instore_code }
46
+ end
47
+ end
48
+ end
@@ -1,41 +1,22 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  describe Jan::Random do
4
- describe ".code(size)" do
5
- (6..20).each do |size|
6
- it "should return code with valid check digit" do
7
- code = Jan::Random.code(size)
8
- check_digit = Jan::Parser.check_digit(code)
9
- actual = Jan::CheckDigitCalculator.calculate(Jan::Parser.body(code))
10
- expect(actual).to eq(check_digit)
11
- end
4
+ let(:random) { described_class.new }
12
5
 
13
- it "should return given length valid code" do
14
- code = Jan::Random.code(size)
15
- expect(code.size).to eq(size)
16
- end
6
+ describe '#code' do
7
+ it 'returns code' do
8
+ code = random.code
9
+ expect(code).to be_an_instance_of(Jan::Code)
10
+ expect(code).to be_valid
17
11
  end
18
12
  end
19
13
 
20
- describe ".instore_code(size)" do
21
- (6..20).each do |size|
22
- it "should return code with valid check digit" do
23
- code = Jan::Random.instore_code(size)
24
- check_digit = Jan::Parser.check_digit(code)
25
- actual = Jan::CheckDigitCalculator.calculate(Jan::Parser.body(code))
26
- expect(actual).to eq(check_digit)
27
- end
28
-
29
- it "should return instore code" do
30
- code = Jan::Random.instore_code(size)
31
- actual = Jan::Parser.instore_code?(code)
32
- expect(actual).to be true
33
- end
34
-
35
- it "should return given length valid code" do
36
- code = Jan::Random.instore_code(size)
37
- expect(code.size).to eq(size)
38
- end
14
+ describe '#instore_code' do
15
+ it 'returns in-store code' do
16
+ code = random.instore_code
17
+ expect(code).to be_an_instance_of(Jan::Code)
18
+ expect(code).to be_valid
19
+ expect(code).to be_instore_code
39
20
  end
40
21
  end
41
22
  end
@@ -1,6 +1,3 @@
1
- project_root = File.join(File.dirname(__FILE__), '..')
2
- $: << project_root
3
-
4
- Dir[File.join(File.dirname(__FILE__), "support", "*")].each {|f| require f }
1
+ $: << File.join(File.dirname(__FILE__), '..')
5
2
 
6
3
  require 'lib/jan'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OSA Shunsuke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-23 00:00:00.000000000 Z
11
+ date: 2019-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,71 +16,43 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.6'
19
+ version: '2.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.6'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '12.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '12.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
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: guard
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: guard-rspec
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
45
+ - - "~>"
74
46
  - !ruby/object:Gem::Version
75
- version: '0'
47
+ version: '3.8'
76
48
  type: :development
77
49
  prerelease: false
78
50
  version_requirements: !ruby/object:Gem::Requirement
79
51
  requirements:
80
- - - ">="
52
+ - - "~>"
81
53
  - !ruby/object:Gem::Version
82
- version: '0'
83
- description: JAN utility gem
54
+ version: '3.8'
55
+ description: Validate and generate JAN code
84
56
  email:
85
57
  - hhelibebcnofnenamg@gmail.com
86
58
  executables: []
@@ -90,25 +62,20 @@ files:
90
62
  - ".gitignore"
91
63
  - ".travis.yml"
92
64
  - Gemfile
93
- - Guardfile
94
65
  - LICENSE
95
66
  - README.md
96
67
  - Rakefile
97
68
  - jan.gemspec
98
69
  - lib/jan.rb
99
- - lib/jan/check_digit_calculator.rb
100
- - lib/jan/parser.rb
70
+ - lib/jan/check_digit.rb
71
+ - lib/jan/code.rb
72
+ - lib/jan/code_body.rb
101
73
  - lib/jan/random.rb
102
- - lib/jan/validator.rb
103
74
  - lib/jan/version.rb
104
- - spec/jan/check_digit_calculator_spec.rb
105
- - spec/jan/parser_spec.rb
75
+ - spec/jan/code_body_spec.rb
76
+ - spec/jan/code_spec.rb
106
77
  - spec/jan/random_spec.rb
107
- - spec/jan/validator_spec.rb
108
- - spec/jan_spec.rb
109
78
  - spec/spec_helper.rb
110
- - spec/support/invalid_codes.rb
111
- - spec/support/valid_codes.rb
112
79
  homepage: https://github.com/s-osa/jan
113
80
  licenses:
114
81
  - MIT
@@ -128,17 +95,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
95
  - !ruby/object:Gem::Version
129
96
  version: '0'
130
97
  requirements: []
131
- rubyforge_project:
132
- rubygems_version: 2.2.2
98
+ rubygems_version: 3.0.1
133
99
  signing_key:
134
100
  specification_version: 4
135
- summary: JAN utility gem
101
+ summary: Simple gem for JAN code
136
102
  test_files:
137
- - spec/jan/check_digit_calculator_spec.rb
138
- - spec/jan/parser_spec.rb
103
+ - spec/jan/code_body_spec.rb
104
+ - spec/jan/code_spec.rb
139
105
  - spec/jan/random_spec.rb
140
- - spec/jan/validator_spec.rb
141
- - spec/jan_spec.rb
142
106
  - spec/spec_helper.rb
143
- - spec/support/invalid_codes.rb
144
- - spec/support/valid_codes.rb
data/Guardfile DELETED
@@ -1,6 +0,0 @@
1
- guard :rspec do
2
- watch(%r{^spec/.+_spec\.rb$})
3
- watch('spec/spec_helper.rb') { "spec" }
4
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
5
- end
6
-
@@ -1,13 +0,0 @@
1
- require "jan/parser"
2
-
3
- class Jan < ::String
4
- module CheckDigitCalculator
5
- module_function
6
-
7
- def calculate(body)
8
- code = body.to_s + "x"
9
- digit = 10 - (Parser.even_digits(code).reduce(&:+) * 3 + Parser.odd_digits(code).reduce(&:+)) % 10
10
- digit % 10
11
- end
12
- end
13
- end
@@ -1,25 +0,0 @@
1
- class Jan < ::String
2
- module Parser
3
- module_function
4
-
5
- def check_digit(code)
6
- code[-1].to_i
7
- end
8
-
9
- def body(code)
10
- code[0..-2]
11
- end
12
-
13
- def even_digits(code)
14
- code.split('').reverse.map(&:to_i).select.with_index(1){|_d, index| index.even? }.reverse
15
- end
16
-
17
- def odd_digits(code)
18
- code.split('').reverse.map(&:to_i).select.with_index(1){|_d, index| index.odd? }.reverse[0..-2]
19
- end
20
-
21
- def instore_code?(code)
22
- InstoreCodePrefixes.include?(code[0..1])
23
- end
24
- end
25
- end
@@ -1,20 +0,0 @@
1
- require "jan/parser"
2
- require "jan/check_digit_calculator"
3
-
4
- class Jan < ::String
5
- module Validator
6
- module_function
7
-
8
- def validate(code)
9
- validate_size(code.to_s) && validate_check_digit(code.to_s)
10
- end
11
-
12
- def validate_size(code)
13
- [8,13].include?(code.to_s.size)
14
- end
15
-
16
- def validate_check_digit(code)
17
- Parser.check_digit(code.to_s) == CheckDigitCalculator.calculate(Parser.body(code.to_s))
18
- end
19
- end
20
- end
@@ -1,15 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Jan::CheckDigitCalculator do
4
- describe ".calculate" do
5
- it "should return check digit" do
6
- actual = Jan::CheckDigitCalculator.calculate("490127724112")
7
- expect(actual).to eq(6)
8
- end
9
-
10
- it "should return 0 as check digit" do
11
- actual = Jan::CheckDigitCalculator.calculate("490330111020")
12
- expect(actual).to eq(0)
13
- end
14
- end
15
- end
@@ -1,49 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Jan::Parser do
4
- let(:code){ "1234567890123" }
5
-
6
- describe ".check_digit(code)" do
7
- it "should return last digit" do
8
- actual = Jan::Parser.check_digit(code)
9
- expect(actual).to eq(3)
10
- end
11
- end
12
-
13
- describe ".body(code)" do
14
- it "should return string except last digit" do
15
- actual = Jan::Parser.body(code)
16
- expect(actual).to eq("123456789012")
17
- end
18
- end
19
-
20
- describe ".even_digits(code)" do
21
- it "should return digits in even number-th position" do
22
- actual = Jan::Parser.even_digits(code)
23
- expect(actual).to eq([2,4,6,8,0,2])
24
- end
25
- end
26
-
27
- describe ".odd_digits(code)" do
28
- it "should return digits in odd number-th position except check_digit" do
29
- actual = Jan::Parser.odd_digits(code)
30
- expect(actual).to eq([1,3,5,7,9,1])
31
- end
32
- end
33
-
34
- describe ".instore_code?(code)" do
35
- context "instore code" do
36
- it "should be true" do
37
- actual = Jan::Parser.instore_code?("2101085089347")
38
- expect(actual).to be true
39
- end
40
- end
41
-
42
- context "global code" do
43
- it "should be false" do
44
- actual = Jan::Parser.instore_code?("4901085089347")
45
- expect(actual).to be false
46
- end
47
- end
48
- end
49
- end
@@ -1,76 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Jan::Validator do
4
- describe ".validate" do
5
- context "valid codes" do
6
- valid_codes.each do |code|
7
- it "#{code} should be valid" do
8
- actual = Jan::Validator.validate(code)
9
- expect(actual).to be true
10
- end
11
- end
12
- end
13
-
14
- context "invalid codes" do
15
- invalid_codes.each do |code|
16
- it "#{code} should be invalid" do
17
- actual = Jan::Validator.validate(code)
18
- expect(actual).to be false
19
- end
20
- end
21
- end
22
- end
23
-
24
- describe ".validate_size" do
25
- context "valid codes" do
26
- valid_size_codes = [8, 13].map{|n| "x" * n }
27
-
28
- valid_size_codes.each do |code|
29
- it "#{code} should be valid" do
30
- actual = Jan::Validator.validate_size(code)
31
- expect(actual).to be true
32
- end
33
- end
34
- end
35
-
36
- context "invalid codes" do
37
- invalid_size_codes = [1,2,3,4,5,6,7,9,10,11,12,14,15].map{|n| "x" * n }
38
-
39
- invalid_size_codes.each do |code|
40
- it "#{code} should be invalid" do
41
- actual = Jan::Validator.validate_size(code)
42
- expect(actual).to be false
43
- end
44
- end
45
- end
46
- end
47
-
48
- describe ".validate_check_digit" do
49
- context "valid codes" do
50
- valid_check_digit_codes = (3..15).to_a.map{|n| Jan::Random.code(n) }
51
-
52
- valid_check_digit_codes.each do |code|
53
- it "#{code} should be valid" do
54
- actual = Jan::Validator.validate_check_digit(code)
55
- expect(actual).to be true
56
- end
57
- end
58
- end
59
-
60
- context "invalid codes" do
61
- invalid_check_digit_codes = []
62
- (3..15).each do |n|
63
- code = Jan::Random.code(n)
64
- code[-1] = ((Jan::Parser.check_digit(code) + 1 ) % 10).to_s
65
- invalid_check_digit_codes << code
66
- end
67
-
68
- invalid_check_digit_codes.each do |code|
69
- it "#{code} should be invalid" do
70
- actual = Jan::Validator.validate_check_digit(code)
71
- expect(actual).to be false
72
- end
73
- end
74
- end
75
- end
76
- end
@@ -1,63 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Jan do
4
- describe "#initialize" do
5
- let(:jan_code){ "4901085089347" }
6
- subject{ described_class.new(jan_code) }
7
- it{ is_expected.to be_an_instance_of(described_class) }
8
- it{ is_expected.to be_a(String) }
9
- it("should equals to given string"){ is_expected.to eq(jan_code) }
10
- end
11
-
12
- describe "#valid?" do
13
- context "valid codes" do
14
- valid_codes.each do |valid_code|
15
- subject{ described_class.new(valid_code) }
16
- it("#{valid_code} should be valid"){ is_expected.to be_valid }
17
- end
18
- end
19
-
20
- context "invalid codes" do
21
- invalid_codes.each do |invalid_code|
22
- subject{ described_class.new(invalid_code) }
23
- it("#{invalid_code} should be invalid"){ is_expected.not_to be_valid }
24
- end
25
- end
26
- end
27
-
28
- describe "#check_digit" do
29
- let(:jan){ described_class.new("4901277241126") }
30
- subject{ jan.check_digit }
31
- it("should return last digit"){ is_expected.to eq(6) }
32
- end
33
-
34
- describe "#body" do
35
- let(:jan){ described_class.new("4901277241126") }
36
- subject{ jan.body }
37
- it("should return digits without check digit"){ is_expected.to eq("490127724112") }
38
- end
39
-
40
- describe "#even_digits" do
41
- let(:jan){ described_class.new("4901277241126") }
42
- subject{ jan.even_digits }
43
- it("should return digits in even number-th position"){ is_expected.to eq([9,1,7,2,1,2]) }
44
- end
45
-
46
- describe "#odd_digits" do
47
- let(:jan){ described_class.new("4901277241126") }
48
- subject{ jan.odd_digits }
49
- it("should return digits in odd number-th position except check digit"){ is_expected.to eq([4,0,2,7,4,1]) }
50
- end
51
-
52
- describe "#instore_code?" do
53
- context "instore code" do
54
- subject{ described_class.new("2101085089347") }
55
- it("should be true"){ is_expected.to be_instore_code }
56
- end
57
-
58
- context "global code" do
59
- subject{ described_class.new("4901085089347") }
60
- it("should be false"){ is_expected.not_to be_instore_code }
61
- end
62
- end
63
- end
@@ -1,8 +0,0 @@
1
- def invalid_codes
2
- [
3
- "11",
4
- "123abc",
5
- "4901277 241126",
6
- "4901277 24112"
7
- ]
8
- end
@@ -1,9 +0,0 @@
1
- def valid_codes
2
- [
3
- "0000000000000",
4
- "4901277241126",
5
- 4901277241126,
6
- "49123456",
7
- 49123456
8
- ]
9
- end