Sha256: 9c4baf4e545e6bf8cb0bbb655cd14db6e9d1c62cd9f0811298fbdfa33a475e3d

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Correios
  module SRO
    class Tracker
      attr_accessor :user, :password
      attr_accessor :query_type, :result_mode
      attr_reader :object_numbers

      DEFAULT_OPTIONS = { query_type: :list, result_mode: :last }.freeze

      def initialize(options = {})
        DEFAULT_OPTIONS.merge(options).each do |attr, value|
          self.send("#{attr}=", value)
        end

        yield self if block_given?
        @object_numbers = []

        set_attributes_from_config!
      end

      def get(*object_numbers)
        @object_numbers = object_numbers
        response = web_service.request!
        objects = parser.objects(response)

        if @object_numbers.size == 1
          objects.values.first
        else
          objects
        end
      end

      private

      def set_attributes_from_config!
        [:user, :password].each do |attr|
          self.send("#{attr}=", Correios::SRO.send(attr)) unless self.send(attr)
        end
      end

      def web_service
        @web_service ||= Correios::SRO::WebService.new(self)
      end

      def parser
        @parser ||= Correios::SRO::Parser.new
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
correios-sro-xml-0.2.1 lib/correios/sro/tracker.rb