Class: VerificadorBoletoBr::Slip::Arrecadation::Validity

Inherits:
Object
  • Object
show all
Includes:
Calculus::Module10, Calculus::Module11, Operationals
Defined in:
lib/verificador_boleto_br/slip/arrecadation/validity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Calculus::Module11

#modulo11

Methods included from Calculus::Module10

#modulo10

Methods included from Operationals

#bank_epoque, #clean_masks

Constructor Details

#initialize(cod) ⇒ Validity

Returns a new instance of Validity.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 14

def initialize(cod)
  @digitable_line = cod
  raise ArgumentError, 'Expected String'              unless @digitable_line.class.eql?(String)
  raise ArgumentError, 'Invalid Digitable Line'       unless clean_digitable_line.size == 48
  raise ArgumentError, 'Boleto Não é de Arrecadação'  unless @digitable_line[0] == '8'

  @errors = []
  @kind = :arrecadation_slip
end

Instance Attribute Details

#digitable_lineObject

Returns the value of attribute digitable_line.



12
13
14
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 12

def digitable_line
  @digitable_line
end

#errorsObject

Returns the value of attribute errors.



12
13
14
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 12

def errors
  @errors
end

#kindObject

Returns the value of attribute kind.



12
13
14
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 12

def kind
  @kind
end

Instance Method Details

#bacen_moduleObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 79

def bacen_module
  case clean_digitable_line[2]
  when '6'
    'modulo10'
  when '7'
    'modulo10'
  when '8'
    'modulo11'
  when '9'
    'modulo11'
  else
    raise ArgumentError, 'Invalid Digitable Line'
  end
end

#clean_digitable_lineObject



24
25
26
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 24

def clean_digitable_line
  clean_masks(@digitable_line)
end

#digitable_line_without_dvsObject



129
130
131
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 129

def digitable_line_without_dvs
  grouped_digitable_line.map { |group| group[:num] }.join
end

#due_dateObject



42
43
44
45
46
47
48
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 42

def due_date
  begin
    Date.parse(digitable_line_without_dvs[-6..-1])
  rescue
    nil
  end
end

#global_validityObject



115
116
117
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 115

def global_validity
  send(bacen_module, digitable_line_without_dvs[0..2] + digitable_line_without_dvs[4..-1]) == verification_digit
end

#grouped_digitable_lineObject



119
120
121
122
123
124
125
126
127
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 119

def grouped_digitable_line
  clean_digitable_line.scan(/.{1,12}/).each_with_index.map do |group, index|
    {
      group: index,
      num: group[0..-2],
      dv: group[-1]
    }
  end
end

#groups_validityObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 103

def groups_validity
  grouped_digitable_line.map do |group|
    dv = send(bacen_module, group[:num]).to_s
    valid = dv.eql?(group[:dv])
    errors << "Campo #{group[:group]}" unless valid
    {
      group: group[:group],
      valid: valid
    }
  end
end

#identificationObject



71
72
73
74
75
76
77
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 71

def identification
  identification_str = JSON.parse(File.read(File.join( File.dirname(__FILE__), '../../data/Dealerships.json')))
                         .select { |d| d['code'] == clean_digitable_line[16..19] && d['segment'] == clean_digitable_line[1].to_i }&.dig(0,"dealership")
  return identification_str unless identification_str.nil?

  "Boleto de #{segment}"
end

#segmentObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 50

def segment
  case clean_digitable_line[1]
  when '1'
    'Prefeitura'
  when '2'
    'Saneamento'
  when '3'
    'Energia Elétrica e Gás'
  when '4'
    'Telefonia'
  when '5'
    'Órgãos Governamentais'
  when '6'
    'Empresas / Órgãos com CNPJ.'
  when '7'
    'Multa de trânsito'
  when '9'
    'Uso exclusivo do banco'
  end
end

#valid?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 94

def valid?
  global_validity && !groups_validity.map { |group| group[:valid] }.include?(false)
end

#valid_without_global?Boolean

Duplicate just for mantaining module methods

Returns:

  • (Boolean)


99
100
101
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 99

def valid_without_global?
  valid?
end

#valueObject



37
38
39
40
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 37

def value
  return nil unless valid?
  value_in_cents.to_f / 100
end

#value_in_centsObject



32
33
34
35
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 32

def value_in_cents
  return nil unless valid?
  digitable_line_without_dvs[4..14].to_i
end

#verification_digitObject



28
29
30
# File 'lib/verificador_boleto_br/slip/arrecadation/validity.rb', line 28

def verification_digit
  clean_digitable_line[3].to_i
end