Class: BrZipCode::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/br_zip_code/service.rb,
lib/br_zip_code/service/viacep.rb,
lib/br_zip_code/service/postmon.rb,
lib/br_zip_code/service/cep_livre.rb,
lib/br_zip_code/service/correio_control.rb,
lib/br_zip_code/service/republica_virtual.rb

Direct Known Subclasses

CepLivre, CorreioControl, Postmon, RepublicaVirtual, Viacep

Defined Under Namespace

Classes: CepLivre, CorreioControl, Postmon, RepublicaVirtual, Viacep

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip_code, no_timeout = false) ⇒ Service

Returns a new instance of Service.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/br_zip_code/service.rb', line 5

def initialize zip_code, no_timeout = false
  @zip_code = BrZipCode.strip(zip_code)

  return unless self.basic_valid?

  if no_timeout
    @response = HTTParty.get(final_url)
  else
    @response = HTTParty.get(final_url, timeout: BrZipCode.timeout)
  end

  @response_body = @response.response.body

  if self.valid?
    @response_body = JSON.parse(@response_body)
  end
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/br_zip_code/service.rb', line 3

def response
  @response
end

#response_bodyObject

Returns the value of attribute response_body.



3
4
5
# File 'lib/br_zip_code/service.rb', line 3

def response_body
  @response_body
end

#zip_codeObject

Returns the value of attribute zip_code.



3
4
5
# File 'lib/br_zip_code/service.rb', line 3

def zip_code
  @zip_code
end

Instance Method Details

#basic_valid?Boolean

Returns:

  • (Boolean)


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

def basic_valid?
  @zip_code.length == 8
end

#valid?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/br_zip_code/service.rb', line 27

def valid?
  not self.response.nil? and not self.response_body.nil? and not zip_code.nil?
end