Class: Cnab240santander::Retorno

Inherits:
Object
  • Object
show all
Defined in:
lib/cnab240santander/retorno.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Retorno



12
13
14
15
16
17
18
# File 'lib/cnab240santander/retorno.rb', line 12

def initialize(args = {})
  if args.has_key? :path
    @linhas = processar(args) if validaCNAB240(args[:path])
  else
    raise Exception, "Necessario informar o :path"
  end
end

Instance Attribute Details

#linhasObject

Returns the value of attribute linhas.



10
11
12
# File 'lib/cnab240santander/retorno.rb', line 10

def linhas
  @linhas
end

Instance Method Details

#add_to_hash(my_hash, combined_hash) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/cnab240santander/retorno.rb', line 85

def add_to_hash(my_hash, combined_hash)
  my_hash.each_key do |key|
    if ( combined_hash.has_key?(key) )
      combined_hash[ "#{key}-dup" ] = my_hash[key]
    else
      combined_hash[key]=my_hash[key]
    end
  end
  combined_hash
end

#processar(args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cnab240santander/retorno.rb', line 47

def processar(args)
  linhas = []
  numLn = 0
  
  File.open(args[:path], 'rb') { |f|
    while (linha = f.gets)
      tipoLn = linha[7..7].to_i
      if tipoLn == $HEADER_ARQUIVO and args[:retorna] == $HEADER_ARQUIVO #0
        linhas << HeaderArquivo.processar(linha)
      elsif tipoLn == $DETALHE and args[:retorna] == $DETALHE #3
        segmento = linha[13..13]
        if args[:merge] ==  true
          hash_aux = {}
      
          #RESGATANDO SEGMENTO G PARA AGRUPAMENTO
          segmento = linha[13..13]
          add_to_hash(Detalhe.processar(linha, segmento), hash_aux)
      
          #RESGATANDO SEGMENTO H PARA AGRUPAMENTO (PROXIMA LINHA)
          linha = f.gets
          segmento = linha[13..13]
          add_to_hash(Detalhe.processar(linha, segmento), hash_aux)
      
          linhas << hash_aux
        else
          linhas << Detalhe.processar(linha, segmento)
        end
    
      elsif args[:retorna].blank?
        linhas << processarLinha(numLn, linha)
      end
      numLn += 1
    end
  }
  
  return linhas
end

#processarLinha(numLn, linha) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cnab240santander/retorno.rb', line 29

def processarLinha(numLn, linha)
  tipoLn = linha[7..7].to_i
  segmento = linha[13..13].to_s

  case tipoLn
    when $HEADER_ARQUIVO
       HeaderArquivo.processar(linha)
    when $HEADER_LOTE 
       HeaderLote.processar(linha)
    when $DETALHE
       Detalhe.processar(linha, segmento)
    when $TRAILER_LOTE
       TrailerLote.processar(linha)
    when $TRAILER_ARQUIVO 
       TrailerArquivo.processar(linha) 
    end
end

#validaCNAB240(path) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/cnab240santander/retorno.rb', line 20

def validaCNAB240(path)
  File.open(path, 'rb') do |f|
    while linha = f.gets
      return false if linha.length != 242
    end
  end
  return true
end