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 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
- TODO: Write usage instructions here
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
@@ -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
- detalhes << Detalhe.parse(line)
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)
@@ -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
- @banco = line[0..2]
8
- @lote = line[3..6]
9
- @tipo_registro = line[7..7]
10
- @num_registro = line[8..12]
11
- @segmento = line[13..13]
12
- @res_cnab1 = line[14..14]
13
- @cod_movimentacao = line[15..16]
14
- @agencia = line[17..21]
15
- @dv_agencia = line[22..22]
16
- @conta_corrente = line[23..34]
17
- @dv_conta = line[35..35]
18
- @dv_conta_agencia = line[36..36]
19
- @id_titulo = line[37..56]
20
- @carteira = line[57..57]
21
- @doc_cobranca = line[58..72]
22
- @vencimento = line[73..80]
23
- @valor_titulo = line[81..95]
24
- @banco_recebimento = line[96..98]
25
- @ag_recebimento = line[99..103]
26
- @dv_ag_recebimento = line[104..104]
27
- @id_titulo_empresa = line[105..129]
28
- @cod_moeda = line[130..131]
29
- @tipo_inscricao = line[132..132]
30
- @num_inscricao = line[133..147]
31
- @nome = line[148..187]
32
- @num_contrato = line[188..197]
33
- @tarifa = line[198..212]
34
- @motivo = line[213..222]
35
- @res_cnab2 = line[223..239]
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
- @banco = line[0..2]
8
- @lote = line[3..6]
9
- @tipo_registro = line[7..7]
10
- @num_registro = line[8..12]
11
- @segmento = line[13..13]
12
- @res_cnab1 = line[14..14]
13
- @cod_movimentacao = line[15..16]
14
- @acrescimos = line[17..31]
15
- @valor_desconto = line[32..46]
16
- @valor_abatimento = line[47..61]
17
- @valor_iof = line[62..76]
18
- @valor_pago = line[77..91]
19
- @valor_liquido = line[92..106]
20
- @outras_despesas = line[107..122]
21
- @outros_creditos = line[123..136]
22
- @data_ocorrencia = line[137..144]
23
- @data_credito = line[145..152]
24
- @cod_ocorrencia_sacado = line[153..156]
25
- @data_ocorrencia_sacado = line[157..164]
26
- @valor_ocorrencia_sacado = line[165..179]
27
- @compl_ocorrencia_sacado = line[180..209]
28
- @banco_compensacao = line[210..212]
29
- @banco_enviado = line[213..232]
30
- @res_cnab2 = line[233..239]
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
@@ -2,5 +2,6 @@ module Cnab
2
2
  module Exceptions
3
3
  autoload :NoFileGiven, 'cnab/exceptions/no_file_given'
4
4
  autoload :SegmentNotImplemented, 'cnab/exceptions/segment_not_implemented'
5
+ autoload :LineNotParseable, 'cnab/exceptions/line_not_parseable'
5
6
  end
6
7
  end
@@ -0,0 +1,9 @@
1
+ module Cnab
2
+ module Exceptions
3
+ class LineNotParseable < StandardError
4
+ def self.initialize
5
+ super("This line does not have the expected size!")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -3,30 +3,32 @@ module Cnab
3
3
  include Helper
4
4
 
5
5
  def initialize(line)
6
- @banco = line[0..2]
7
- @lote = line[3..6]
8
- @registro = line[7..7]
9
- @res_cnab1 = line[8..16]
10
- @tipo_empresa = line[17..17]
11
- @num_empresa = line[18..31]
12
- @convenio = line[32..51]
13
- @agencia = line[52..56]
14
- @dv_agencia = line[57..57]
15
- @conta_corrente = line[58..69]
16
- @dv_conta = line[70..70]
17
- @dv_conta_agencia = line[71..71]
18
- @nome_empresa = line[72..101]
19
- @nome_banco = line[102..131]
20
- @res_cnab2 = line[132..141]
21
- @cod_retorno = line[142..142]
22
- @data_geracao_arq = line[143..150]
23
- @hora_geracao_arq = line[151..156]
24
- @sequencia = line[157..162]
25
- @versao = line[163..165]
26
- @densidade = line[166..170]
27
- @res_banco = line[171..190]
28
- @res_empresa = line[191..210]
29
- @res_cnab3 = line[211..239]
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
- @banco = line[0..2]
8
- @lote = line[3..6]
9
- @registro = line[7..7]
10
- @tipo_operacao = line[8..8]
11
- @tipo_servico = line[9..10]
12
- @res_cnab1 = line[11..12]
13
- @layout_lote = line[13..15]
14
- @res_cnab2 = line[16..16]
15
- @tipo_empresa = line[17..17]
16
- @num_empresa = line[18..32]
17
- @convenio = line[33..52]
18
- @agencia = line[53..57]
19
- @dv_agencia = line[58..58]
20
- @conta_corrente = line[59..70]
21
- @dv_conta = line[71..71]
22
- @dv_conta_agencia = line[72..72]
23
- @nome_empresa = line[73..102]
24
- @informacao1 = line[103..142]
25
- @informacao2 = line[143..182]
26
- @numero_remessa = line[183..190]
27
- @data_gravacao = line[191..198]
28
- @data_credito = line[199..206]
29
- @res_cnab3 = line[207..239]
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
@@ -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
@@ -3,14 +3,16 @@ module Cnab
3
3
  include Helper
4
4
 
5
5
  def initialize(line)
6
- @banco = line[0..2]
7
- @lote = line[3..6]
8
- @registro = line[7..7]
9
- @res_cnab1 = line[8..16]
10
- @qtd_lotes = line[17..22]
11
- @qtd_registros = line[23..28]
12
- @qtd_contas = line[29..34]
13
- @res_cnab2 = line[35..239]
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
- @banco = line[0..2]
8
- @lote = line[3..6]
9
- @registro = line[7..7]
10
- @res_cnab1 = line[8..16]
11
- @qtd_registros = line[17..22]
12
- @qtd_titulos_simples = line[23..28]
13
- @valor_titulos_simples = line[29..45]
14
- @qtd_titulos_vinculada = line[46..51]
15
- @valor_titulos_simples = line[52..68]
16
- @qtd_titulos_caucionada = line[69..74]
17
- @valor_titulos_caucionada = line[75..91]
18
- @qtd_titulos_descontada = line[92..97]
19
- @valor_titulos_descontada = line[98..114]
20
- @num_aviso = line[115..122]
21
- @res_cnab3 = line[123..239]
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
@@ -1,3 +1,3 @@
1
1
  module Cnab
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end