class Business::BR::CEP

Public Class Methods

new(opts = {}) click to toggle source
# File lib/business-br/cep.rb, line 20
def initialize(opts = {})
  @opts = opts || {}
end

Public Instance Methods

format(cep) click to toggle source
# File lib/business-br/cep.rb, line 41
def format(cep)
  if cep =~ /^(\d{5})-?(\d{3})$/
    "#{Regexp.last_match(1)}-#{Regexp.last_match(2)}"
  end
end
normalize(cep) click to toggle source
# File lib/business-br/cep.rb, line 35
def normalize(cep)
  if cep =~ /^(\d{5})-?(\d{3})$/
    "#{Regexp.last_match(1)}#{Regexp.last_match(2)}"
  end
end
region(cep) click to toggle source
# File lib/business-br/cep.rb, line 47
def region(cep)
  raise Exception, 'This cep is not valid' unless valid?(cep)

  @@regions[cep[0].to_i]
end
search_by(cep, provider: 'Postmon') click to toggle source
# File lib/business-br/cep.rb, line 64
def search_by(cep, provider: 'Postmon')
  if cep_provider = Business::BR::CEP::Providers.get_provider(provider)
    return cep_provider.search_by(cep)
  end

  nil
end
type(cep) click to toggle source
# File lib/business-br/cep.rb, line 53
def type(cep)
  cep = normalize(cep)
  suffix = cep.slice(5, 3).to_i
  if suffix < 900 then 'LOGRADOURO'
  elsif suffix < 960 then 'ESPECIAL'
  elsif suffix < 970 then 'PROMOCIONAIS'
  elsif suffix < 990 || suffix == 999 then 'CORREIOS'
  else 'CAIXAPOSTAL'
  end
end
valid?(cep) click to toggle source
# File lib/business-br/cep.rb, line 31
def valid?(cep)
  validate(cep)
end
validate(cep) click to toggle source
# File lib/business-br/cep.rb, line 24
def validate(cep)
  return false unless cep =~ /^\d{5}-?\d{3}$/
  return false unless cep.length == 8 || cep.length == 9

  true
end