complicode 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e8799896321a70ce1c4882a2ccd2c19c70fa36d55782e4c425c235f4929296be
4
+ data.tar.gz: 62a4064fce977a49caa7053bb155a085d9f639b3da43d112af0ecf8cf36c2689
5
+ SHA512:
6
+ metadata.gz: c009cdf95befbcb3806b4c979c7cfa05fa9bfbd67c79ce0ded4026dc8cfe977e91c3c8cf767867df840b2f4623c63b654891e043f6474bb0d21c629da8bb5cea
7
+ data.tar.gz: 86915e36ef6fae206810371d3ba681ac899e7f53c333480cf7d7160d3fe8a417c57e09124f264de0d48733ce9fa1a33d787cc8c367792b4fadd93ebb8b3b592b
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Pablo Crivella.
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Complicode
2
+
3
+ [![Gem](https://img.shields.io/gem/v/complicode.svg?style=flat)](http://rubygems.org/gems/complicode)
4
+ [![CircleCI](https://circleci.com/gh/pablocrivella/complicode.svg?style=svg)](https://circleci.com/gh/pablocrivella/complicode)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/935822c7c481aa464186/maintainability)](https://codeclimate.com/github/pablocrivella/complicode/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/935822c7c481aa464186/test_coverage)](https://codeclimate.com/github/pablocrivella/complicode/test_coverage)
7
+
8
+ Control code generator for invoices inside the Bolivian national tax service..
9
+
10
+ Links:
11
+
12
+ - [API Docs](https://www.rubydoc.info/gems/complicode)
13
+ - [Contributing](https://github.com/InspireNL/complicode/blob/master/CONTRIBUTING.md)
14
+ - [Code of Conduct](https://github.com/InspireNL/complicode/blob/master/CODE_OF_CONDUCT.md)
15
+
16
+ ## Requirements
17
+
18
+ 1. [Ruby 2.5.0](https://www.ruby-lang.org)
19
+
20
+ ## Installation
21
+
22
+ To install, run:
23
+
24
+ ```
25
+ gem install complicode
26
+ ```
27
+
28
+ Or add the following to your Gemfile:
29
+
30
+ ```
31
+ gem "complicode"
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ ```ruby
37
+ require "complicode"
38
+
39
+ authorization_code = "29040011007"
40
+ key = "9rCB7Sv4X29d)5k7N%3ab89p-3(5[A"
41
+ Complicode::Generate.call(authorization_code, key, number: "1503", nit: "4189179011", issue_date: "20070702", amount: "2500")
42
+ # => "6A-DC-53-05-14"
43
+ # If ignored, "nit" defaults to "0"
44
+ Complicode::Generate.call(authorization_code, key, number: "1503", issue_date: "20070702", amount: "2500")
45
+ # => "9E-84-73-A4"
46
+ ```
47
+
48
+ ## Tests
49
+
50
+ To test, run:
51
+
52
+ ```
53
+ bundle exec rspec spec/
54
+ ```
55
+
56
+ ## Versioning
57
+
58
+ Read [Semantic Versioning](https://semver.org) for details. Briefly, it means:
59
+
60
+ - Major (X.y.z) - Incremented for any backwards incompatible public API changes.
61
+ - Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
62
+ - Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
63
+
64
+ ## License
65
+
66
+ Copyright 2018 [Inspire Innovation BV](https://inspire.nl).
67
+ Read [LICENSE](LICENSE.md) for details.
data/lib/complicode.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "radix"
4
+ require "rc4"
5
+ require "verhoeff"
6
+ require "date"
7
+ require "complicode/invoice"
8
+ require "complicode/partial_key"
9
+ require "complicode/generators/verification_digits"
10
+ require "complicode/generators/partial_keys"
11
+ require "complicode/generators/data"
12
+ require "complicode/generators/ascii_sums"
13
+ require "complicode/generators/base64_data"
14
+ require "complicode/generate"
15
+ require "complicode/version"
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Complicode
4
+ class Generate
5
+ # @return [String]
6
+ def self.call(*args)
7
+ new(*args).send(:call)
8
+ end
9
+
10
+ # @param authorization_code [String]
11
+ # @param key [String]
12
+ # @param [Hash] invoice_attributes of the invoice.
13
+ # @option invoice_attributes [String] :nit
14
+ # @option invoice_attributes [String] :number
15
+ # @option invoice_attributes [String] :amount
16
+ # @option invoice_attributes [String] :issue_date
17
+ def initialize(authorization_code, key, invoice_attributes = {})
18
+ @invoice = Invoice.new(invoice_attributes)
19
+ @authorization_code = authorization_code
20
+ @key = key
21
+ end
22
+
23
+ private
24
+
25
+ # @return [String]
26
+ def call
27
+ partial_keys = Generators::PartialKeys.call(@key.dup, verification_digits)
28
+ data = Generators::Data.call(@authorization_code, @invoice, partial_keys)
29
+ encrypted_data = encrypt(data, encryption_key)
30
+ ascii_sums = Generators::AsciiSums.call(encrypted_data, partial_keys.count)
31
+ base64_data = Generators::Base64Data.call(ascii_sums, partial_keys)
32
+ format(encrypt(base64_data, encryption_key))
33
+ end
34
+
35
+ # @return [String]
36
+ def encryption_key
37
+ @encryption_key ||= @key + verification_digits
38
+ end
39
+
40
+ # @return [String]
41
+ def verification_digits
42
+ @verification_digits ||= Generators::VerificationDigits.call(@invoice)
43
+ end
44
+
45
+ # @param data [String]
46
+ # @param encryption_key [String]
47
+ # @return [String]
48
+ def encrypt(data, encryption_key)
49
+ RC4.new(encryption_key).encrypt(data).unpack1("H*").upcase
50
+ end
51
+
52
+ # @param code [String]
53
+ # @return [String]
54
+ def format(code)
55
+ code.scan(/.{2}/).join("-")
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Complicode
4
+ module Generators
5
+ class AsciiSums
6
+ # @param encrypted_data [String]
7
+ # @param partials_count [Integer]
8
+ # @return [Struct]
9
+ def self.call(encrypted_data, partials_count)
10
+ Struct.new(:total, :partials).new(0, Array.new(partials_count, 0)).tap do |sums|
11
+ encrypted_data.each_byte.with_index do |byte, index|
12
+ sums.total += byte
13
+ sums.partials[index % partials_count] += byte
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Complicode
4
+ module Generators
5
+ class Base64Data
6
+ BASE64 = %w[
7
+ 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V
8
+ W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z + /
9
+ ].freeze
10
+
11
+ # @param ascii_sums [Struct]
12
+ # @param partial_keys [Array<String>]
13
+ # @return [String]
14
+ def self.call(ascii_sums, partial_keys)
15
+ ascii_sums.partials.each_with_index.inject(0) do |sum, (partial_sum, index)|
16
+ sum + ascii_sums.total * partial_sum / partial_keys[index].size
17
+ end.b(10).to_s(BASE64)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Complicode
4
+ module Generators
5
+ class Data
6
+ # @param authorization_code [String]
7
+ # @param invoice [Complicode::Invoice]
8
+ # @param partial_keys [Array<String>]
9
+ # @return [String]
10
+ def self.call(authorization_code, invoice, partial_keys)
11
+ authorization_code += partial_keys[0].value
12
+
13
+ %i[number nit issue_date amount].each.with_index(1) do |attribute, index|
14
+ invoice.append_to(attribute, partial_keys[index].value)
15
+ end
16
+
17
+ authorization_code + invoice.concat
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Complicode
4
+ module Generators
5
+ class PartialKeys
6
+ # @param key [String]
7
+ # @param verification_digits [String]
8
+ # @return [Array<String>]
9
+ def self.call(key, verification_digits)
10
+ partial_key_sizes = verification_digits.split("").map { |digit| digit.to_i + 1 }
11
+ partial_key_sizes.map do |index|
12
+ PartialKey.new(value: key.slice!(0...index))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Complicode
4
+ module Generators
5
+ class VerificationDigits
6
+ DIGITS_COUNT = 5
7
+ ITERATIONS = 2
8
+
9
+ # @param invoice [Complicode::Invoice]
10
+ # @return [String]
11
+ def self.call(invoice)
12
+ ITERATIONS.times do
13
+ %i[number nit issue_date amount]
14
+ .each { |attribute| invoice.append_checksum_digit_to(attribute) }
15
+ end
16
+
17
+ sum = invoice.sum
18
+ DIGITS_COUNT.times { sum = Verhoeff.checksum_of(sum) }
19
+ sum.to_s[-DIGITS_COUNT..-1]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Complicode
4
+ class Invoice
5
+ attr_accessor :nit, :amount, :issue_date, :number
6
+
7
+ # @param [Hash] attributes of the invoice.
8
+ # @option attributes [String] :nit
9
+ # @option attributes [String] :number
10
+ # @option attributes [String] :amount
11
+ # @option attributes [String] :issue_date
12
+ def initialize(attributes = {})
13
+ @nit = attributes.fetch(:nit, "0").to_s
14
+ @number = attributes.fetch(:number).to_s
15
+ @amount = attributes.fetch(:amount).to_s.tr(",", ".").to_f.round.to_s
16
+ @issue_date = Date.parse(attributes.fetch(:issue_date).to_s).strftime("%Y%m%d")
17
+ end
18
+
19
+ # @param attribute [Symbol]
20
+ def append_checksum_digit_to(attribute)
21
+ append_to(attribute, Verhoeff.checksum_digit_of(send(attribute)).to_s)
22
+ end
23
+
24
+ # @param attribute [Symbol]
25
+ # @param value [String]
26
+ def append_to(attribute, value)
27
+ send("#{attribute}=", send(attribute) + value)
28
+ end
29
+
30
+ # @return [Integer]
31
+ def sum
32
+ [number, nit, issue_date, amount].map(&:to_i).inject(:+)
33
+ end
34
+
35
+ # @return [String]
36
+ def concat
37
+ [number, nit, issue_date, amount].inject(:+)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Complicode
4
+ PartialKey = Struct.new(:value, keyword_init: true) do
5
+ def size
6
+ value.size
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Complicode
4
+ VERSION = "1.0.0"
5
+ end
metadata ADDED
@@ -0,0 +1,244 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: complicode
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Crivella
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: radix
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-rc4
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: verhoeff
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.11'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.11'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '12.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '12.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec_junit_formatter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.4'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.4'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.58'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.58'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.29'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.29'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simplecov
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.16'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.16'
181
+ - !ruby/object:Gem::Dependency
182
+ name: smarter_csv
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '1.2'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '1.2'
195
+ description: Control code generator for invoices inside the Bolivian national tax
196
+ service.
197
+ email:
198
+ - pablocrivella@gmail.com
199
+ executables: []
200
+ extensions: []
201
+ extra_rdoc_files:
202
+ - README.md
203
+ - LICENSE
204
+ files:
205
+ - LICENSE
206
+ - README.md
207
+ - lib/complicode.rb
208
+ - lib/complicode/generate.rb
209
+ - lib/complicode/generators/ascii_sums.rb
210
+ - lib/complicode/generators/base64_data.rb
211
+ - lib/complicode/generators/data.rb
212
+ - lib/complicode/generators/partial_keys.rb
213
+ - lib/complicode/generators/verification_digits.rb
214
+ - lib/complicode/invoice.rb
215
+ - lib/complicode/partial_key.rb
216
+ - lib/complicode/version.rb
217
+ homepage: https://github.com/mobile-bits/complicode-ruby
218
+ licenses:
219
+ - MIT
220
+ metadata:
221
+ bug_tracker_uri: https://github.com/pablocrivella/complicode/issues
222
+ changelog_uri: https://github.com/pablocrivella/complicode/blob/master/CHANGELOG.md
223
+ source_code_uri: https://github.com/pablocrivella/complicode
224
+ post_install_message:
225
+ rdoc_options: []
226
+ require_paths:
227
+ - lib
228
+ required_ruby_version: !ruby/object:Gem::Requirement
229
+ requirements:
230
+ - - ">="
231
+ - !ruby/object:Gem::Version
232
+ version: '0'
233
+ required_rubygems_version: !ruby/object:Gem::Requirement
234
+ requirements:
235
+ - - ">="
236
+ - !ruby/object:Gem::Version
237
+ version: '0'
238
+ requirements: []
239
+ rubyforge_project:
240
+ rubygems_version: 2.7.6
241
+ signing_key:
242
+ specification_version: 4
243
+ summary: Complicode! A needlessly complicated code generator!
244
+ test_files: []