Class: ViaCep::Address
- Inherits:
-
Object
- Object
- ViaCep::Address
- Defined in:
- lib/via_cep/address.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #has_valid_format?(zipcode) ⇒ Boolean
-
#initialize(zipcode) ⇒ Address
constructor
A new instance of Address.
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
#response ⇒ Object (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
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 |