Sha256: 7c6032c1af20e13fbbd0e63ad1346fb71e0753668bd22a49625113b823117ae6

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

require 'net/http'
require 'uri'

module Correios
  module SRO
    class WebService
      URL = "http://websro.correios.com.br/sro_bin/sroii_xml.eventos"
      QUERY_TYPES =  { list: "L", range: "F" }
      RESULT_MODES = { all:  "T", last:  "U" }

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

      def request!
        http = build_http

        request = build_request
        Correios::SRO.log_request request, @uri.to_s

        response = http.request(request)
        Correios::SRO.log_response response

        response.body
      end

      private

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

      def build_request
        request = Net::HTTP::Post.new(@uri.path)
        request.set_form_data(request_params)
        request
      end

      def request_params
        {
          Usuario: @tracker.user,
          Senha: @tracker.password,
          Tipo: QUERY_TYPES[@tracker.query_type],
          Resultado: RESULT_MODES[@tracker.result_mode],
          Objetos: @tracker.object_numbers.join
        }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
correios-sro-xml-0.3.1 lib/correios/sro/web_service.rb
correios-sro-xml-0.3.0 lib/correios/sro/web_service.rb
correios-sro-xml-0.2.1 lib/correios/sro/web_service.rb
correios-sro-xml-0.2.0 lib/correios/sro/web_service.rb