Class: ViaCep::Address

Inherits:
Instance show all
Defined in:
lib/via_cep/address.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instance

#define_attributes

Constructor Details

#initialize(zipcode) ⇒ Address

Initialize an instance of ViaCep::Address and retrieve the CEP using the ViaCep WS API.

Retrieve a zipcode

ViaCep::Address.new('01001000')
#=> #<ViaCep::Address:0x0000558da60b3770 @zipcode="01001-000",
@street="Praca da Se", @complement="lado impar", @neighborhood="Se", @city="Sao Paulo", @state="SP",
@ibge="3550308", @gia="1004", @error=nil>

Retrieve a zipcode with a formatted string

ViaCep::Address.new('01001-000')
#=> #<ViaCep::Address:0x000055e5429aef98 @zipcode="01001-000", @street="Praca da Se", @complement="lado impar",
@neighborhood="Se", @city="Sao Paulo", @state="SP", @ibge="3550308", @gia="1004", @error=nil>

raise [ArgumentError] Error raised when zipcode is nil. raise [ViaCep::Errors::InvalidZipcodeFormat] Errorraised when zipcode format is invalid. raise [ViaCep::Errors::ZipcodeNotFound] Error raised when the external API is down or the CEP does not exist.

return [ViaCep::Address]



28
29
30
31
32
33
34
# File 'lib/via_cep/address.rb', line 28

def initialize(zipcode)
  @zipcode = zipcode
  valid?
  call_service
rescue JSON::ParserError, Net::HTTPBadRequest
  raise ViaCep::Errors::ZipcodeNotFound
end

Instance Attribute Details

#zipcodeObject (readonly)

Returns the value of attribute zipcode.



5
6
7
# File 'lib/via_cep/address.rb', line 5

def zipcode
  @zipcode
end