Sha256: 0ca79aa551b214811863aef8dcc42534b604d2c08413d6e2079272aed707161b

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Brazil
  module Cep
    # Adapters class namespace
    module Adapters
      # autoload default adapters
      autoload :Base, "brazil_cep/adapters/base"
      autoload :Viacep, "brazil_cep/adapters/viacep"
      autoload :Postmon, "brazil_cep/adapters/postmon"
      autoload :RepublicaVirtual, "brazil_cep/adapters/republica_virtual"
      autoload :CorreiosWeb, "brazil_cep/adapters/correios_web"
      autoload :CepAberto, "brazil_cep/adapters/cep_aberto"

      # list of provider keys and classes
      PROVIDERS = {
        viacep: Viacep,
        postmon: Postmon,
        republica_virtual: RepublicaVirtual,
        correios_web: CorreiosWeb,
        cep_aberto: CepAberto
      }

      private_constant :PROVIDERS

      # get providers
      # @return [Hash] providers
      def self.providers
        PROVIDERS
      end

      # register a provider
      #
      # @example
      #   Brazil::Cep::Adapters.register(:custom_provider, Brazil::Cep::Adapters::CustomProvider)
      #
      # @param provider_key [Symbol] provider key
      # @param provider_class [Class] provider class
      def self.register(provider_key, provider_class)
        PROVIDERS[provider_key] = provider_class
      end

      # get provider instance
      # @param provider [Symbol] provider key
      # @return [Base] provider instance
      def self.get(provider = :viacep)
        PROVIDERS[provider].new
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
brazil-cep-0.5.0 lib/brazil_cep/adapters.rb