czech-post-code-generator 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 35346e06426a724537e9049903e94f172980b09b
4
+ data.tar.gz: a5f0bb65ddf4b096f07c31d16e5454af06cb10a6
5
+ SHA512:
6
+ metadata.gz: bb6270389143706cf70a4d42e1db5cb4be9436a2e8c0298392d50e856b7d42b44e18f99efd55089deb77e6b06bf9a5eef8379eeefd2f5940a7c0fdd0beaa20e8
7
+ data.tar.gz: 851d1d5fce7cd9d65fc8bd99aaeb12f2626ae4342d2d7f134fe07c56149918ce15f141129f961d83f451a4c11c45d7e8e9c2ef3155103e868352d176bd6810f7
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,12 @@
1
+ AllCops:
2
+ DisplayStyleGuide: true
3
+ Include:
4
+ - '**/Gemfile'
5
+ - '**/Rakefile'
6
+ Exclude:
7
+ - 'bin/*'
8
+
9
+ Metrics/LineLength:
10
+ Max: 180
11
+ Documentation:
12
+ Enabled: false
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at a.martinik@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Adam Martinik
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.
@@ -0,0 +1,70 @@
1
+ # Czech post CodeGenerator
2
+
3
+ [![Build Status](https://travis-ci.org/14113/czech-post-code-generator.svg?branch=master)](https://travis-ci.org/14113/czech-post-code-generator)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```Ruby
10
+ gem 'czech-post-code-generator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```Ruby
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```Ruby
22
+ $ gem install czech-post-code-generator
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```Ruby
28
+ service_id = 'DR'
29
+ order_id = 202
30
+ client_id = 7038
31
+ client_type = 'C'
32
+
33
+ serial_number = CzechPost::CodeGenerator::SerialNumber.new(service_id, order_id, client_id, client_type).run
34
+ ```
35
+
36
+ ### If you have just one place for sending, you can alternative use initializer
37
+
38
+ ```Ruby
39
+ # config/initializers/czech_post_code_generator.rb
40
+ CzechPost::CodeGenerator.configure do |config|
41
+ config.client_id = 7039
42
+ config.client_type = 'D'
43
+ end
44
+ ```
45
+
46
+ ```Ruby
47
+ service_id = 'DR'
48
+ order_id = 202
49
+ serial_number = CzechPost::CodeGenerator::SerialNumber.new(service_id, order_id).run
50
+ ```
51
+
52
+ ## Documentation
53
+
54
+ For more details check [documentation](https://www.ceskaposta.cz/documents/10180/282457/apost_Smluvni-podavatel_uziv_dok_v1-11.pdf) from Czech post.
55
+
56
+ ## Development
57
+
58
+ 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.
59
+
60
+ 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).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/czech-post-code-generator. 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.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
70
+
@@ -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
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'czech/post/code/generator'
5
+ require 'irb'
6
+ IRB.start
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -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 'czech_post/code_generator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'czech-post-code-generator'
8
+ spec.version = CzechPost::CodeGenerator::VERSION
9
+ spec.authors = ['Adam Martinik']
10
+ spec.email = ['a.martinik@gmail.com']
11
+
12
+ spec.summary = 'Code generator for Czech post'
13
+ spec.homepage = 'https://github.com/14113/czech-post-code-generator'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.0.0'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ end
@@ -0,0 +1,12 @@
1
+ module CzechPost
2
+ module CodeGenerator
3
+ def self.configuration
4
+ @configuration ||= CzechPost::CodeGenerator::Configuration.new
5
+ end
6
+
7
+ def self.configure
8
+ config = configuration
9
+ yield(config)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,57 @@
1
+ module CzechPost
2
+ module CodeGenerator
3
+ class CheckDigit
4
+ DIGIT = [1, 8, 6, 4, 2, 3, 5, 9, 7].freeze # 186_423_597
5
+ SIZE = 8
6
+
7
+ attr_reader :order, :client_id
8
+
9
+ def initialize(order, client_id)
10
+ @order = order
11
+ @client_id = client_id
12
+ end
13
+
14
+ def run
15
+ if modulo > 1
16
+ 11 - modulo
17
+ elsif modulo == 0
18
+ 5
19
+ elsif modulo == 1
20
+ 0
21
+ end
22
+ end
23
+
24
+ def valid?
25
+ size == SIZE
26
+ end
27
+
28
+ private
29
+
30
+ def get_number_by_index(index)
31
+ full_number.to_s[index].to_i
32
+ end
33
+
34
+ def times_digit
35
+ (0..SIZE).map do |index|
36
+ DIGIT[index].to_i * get_number_by_index(index)
37
+ end
38
+ end
39
+
40
+ def full_number
41
+ "#{client_id}#{order.id}"
42
+ end
43
+
44
+ def size
45
+ @size ||= full_number.size - 1
46
+ end
47
+
48
+ def sum
49
+ @sum ||= times_digit.inject { |a, e| a + e }
50
+ end
51
+
52
+ def modulo(value = 11)
53
+ @modulo ||= sum % value
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ module CzechPost
2
+ module CodeGenerator
3
+ class Configuration
4
+ attr_accessor :client_id, :client_type
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ module CzechPost
2
+ module CodeGenerator
3
+ module Errors
4
+ class InvalidOrderNumberId < StandardError
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ module CzechPost
2
+ module CodeGenerator
3
+ class OrderNumber
4
+ MAX_SIZE = 9
5
+
6
+ attr_reader :order_id, :client_id
7
+
8
+ def initialize(order_id, client_id)
9
+ @order_id = order_id.to_s
10
+ @client_id = client_id.to_s
11
+ raise(CzechPost::CodeGenerator::Errors::InvalidOrderNumberId, order_id) unless valid?
12
+ end
13
+
14
+ def id
15
+ @id ||= order_id.to_s.rjust(size, '0')
16
+ end
17
+
18
+ def valid?
19
+ return false if order_id == '' || client_id == ''
20
+ id.length == size
21
+ end
22
+
23
+ def size
24
+ MAX_SIZE - client_id.length
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,36 @@
1
+ module CzechPost
2
+ module CodeGenerator
3
+ class SerialNumber
4
+ attr_reader :service_id, :order_id
5
+
6
+ def initialize(service_id, order_id, client_id = nil, client_type = nil)
7
+ @service_id = service_id
8
+ @order_id = order_id
9
+ @client_id = client_id
10
+ @client_type = client_type
11
+ end
12
+
13
+ def run
14
+ "#{service_id}#{client_id}#{order_number.id}#{check_number}#{client_type}"
15
+ end
16
+
17
+ private
18
+
19
+ def check_number
20
+ @check_number ||= CzechPost::CodeGenerator::CheckDigit.new(order_number, client_id).run
21
+ end
22
+
23
+ def order_number
24
+ @order_number ||= CzechPost::CodeGenerator::OrderNumber.new(order_id, client_id)
25
+ end
26
+
27
+ def client_id
28
+ @client_id || CzechPost::CodeGenerator.configuration.client_id
29
+ end
30
+
31
+ def client_type
32
+ @client_type || CzechPost::CodeGenerator.configuration.client_type
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module CzechPost
2
+ module CodeGenerator
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: czech-post-code-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Martinik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description:
42
+ email:
43
+ - a.martinik@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - ".travis.yml"
52
+ - CODE_OF_CONDUCT.md
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/console
58
+ - bin/setup
59
+ - czech-post-code-generator.gemspec
60
+ - lib/czech_post/code_generator.rb
61
+ - lib/czech_post/code_generator/check_digit.rb
62
+ - lib/czech_post/code_generator/configuration.rb
63
+ - lib/czech_post/code_generator/errors/invalid_order_number_id.rb
64
+ - lib/czech_post/code_generator/order_number.rb
65
+ - lib/czech_post/code_generator/serial_number.rb
66
+ - lib/czech_post/code_generator/version.rb
67
+ homepage: https://github.com/14113/czech-post-code-generator
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.0.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.4.6
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Code generator for Czech post
91
+ test_files: []