Class: ViaCep::Address

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zipcode) ⇒ Address

Returns a new instance of Address.



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

def initialize(zipcode)
  begin
    @response = HTTParty.get("https://viacep.com.br/ws/#{zipcode}/json/")
  raise ArgumentError, 'Invalid zipcode format' unless has_valid_format?(zipcode)
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



3
4
5
# File 'lib/via_cep/address.rb', line 3

def response
  @response
end

Instance Method Details

#has_valid_format?(zipcode) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/via_cep/address.rb', line 12

def has_valid_format?(zipcode)
  zipcode.to_s.match(/\d{5}(-)\d{3}\z/) || zipcode.to_s.match(/\d{8}/)
end