cnab 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +18 -2
- data/lib/cnab.rb +6 -2
- data/lib/cnab/detalhe.rb +5 -0
- data/lib/cnab/detalhe/segmento_t.rb +31 -29
- data/lib/cnab/detalhe/segmento_t_u.rb +22 -0
- data/lib/cnab/detalhe/segmento_u.rb +26 -24
- data/lib/cnab/exceptions.rb +1 -0
- data/lib/cnab/exceptions/line_not_parseable.rb +9 -0
- data/lib/cnab/header_arquivo.rb +26 -24
- data/lib/cnab/header_lote/cobranca.rb +25 -23
- data/lib/cnab/helper.rb +13 -1
- data/lib/cnab/trailer_arquivo.rb +10 -8
- data/lib/cnab/trailer_lote/cobranca.rb +17 -15
- data/lib/cnab/version.rb +1 -1
- data/spec/cnab/detalhe/segmento_t_spec.rb +97 -89
- data/spec/cnab/detalhe/segmento_t_u_spec.rb +24 -0
- data/spec/cnab/detalhe/segmento_u_spec.rb +105 -97
- data/spec/cnab/detalhe_spec.rb +24 -3
- data/spec/cnab/header_arquivo_spec.rb +103 -95
- data/spec/cnab/header_lote/cobranca_spec.rb +79 -71
- data/spec/cnab/trailer_arquivo_spec.rb +34 -26
- data/spec/cnab_spec.rb +55 -31
- data/spec/spec_helper.rb +1 -1
- metadata +12 -2
data/README.md
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/cnab.png)](http://badge.fury.io/rb/cnab) [![Build Status](https://travis-ci.org/zertico/cnab.png)](https://travis-ci.org/zertico/cnab) [![Coverage Status](https://coveralls.io/repos/zertico/cnab/badge.png?branch=master)](https://coveralls.io/r/zertico/cnab) [![Code Climate](https://codeclimate.com/github/zertico/cnab.png)](https://codeclimate.com/github/zertico/cnab)
|
4
4
|
|
5
|
-
Gem used to parse return from Brazil banks in format of cnab files.
|
5
|
+
Gem used to parse return from Brazil banks in format of cnab files. It is based on the version 08.7 of the cnab.
|
6
|
+
Right now it has support only for Segmento T and Segmento U.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -20,10 +21,25 @@ Or install it yourself as:
|
|
20
21
|
|
21
22
|
## Usage
|
22
23
|
|
23
|
-
|
24
|
+
```ruby
|
25
|
+
cnab = Cnab.parse('path/to/your/file')
|
26
|
+
```
|
27
|
+
|
28
|
+
This method will return the file parsed in objects.
|
29
|
+
|
30
|
+
If you need to merge the segments T and U, just pass true as the optional parameter:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
cnab = Cnab.parse('path/to/your/file', true)
|
34
|
+
```
|
35
|
+
|
36
|
+
## TODO
|
37
|
+
|
38
|
+
* Add support for another type of segments
|
24
39
|
|
25
40
|
## Mantainers
|
26
41
|
[@plribeiro3000](https://github.com/plribeiro3000)
|
42
|
+
|
27
43
|
[@mfbmina](https://github.com/mfbmina)
|
28
44
|
|
29
45
|
## Contributing
|
data/lib/cnab.rb
CHANGED
@@ -11,7 +11,7 @@ module Cnab
|
|
11
11
|
autoload :Helper, 'cnab/helper'
|
12
12
|
autoload :Exceptions, 'cnab/exceptions'
|
13
13
|
|
14
|
-
def self.parse(file = nil)
|
14
|
+
def self.parse(file = nil, merge = false)
|
15
15
|
raise Exceptions::NoFileGiven if file.nil?
|
16
16
|
|
17
17
|
File.open(file, 'rb') do |f|
|
@@ -24,7 +24,11 @@ module Cnab
|
|
24
24
|
trailer_lote = TrailerLote::Cobranca.new(line)
|
25
25
|
break
|
26
26
|
end
|
27
|
-
|
27
|
+
if merge
|
28
|
+
detalhes << Detalhe.merge(line, f.gets)
|
29
|
+
else
|
30
|
+
detalhes << Detalhe.parse(line)
|
31
|
+
end
|
28
32
|
end
|
29
33
|
|
30
34
|
trailer_arquivo = TrailerArquivo.new(f.gets)
|
data/lib/cnab/detalhe.rb
CHANGED
@@ -2,6 +2,7 @@ module Cnab
|
|
2
2
|
module Detalhe
|
3
3
|
autoload :SegmentoT, 'cnab/detalhe/segmento_t'
|
4
4
|
autoload :SegmentoU, 'cnab/detalhe/segmento_u'
|
5
|
+
autoload :SegmentoTU, 'cnab/detalhe/segmento_t_u'
|
5
6
|
|
6
7
|
def self.parse(line)
|
7
8
|
case line[13]
|
@@ -13,5 +14,9 @@ module Cnab
|
|
13
14
|
raise Exceptions::SegmentNotImplemented
|
14
15
|
end
|
15
16
|
end
|
17
|
+
|
18
|
+
def self.merge(line1, line2)
|
19
|
+
SegmentoTU.new(parse(line1), parse(line2))
|
20
|
+
end
|
16
21
|
end
|
17
22
|
end
|
@@ -4,35 +4,37 @@ module Cnab
|
|
4
4
|
include Helper
|
5
5
|
|
6
6
|
def initialize(line)
|
7
|
-
|
8
|
-
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@
|
24
|
-
@
|
25
|
-
@
|
26
|
-
@
|
27
|
-
@
|
28
|
-
@
|
29
|
-
@
|
30
|
-
@
|
31
|
-
@
|
32
|
-
@
|
33
|
-
@
|
34
|
-
@
|
35
|
-
@
|
7
|
+
raise Exceptions::LineNotParseable unless line.size == 242
|
8
|
+
|
9
|
+
@banco = line[0..2].strip
|
10
|
+
@lote = line[3..6].strip
|
11
|
+
@tipo_registro = line[7..7].strip
|
12
|
+
@num_registro = line[8..12].strip
|
13
|
+
@segmento = line[13..13].strip
|
14
|
+
@res_cnab1 = line[14..14].strip
|
15
|
+
@cod_movimentacao = line[15..16].strip
|
16
|
+
@agencia = line[17..21].strip
|
17
|
+
@dv_agencia = line[22..22].strip
|
18
|
+
@conta_corrente = line[23..34].strip
|
19
|
+
@dv_conta = line[35..35].strip
|
20
|
+
@dv_conta_agencia = line[36..36].strip
|
21
|
+
@id_titulo = line[37..56].strip
|
22
|
+
@carteira = line[57..57].strip
|
23
|
+
@doc_cobranca = line[58..72].strip
|
24
|
+
@vencimento = line[73..80].strip
|
25
|
+
@valor_titulo = line[81..95].strip
|
26
|
+
@banco_recebimento = line[96..98].strip
|
27
|
+
@ag_recebimento = line[99..103].strip
|
28
|
+
@dv_ag_recebimento = line[104..104].strip
|
29
|
+
@id_titulo_empresa = line[105..129].strip
|
30
|
+
@cod_moeda = line[130..131].strip
|
31
|
+
@tipo_inscricao = line[132..132].strip
|
32
|
+
@num_inscricao = line[133..147].strip
|
33
|
+
@nome = line[148..187].strip
|
34
|
+
@num_contrato = line[188..197].strip
|
35
|
+
@tarifa = line[198..212].strip
|
36
|
+
@motivo = line[213..222].strip
|
37
|
+
@res_cnab2 = line[223..239].strip
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Cnab
|
2
|
+
module Detalhe
|
3
|
+
class SegmentoTU
|
4
|
+
def initialize(segmento_t, segmento_u)
|
5
|
+
@segmento_t = segmento_t
|
6
|
+
@segmento_u = segmento_u
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(method_name)
|
10
|
+
return @segmento_t.send(method_name) if @segmento_t.respond_to?(method_name)
|
11
|
+
return @segmento_u.send(method_name) if @segmento_u.respond_to?(method_name)
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
def respond_to_missing?(method_name, include_private = false)
|
16
|
+
return true if @segmento_t.respond_to?(method_name)
|
17
|
+
return true if @segmento_u.respond_to?(method_name)
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -4,30 +4,32 @@ module Cnab
|
|
4
4
|
include Helper
|
5
5
|
|
6
6
|
def initialize(line)
|
7
|
-
|
8
|
-
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@
|
24
|
-
@
|
25
|
-
@
|
26
|
-
@
|
27
|
-
@
|
28
|
-
@
|
29
|
-
@
|
30
|
-
@
|
7
|
+
raise Exceptions::LineNotParseable unless line.size == 242
|
8
|
+
|
9
|
+
@banco = line[0..2].strip
|
10
|
+
@lote = line[3..6].strip
|
11
|
+
@tipo_registro = line[7..7].strip
|
12
|
+
@num_registro = line[8..12].strip
|
13
|
+
@segmento = line[13..13].strip
|
14
|
+
@res_cnab1 = line[14..14].strip
|
15
|
+
@cod_movimentacao = line[15..16].strip
|
16
|
+
@acrescimos = line[17..31].strip
|
17
|
+
@valor_desconto = line[32..46].strip
|
18
|
+
@valor_abatimento = line[47..61].strip
|
19
|
+
@valor_iof = line[62..76].strip
|
20
|
+
@valor_pago = line[77..91].strip
|
21
|
+
@valor_liquido = line[92..106].strip
|
22
|
+
@outras_despesas = line[107..122].strip
|
23
|
+
@outros_creditos = line[123..136].strip
|
24
|
+
@data_ocorrencia = line[137..144].strip
|
25
|
+
@data_credito = line[145..152].strip
|
26
|
+
@cod_ocorrencia_sacado = line[153..156].strip
|
27
|
+
@data_ocorrencia_sacado = line[157..164].strip
|
28
|
+
@valor_ocorrencia_sacado = line[165..179].strip
|
29
|
+
@compl_ocorrencia_sacado = line[180..209].strip
|
30
|
+
@banco_compensacao = line[210..212].strip
|
31
|
+
@banco_enviado = line[213..232].strip
|
32
|
+
@res_cnab2 = line[233..239].strip
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
data/lib/cnab/exceptions.rb
CHANGED
data/lib/cnab/header_arquivo.rb
CHANGED
@@ -3,30 +3,32 @@ module Cnab
|
|
3
3
|
include Helper
|
4
4
|
|
5
5
|
def initialize(line)
|
6
|
-
|
7
|
-
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@
|
24
|
-
@
|
25
|
-
@
|
26
|
-
@
|
27
|
-
@
|
28
|
-
@
|
29
|
-
@
|
6
|
+
raise Exceptions::LineNotParseable unless line.size == 242
|
7
|
+
|
8
|
+
@banco = line[0..2].strip
|
9
|
+
@lote = line[3..6].strip
|
10
|
+
@registro = line[7..7].strip
|
11
|
+
@res_cnab1 = line[8..16].strip
|
12
|
+
@tipo_empresa = line[17..17].strip
|
13
|
+
@num_empresa = line[18..31].strip
|
14
|
+
@convenio = line[32..51].strip
|
15
|
+
@agencia = line[52..56].strip
|
16
|
+
@dv_agencia = line[57..57].strip
|
17
|
+
@conta_corrente = line[58..69].strip
|
18
|
+
@dv_conta = line[70..70].strip
|
19
|
+
@dv_conta_agencia = line[71..71].strip
|
20
|
+
@nome_empresa = line[72..101].strip
|
21
|
+
@nome_banco = line[102..131].strip
|
22
|
+
@res_cnab2 = line[132..141].strip
|
23
|
+
@cod_retorno = line[142..142].strip
|
24
|
+
@data_geracao_arq = line[143..150].strip
|
25
|
+
@hora_geracao_arq = line[151..156].strip
|
26
|
+
@sequencia = line[157..162].strip
|
27
|
+
@versao = line[163..165].strip
|
28
|
+
@densidade = line[166..170].strip
|
29
|
+
@res_banco = line[171..190].strip
|
30
|
+
@res_empresa = line[191..210].strip
|
31
|
+
@res_cnab3 = line[211..239].strip
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|
@@ -4,29 +4,31 @@ module Cnab
|
|
4
4
|
include Helper
|
5
5
|
|
6
6
|
def initialize(line)
|
7
|
-
|
8
|
-
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@
|
22
|
-
@
|
23
|
-
@
|
24
|
-
@
|
25
|
-
@
|
26
|
-
@
|
27
|
-
@
|
28
|
-
@
|
29
|
-
@
|
7
|
+
raise Exceptions::LineNotParseable unless line.size == 242
|
8
|
+
|
9
|
+
@banco = line[0..2].strip
|
10
|
+
@lote = line[3..6].strip
|
11
|
+
@registro = line[7..7].strip
|
12
|
+
@tipo_operacao = line[8..8].strip
|
13
|
+
@tipo_servico = line[9..10].strip
|
14
|
+
@res_cnab1 = line[11..12].strip
|
15
|
+
@layout_lote = line[13..15].strip
|
16
|
+
@res_cnab2 = line[16..16].strip
|
17
|
+
@tipo_empresa = line[17..17].strip
|
18
|
+
@num_empresa = line[18..32].strip
|
19
|
+
@convenio = line[33..52].strip
|
20
|
+
@agencia = line[53..57].strip
|
21
|
+
@dv_agencia = line[58..58].strip
|
22
|
+
@conta_corrente = line[59..70].strip
|
23
|
+
@dv_conta = line[71..71].strip
|
24
|
+
@dv_conta_agencia = line[72..72].strip
|
25
|
+
@nome_empresa = line[73..102].strip
|
26
|
+
@informacao1 = line[103..142].strip
|
27
|
+
@informacao2 = line[143..182].strip
|
28
|
+
@numero_remessa = line[183..190].strip
|
29
|
+
@data_gravacao = line[191..198].strip
|
30
|
+
@data_credito = line[199..206].strip
|
31
|
+
@res_cnab3 = line[207..239].strip
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|
data/lib/cnab/helper.rb
CHANGED
@@ -3,5 +3,17 @@ module Cnab
|
|
3
3
|
def method_missing(method)
|
4
4
|
instance_variable_get("@#{method}")
|
5
5
|
end
|
6
|
+
|
7
|
+
def respond_to_missing?(method_name, include_private = false)
|
8
|
+
return true unless instance_variable_get("@#{method_name}").nil?
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def inspect
|
13
|
+
variables = instance_variables.map do |instance_variable|
|
14
|
+
"#{instance_variable.to_s.gsub('@', '')}: \"#{instance_variable_get(instance_variable)}\""
|
15
|
+
end.join(", ")
|
16
|
+
"<#{self.class.name} #{variables}>"
|
17
|
+
end
|
6
18
|
end
|
7
|
-
end
|
19
|
+
end
|
data/lib/cnab/trailer_arquivo.rb
CHANGED
@@ -3,14 +3,16 @@ module Cnab
|
|
3
3
|
include Helper
|
4
4
|
|
5
5
|
def initialize(line)
|
6
|
-
|
7
|
-
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
6
|
+
raise Exceptions::LineNotParseable unless line.size == 242
|
7
|
+
|
8
|
+
@banco = line[0..2].strip
|
9
|
+
@lote = line[3..6].strip
|
10
|
+
@registro = line[7..7].strip
|
11
|
+
@res_cnab1 = line[8..16].strip
|
12
|
+
@qtd_lotes = line[17..22].strip
|
13
|
+
@qtd_registros = line[23..28].strip
|
14
|
+
@qtd_contas = line[29..34].strip
|
15
|
+
@res_cnab2 = line[35..239].strip
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
@@ -4,21 +4,23 @@ module Cnab
|
|
4
4
|
include Helper
|
5
5
|
|
6
6
|
def initialize(line)
|
7
|
-
|
8
|
-
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@valor_titulos_simples = line[
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
19
|
-
@
|
20
|
-
@
|
21
|
-
@
|
7
|
+
raise Exceptions::LineNotParseable unless line.size == 242
|
8
|
+
|
9
|
+
@banco = line[0..2].strip
|
10
|
+
@lote = line[3..6].strip
|
11
|
+
@registro = line[7..7].strip
|
12
|
+
@res_cnab1 = line[8..16].strip
|
13
|
+
@qtd_registros = line[17..22].strip
|
14
|
+
@qtd_titulos_simples = line[23..28].strip
|
15
|
+
@valor_titulos_simples = line[29..45].strip
|
16
|
+
@qtd_titulos_vinculada = line[46..51].strip
|
17
|
+
@valor_titulos_simples = line[52..68].strip
|
18
|
+
@qtd_titulos_caucionada = line[69..74].strip
|
19
|
+
@valor_titulos_caucionada = line[75..91].strip
|
20
|
+
@qtd_titulos_descontada = line[92..97].strip
|
21
|
+
@valor_titulos_descontada = line[98..114].strip
|
22
|
+
@num_aviso = line[115..122].strip
|
23
|
+
@res_cnab3 = line[123..239].strip
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
data/lib/cnab/version.rb
CHANGED