class ViaCep::SearchByAddress

Search an address

Attributes

city[R]
state[R]
street[R]

Public Class Methods

new(state:, city:, street:) click to toggle source
# File lib/via_cep/search_by_address.rb, line 8
def initialize(state:, city:, street:)
  @state = state
  @city = city
  @street = street

  valid?
  call_service
rescue JSON::ParserError, Net::HTTPBadRequest
  raise ViaCep::Errors::AddressNotFound
end

Private Instance Methods

call_service() click to toggle source
# File lib/via_cep/search_by_address.rb, line 26
def call_service
  request = HTTP.get(path: "#{state}/#{city}/#{street}")
  raise ViaCep::Errors::AddressNotFound unless HTTP.was_successful?(request)

  response = JSON.parse(request.body)
  raise ViaCep::Errors::AddressNotFound if response.length.eql?(0)

  define_attributes(response[0])
end
valid?() click to toggle source
# File lib/via_cep/search_by_address.rb, line 21
def valid?
  raise ViaCep::Errors::InvalidStateFormat unless ViaCep::Validators::State.valid?(state)
  raise ViaCep::Errors::InvalidAddressFormat unless city || state
end