class Correios::CEP::WebService

Constants

BODY_TEMPLATE
HTTP_HEADERS

Public Instance Methods

request(zipcode) click to toggle source
# File lib/correios/cep/web_service.rb, line 23
def request(zipcode)
  response = http.post(Correios::CEP.web_service_url,
                       body: request_body(zipcode),
                       ssl_context: ssl_context)
  response.body.to_s
end

Private Instance Methods

http() click to toggle source
# File lib/correios/cep/web_service.rb, line 32
def http
  http = HTTP.headers(HTTP_HEADERS)
             .timeout(
                connect: Correios::CEP.request_timeout,
                read: Correios::CEP.request_timeout
             )
  http = http_proxy(http)
  http = http_log(http)

  http
end
http_log(http) click to toggle source
# File lib/correios/cep/web_service.rb, line 50
def http_log(http)
  return http if Correios::CEP.logger.nil?

  http.use(logging: { logger: Correios::CEP.logger })
end
http_proxy(http) click to toggle source
# File lib/correios/cep/web_service.rb, line 44
def http_proxy(http)
  return http if proxy_uri.nil?

  http.via(proxy_uri.host, proxy_uri.port)
end
proxy_uri() click to toggle source
# File lib/correios/cep/web_service.rb, line 66
def proxy_uri
  return nil if Correios::CEP.proxy_url.nil?

  @proxy_uri ||= URI.parse(Correios::CEP.proxy_url)
end
request_body(zipcode) click to toggle source
# File lib/correios/cep/web_service.rb, line 56
def request_body(zipcode)
  BODY_TEMPLATE % { zipcode: zipcode }
end
ssl_context() click to toggle source
# File lib/correios/cep/web_service.rb, line 60
def ssl_context
  context = OpenSSL::SSL::SSLContext.new
  context.verify_mode = OpenSSL::SSL::VERIFY_NONE
  context
end