class BankSlip::Barcode

Public Class Methods

new(value:, identification_code:, payment_date:, segment: 1, effective_reference: 6, free_digits: 0, product: 8) click to toggle source

> options = {value: 1, identification_code: 2, document_number: 10, payment_date: Date.new(2010, 3, 10)} > barcode = BankSlip::Barcode.new(options)

# File lib/bank_slip/barcode.rb, line 26
def initialize(value:, identification_code:, payment_date:,
               segment: 1, effective_reference: 6,
               free_digits: 0, product: 8)
  @value               = value
  @segment             = segment
  @identification_code = identification_code
  @payment_date        = payment_date
  @product             = product
  @effective_reference = effective_reference
  @free_digits         = free_digits
  fail "identification_code #{identification_code} too long, #{identification_length} characters is the maximum allowed, for the segment #{segment}"  unless valid_identification_code?
end

Public Instance Methods

digits() click to toggle source

> barcode.digits

> “81640000000000100022010031000000000000000010”

# File lib/bank_slip/barcode.rb, line 41
def digits
  @digits ||= numbers.insert(3, digit(numbers).to_s)
end
numerical_representation() click to toggle source

> barcode.numerical_representation

> [“81640000000 5”, “00010002201 1”, “00310000000 3”, “00000000010 9”]

# File lib/bank_slip/barcode.rb, line 47
def numerical_representation
  @numerical_representation ||= digits.scan(/\d{11}/).collect {|p| "#{p} #{digit(p)}" }
end

Private Instance Methods

digit(code_number) click to toggle source
# File lib/bank_slip/barcode.rb, line 83
def digit(code_number)
  d = BankSlip::CheckDigit.new(number: code_number)
  d.calc
end
free_digits_leading_zeros() click to toggle source
# File lib/bank_slip/barcode.rb, line 78
def free_digits_leading_zeros
  length = 21 - identification_length
  leading_zeros(@free_digits, length)
end
identification_length() click to toggle source

The Indentification code is defined by the segment Use 4 digits (FEBRABAN code) for the segments:

1. Prefeituras;
2. Saneamento;
3. Energia Elétrica e Gás;
4. Telecomunicações;
5. Órgãos Governamentais;
6. Carnes e Assemelhados ou demais

Use 8 digits (CNPJ) for the segments:

7. Multas de transito
9. Uso exclusivo do banco
# File lib/bank_slip/barcode.rb, line 100
def identification_length
  %w{7 9}.include?(@segment.to_s) ? 8 : 4
end
leading_zeros(n, length = 11) click to toggle source
# File lib/bank_slip/barcode.rb, line 70
def leading_zeros(n, length = 11)
  "%0#{length}d" % n
end
mask_id() click to toggle source
# File lib/bank_slip/barcode.rb, line 74
def mask_id
  leading_zeros(@identification_code, identification_length)
end
numbers() click to toggle source
# File lib/bank_slip/barcode.rb, line 57
def numbers
  @numbers ||= [
    @product,
    @segment,
    @effective_reference,
    leading_zeros(@value),
    mask_id,
    @payment_date.strftime('%Y%m%d'),
    free_digits_leading_zeros
  ]
  .join('')
end
valid_identification_code?() click to toggle source
# File lib/bank_slip/barcode.rb, line 53
def valid_identification_code?
  @identification_code.to_s.size.eql?(identification_length)
end