cnab_rb 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: 9668b76dddf165eb9f41fc2ef951f86b99f7f6a79fcc2b11ab1b522b316917b6
4
+ data.tar.gz: 6b6ecc7e01022ebbff08954eaa6e43400b63599d97a9c19e71170f19a4817522
5
+ SHA512:
6
+ metadata.gz: 3a182e807ec9acd97b4178875b286a3171cb1bcf716a562fb40161b3813df7589668ed5463ef49e60da5347b79e08cbfe1135e2f73e68481b582ccdb00179707
7
+ data.tar.gz: bb3a3d765ec2c4c1e0fd4da07fe13284b2cccb9cc9a613f5cbf2f4cb6140bfa79d6ba410a46c6dde5c09e4a5b6487742c6cb4ea2793cfb2ec956c12c4ea2f88c
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cnab_rb.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cnab_rb (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.2)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.4)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 2.0)
30
+ cnab_rb!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TODO: Write your name
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.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # CnabRb
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cnab_rb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cnab_rb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cnab_rb
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cnab_rb.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cnab_rb"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cnab_rb.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "cnab_rb/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cnab_rb"
7
+ spec.version = CnabRb::VERSION
8
+ spec.authors = ["Anderson Danilo"]
9
+ spec.email = ["contato@andersondanilo.com"]
10
+
11
+ spec.summary = %q{CNAB 240 e 400 to brazillian banks.}
12
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
13
+ spec.homepage = "https://github.com/andersondanilo/cnab_rb"
14
+ spec.license = "MIT"
15
+
16
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ #spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
20
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ #spec.add_runtime_dependency "i18n"
32
+
33
+ spec.add_development_dependency "bundler", "~> 2.0"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
@@ -0,0 +1,46 @@
1
+ require 'date'
2
+
3
+ module CnabRb::Format
4
+ class Field
5
+ attr_accessor :pos_start, :pos_end, :picture, :date_format, :default
6
+
7
+ def initialize(params)
8
+ @pos_start = params[:pos].begin
9
+ @pos_end = params[:pos].end
10
+ @picture = Picture.new(params[:picture])
11
+ @default = params[:default]
12
+ @date_format = params[:date_format]
13
+ end
14
+
15
+ def range
16
+ @pos_start..@pos_end
17
+ end
18
+
19
+ def length
20
+ @pos_end - @pos_start + 1
21
+ end
22
+
23
+ def validate!
24
+ if length != @picture.length
25
+ raise CnabRb::Error.new("Invalid picture length between #{@pos_start} and #{@pos_end}")
26
+ end
27
+ end
28
+
29
+ def encode(value)
30
+ if @date_format && value.respond_to?('strftime')
31
+ value = value.strftime(@date_format)
32
+ end
33
+ @picture.encode(value)
34
+ end
35
+
36
+ def decode(value)
37
+ decoded = @picture.decode(value)
38
+
39
+ unless @date_format.nil?
40
+ DateTime.strptime(decoded.to_s, @date_format)
41
+ else
42
+ decoded
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,28 @@
1
+ module CnabRb::Format
2
+ class File
3
+ attr_accessor :lines
4
+
5
+ LINE_BREAK = "\r\n"
6
+
7
+ def initialize
8
+ @lines = []
9
+ end
10
+
11
+ def encode
12
+ @lines.map{|line| line.encode}.join(LINE_BREAK)
13
+ end
14
+
15
+ def decode(text)
16
+ @lines = text.split(LINE_BREAK).map do |line_text|
17
+ layout = get_line_layout(line_text)
18
+ line = Line.new(layout)
19
+ line.decode(line_text)
20
+ line
21
+ end
22
+ end
23
+
24
+ def get_line_layout(line_text)
25
+ raise CnabRb::Error.new("get_line_layout not implemented")
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,49 @@
1
+ module CnabRb::Format
2
+ class Line
3
+ def initialize(layout, attributes = {})
4
+ @layout = layout
5
+ @attributes = attributes
6
+ end
7
+
8
+ def encode
9
+ @layout.validate!
10
+ pieces = @layout.ordered_fields.map do |field_array|
11
+ key, field = field_array
12
+ field.encode(@attributes[key] || field.default)
13
+ end
14
+ pieces.join
15
+ end
16
+
17
+ def decode(text)
18
+ @layout.validate!
19
+ @layout.fields.each do |key, field|
20
+ substr = text[field.pos_start - 1, field.length]
21
+ @attributes[key] = field.decode(substr)
22
+ end
23
+ end
24
+
25
+ def method_missing(method_name, *args)
26
+ if @attributes.key? method_name.to_sym
27
+ return @attributes[method_name.to_sym]
28
+ end
29
+
30
+ if method_name =~ /(.+)=/
31
+ return @attributes[$1.to_sym] = args[0]
32
+ end
33
+
34
+ raise ArgumentError.new("Method `#{method_name}` doesn't exist.")
35
+ end
36
+
37
+ def respond_to_missing?(method_name)
38
+ if @attributes.key? method_name.to_sym
39
+ return true
40
+ end
41
+
42
+ if method_name =~ /(.+)=/
43
+ return @attributes.key? $1.to_sym
44
+ end
45
+
46
+ return false
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,37 @@
1
+ module CnabRb::Format
2
+ class Picture
3
+ def initialize(picture)
4
+ if match = picture.match(/([X9])\((\d+)\)(V9\((\d+)\))?/)
5
+ type, length, _, length_decimal = match.captures
6
+ length = length.to_i
7
+
8
+ unless length_decimal.nil?
9
+ length_decimal = length_decimal.to_i
10
+ end
11
+
12
+
13
+ if 'X' == type
14
+ @format = PictureFormats::Text.new(length)
15
+ elsif ('9' == type and length_decimal.nil?)
16
+ @format = PictureFormats::Integer.new(length)
17
+ else
18
+ @format = PictureFormats::Float.new(length, length_decimal)
19
+ end
20
+ else
21
+ raise CnabRb::Error.new("Invalid picture #{picture}")
22
+ end
23
+ end
24
+
25
+ def encode(value)
26
+ @format.encode(value)
27
+ end
28
+
29
+ def decode(value)
30
+ @format.decode(value)
31
+ end
32
+
33
+ def length
34
+ @format.length
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ module CnabRb::Format::PictureFormats
2
+ class Float
3
+ def initialize(length_left, length_right)
4
+ @length_right = length_right
5
+ @format_left = CnabRb::Format::PictureFormats::Integer.new(length_left)
6
+ end
7
+
8
+ def encode(value)
9
+ left_value = get_left_value(value)
10
+ right_value = get_right_value(value)
11
+ left_value + right_value
12
+ end
13
+
14
+ def get_left_value(value)
15
+ @format_left.encode(value.to_s.split('.')[0])
16
+ end
17
+
18
+ def get_right_value(value)
19
+ value.to_f.round(@length_right).to_s.split('.')[1].ljust(@length_right, '0')
20
+ end
21
+
22
+ def decode(value)
23
+ value.to_f / 10.pow(@length_right)
24
+ end
25
+
26
+ def length
27
+ @length_left + @length_right
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ module CnabRb::Format::PictureFormats
2
+ class Integer
3
+ attr_reader :length
4
+
5
+ def initialize(length)
6
+ @length = length
7
+ end
8
+
9
+ def encode(value)
10
+ value = value.to_s.rjust(@length, '0')
11
+ if value.length > @length
12
+ raise CnabRb::Error.new("Invalid integer length to #{value}")
13
+ end
14
+ value
15
+ end
16
+
17
+ def decode(value)
18
+ value.to_i
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ module CnabRb::Format::PictureFormats
2
+ class Text
3
+ attr_reader :length
4
+
5
+ def initialize(length)
6
+ @length = length
7
+ end
8
+
9
+ def encode(value)
10
+ value = value.to_s
11
+ result = if value.length < @length
12
+ value.ljust(@length)
13
+ else
14
+ value.slice(0, @length)
15
+ end
16
+
17
+ transliterate(result).upcase
18
+ end
19
+
20
+ def decode(value)
21
+ value.strip
22
+ end
23
+
24
+ private
25
+ def transliterate(text)
26
+ text.tr(
27
+ "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
28
+ "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz")
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,137 @@
1
+ module CnabRb::Layouts::Cnab240::Generic
2
+ class HeaderFile < CnabRb::Layouts::Layout
3
+ def init_fields
4
+ field :bank_code,
5
+ # codigo_banco
6
+ pos: 1..3,
7
+ picture: '9(3)'
8
+
9
+ field :service_batch,
10
+ # lote_servico
11
+ pos: 4..7,
12
+ picture: '9(4)'
13
+
14
+ field :registry_type,
15
+ # tipo_registro
16
+ pos: 8..8,
17
+ picture: '9(1)',
18
+ default: '1'
19
+
20
+ field :operation_type,
21
+ # tipo_operacao
22
+ pos: 9..9,
23
+ picture: 'X(1)'
24
+
25
+ field :service_type,
26
+ # tipo_servico
27
+ pos: 10..11,
28
+ picture: '9(2)',
29
+ default: '01'
30
+
31
+ field :release_form,
32
+ # forma_lancamento
33
+ pos: 12..13,
34
+ picture: '9(2)',
35
+ default: '00'
36
+
37
+ field :layout_version,
38
+ # versao_layout_lote
39
+ pos: 14..16,
40
+ picture: '9(3)',
41
+ default: '030'
42
+
43
+ field :exclusive_use_febraban_1,
44
+ # uso_exclusivo_febraban_01
45
+ pos: 17..17,
46
+ picture: 'X(1)',
47
+ default: ''
48
+
49
+ field :registration_code,
50
+ # codigo_inscricao
51
+ pos: 18..18,
52
+ picture: '9(1)'
53
+
54
+ field :registration_number,
55
+ # numero_inscricao
56
+ pos: 19..33,
57
+ picture: '9(15)'
58
+
59
+ field :agreement_code,
60
+ # codigo_convenio
61
+ pos: 34..49,
62
+ picture: 'X(16)'
63
+
64
+ field :exclusive_use_bank_1,
65
+ # uso_exclusivo_banco
66
+ pos: 50..53,
67
+ picture: 'X(4)',
68
+ default: ''
69
+
70
+ field :agency,
71
+ # agencia
72
+ pos: 54..58,
73
+ picture: '9(5)'
74
+
75
+ field :agency_cd,
76
+ # agencia_dv
77
+ pos: 59..59,
78
+ picture: '9(1)'
79
+
80
+ field :transferor_code,
81
+ # codigo_cedente
82
+ pos: 60..71,
83
+ picture: '9(12)'
84
+
85
+ field :transferor_code_cd,
86
+ # codigo_cedente_dv
87
+ pos: 72..72,
88
+ picture: '9(1)'
89
+
90
+ field :agency_transferor_cd,
91
+ # agencia_mais_cedente_dv
92
+ pos: 73..73,
93
+ picture: '9(1)'
94
+
95
+ field :company_name,
96
+ # nome_empresa
97
+ pos: 74..103,
98
+ picture: 'X(30)'
99
+
100
+ field :message_1,
101
+ # mensagem_1
102
+ pos: 104..143,
103
+ picture: 'X(40)',
104
+ default: ''
105
+
106
+ field :message_2,
107
+ # mensagem_2
108
+ pos: 144..183,
109
+ picture: 'X(40)',
110
+ default: ''
111
+
112
+ field :sequential_number_file,
113
+ # numero_sequencial_arquivo
114
+ pos: 184..191,
115
+ picture: '9(8)'
116
+
117
+ field :generation_date,
118
+ # data_geracao
119
+ pos: 192..199,
120
+ picture: '9(8)',
121
+ date_format: '%d%m%Y'
122
+
123
+ field :credit_date,
124
+ # data_credito
125
+ pos: 200..207,
126
+ picture: '9(8)',
127
+ date_format: '%d%m%Y',
128
+ default: '0'
129
+
130
+ field :exclusive_use_febraban_2,
131
+ # uso_exclusivo_febraban_02
132
+ pos: 208..240,
133
+ picture: 'X(33)',
134
+ default: ''
135
+ end
136
+ end
137
+ end