Class: Business::BR::CEP
- Inherits:
-
Object
- Object
- Business::BR::CEP
- Defined in:
- lib/business-br/cep/providers.rb,
lib/business-br/cep/providers/base.rb,
lib/business-br/cep/providers/postmon.rb,
lib/business-br/cep/providers/republicavirtual.rb,
lib/business-br/cep.rb
Defined Under Namespace
Classes: Providers
Constant Summary collapse
- @@regions =
['SP'], ['SP'], %w[RJ ES], ['MG'], %w[BA SE], %w[PE AL PB RN], %w[CE PI MA PA AM AC AP RR], %w[DF GO TO MT MG RO], %w[PR SC], ['RS']
Instance Method Summary collapse
- #format(cep) ⇒ Object
-
#initialize(opts = {}) ⇒ CEP
constructor
A new instance of CEP.
- #normalize(cep) ⇒ Object
- #region(cep) ⇒ Object
- #search_by(cep, provider: 'Postmon') ⇒ Object
- #type(cep) ⇒ Object
- #valid?(cep) ⇒ Boolean
- #validate(cep) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ CEP
Returns a new instance of CEP.
20 21 22 |
# File 'lib/business-br/cep.rb', line 20 def initialize(opts = {}) @opts = opts || {} end |
Instance Method Details
#format(cep) ⇒ Object
41 42 43 44 45 |
# 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) ⇒ Object
35 36 37 38 39 |
# 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) ⇒ Object
47 48 49 50 51 |
# 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') ⇒ Object
64 65 66 67 68 69 70 |
# 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) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# 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) ⇒ Boolean
31 32 33 |
# File 'lib/business-br/cep.rb', line 31 def valid?(cep) validate(cep) end |
#validate(cep) ⇒ Object
24 25 26 27 28 29 |
# 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 |