Class: BrZipCode::Service::Viacep

Inherits:
BrZipCode::Service show all
Defined in:
lib/br_zip_code/service/viacep.rb

Instance Attribute Summary

Attributes inherited from BrZipCode::Service

#response, #response_body, #zip_code

Instance Method Summary collapse

Methods inherited from BrZipCode::Service

#basic_valid?, #initialize

Constructor Details

This class inherits a constructor from BrZipCode::Service

Instance Method Details

#base_urlObject



23
24
25
# File 'lib/br_zip_code/service/viacep.rb', line 23

def base_url
  "http://viacep.com.br/ws/"
end

#final_urlObject



19
20
21
# File 'lib/br_zip_code/service/viacep.rb', line 19

def final_url
  "#{base_url}#{self.zip_code}/json/"
end

#to_hashObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/br_zip_code/service/viacep.rb', line 4

def to_hash
  return nil unless self.valid?

  {
    street: self.response_body['logradouro'],
    district: self.response_body['bairro'],
    city: self.response_body['localidade'],
    state: self.response_body['uf']
  }
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/br_zip_code/service/viacep.rb', line 15

def valid?
  super and self.response.code == 200 and not self.response_body.include? 'erro'
end