Sha256: 6cfad3c25f63f7e81f0447bdbb09677cdd21bb5cd1bc5c688eb9545e9d72c966

Contents?: true

Size: 818 Bytes

Versions: 4

Compression:

Stored size: 818 Bytes

Contents

module Correios
  module CEP
    class AddressFinder
      def initialize(args = {})
        @web_service = args.fetch(:web_service, Correios::CEP::WebService.new)
        @parser = args.fetch(:parser, Correios::CEP::Parser.new)
      end

      def get(zipcode)
        zipcode = zipcode.to_s.strip
        validate(zipcode)

        response = web_service.request(zipcode)
        parser.address(response)
      end

      def self.get(zipcode, args = {})
        self.new(args).get(zipcode)
      end

      private

      attr_reader :web_service, :parser

      def validate(zipcode)
        raise ArgumentError.new('zipcode is required') if zipcode.empty?
        raise ArgumentError.new('zipcode in invalid format (valid format: 00000-000)') unless zipcode.match(/\A\d{5}-?\d{3}\z/)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
correios-cep-0.6.2 lib/correios/cep/address_finder.rb
correios-cep-0.6.1 lib/correios/cep/address_finder.rb
correios-cep-0.6.0 lib/correios/cep/address_finder.rb
correios-cep-0.5.1 lib/correios/cep/address_finder.rb