Sha256: 204e9fb60f992919be887a528aedac3ec6f2a3d7140cd40fc7f47ffe60ae7d06

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

require 'net/http'
require 'uri'

module Correios
  module CEP
    class WebService
      URL = "http://200.252.60.209/SigepCliente/AtendeClienteService"

      def initialize
        @uri = URI.parse(URL)
      end

      def request!(zipcode)
        http = build_http

        request = build_request zipcode
        Correios::CEP.log_request request, @uri.to_s

        response = http.request request
        Correios::CEP.log_response response

        response.body
      end

      private

      def build_http
        http = Net::HTTP.new(@uri.host, @uri.port)
        http.open_timeout = Correios::CEP.request_timeout
        http
      end

      def build_request(zipcode)
        request = Net::HTTP::Post.new(@uri.path)
        request["Content-Type"] = "text/xml; charset=utf-8"
        request.body = request_body zipcode
        request
      end

      def request_body(zipcode)
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
        "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cli=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">" +
           "<soapenv:Header />" +
           "<soapenv:Body>" +
              "<cli:consultaCEP>" +
                "<cep>#{zipcode}</cep>" +
              "</cli:consultaCEP>" +
           "</soapenv:Body>" +
        "</soapenv:Envelope>"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
correios-cep-0.2.0 lib/correios/cep/web_service.rb
correios-cep-0.1.4 lib/correios/cep/web_service.rb
correios-cep-0.1.3 lib/correios/cep/web_service.rb