Class: FocusNfe::Configuration
- Inherits:
-
Object
- Object
- FocusNfe::Configuration
- Defined in:
- lib/focus_nfe/configuration.rb
Overview
Guarda as opções de uso da gem: os dois tokens da Focus NFe (+token_empresa+,
que identifica a empresa emitente/consultada, e token_conta, usado nas
consultas auxiliares e na gestão de empresas), o ambiente (que resolve a URL
base), timeouts, logger, adaptador HTTP e cabeçalhos extras. Serve tanto ao
modo global (FocusNfe.configure) quanto ao Client explícito (multi-empresa).
Constant Summary collapse
- BASE_URLS =
Returns ambiente => URL base da API (sem o prefixo /v2).
{ producao: "https://api.focusnfe.com.br", homologacao: "https://homologacao.focusnfe.com.br" }.freeze
- ESCOPOS_TOKEN =
Returns escopo => atributo de token correspondente.
{ empresa: :token_empresa, conta: :token_conta }.freeze
- DEFAULT_ENVIRONMENT =
Returns ambiente padrão quando nenhum é informado.
:homologacao- DEFAULT_TIMEOUT =
Returns timeout de leitura padrão, em segundos.
30- DEFAULT_OPEN_TIMEOUT =
Returns timeout de conexão padrão, em segundos.
10
Instance Attribute Summary collapse
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http_adapter ⇒ Object
Returns the value of attribute http_adapter.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#token_conta ⇒ Object
Returns the value of attribute token_conta.
-
#token_empresa ⇒ Object
Returns the value of attribute token_empresa.
Instance Method Summary collapse
-
#base_url ⇒ String
URL base correspondente ao ambiente atual.
-
#initialize(token_empresa: nil, token_conta: nil, environment: DEFAULT_ENVIRONMENT, timeout: DEFAULT_TIMEOUT, open_timeout: DEFAULT_OPEN_TIMEOUT, logger: nil, http_adapter: nil, headers: {}) ⇒ Configuration
constructor
A new instance of Configuration.
-
#token_de(escopo) ⇒ String?
Token correspondente ao escopo.
-
#validate! ⇒ self
Valida o ambiente e a presença de ao menos um token, falhando cedo quando a configuração é inutilizável em qualquer escopo.
-
#validate_token!(escopo) ⇒ self
Valida o ambiente e a presença do token de um escopo específico, no momento em que um recurso daquele escopo é efetivamente usado.
Constructor Details
#initialize(token_empresa: nil, token_conta: nil, environment: DEFAULT_ENVIRONMENT, timeout: DEFAULT_TIMEOUT, open_timeout: DEFAULT_OPEN_TIMEOUT, logger: nil, http_adapter: nil, headers: {}) ⇒ Configuration
Returns a new instance of Configuration.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/focus_nfe/configuration.rb', line 39 def initialize(token_empresa: nil, token_conta: nil, environment: DEFAULT_ENVIRONMENT, timeout: DEFAULT_TIMEOUT, open_timeout: DEFAULT_OPEN_TIMEOUT, logger: nil, http_adapter: nil, headers: {}) @token_empresa = token_empresa @token_conta = token_conta @environment = environment @timeout = timeout @open_timeout = open_timeout @logger = logger @http_adapter = http_adapter @headers = headers end |
Instance Attribute Details
#environment ⇒ Object
Returns the value of attribute environment.
26 27 28 |
# File 'lib/focus_nfe/configuration.rb', line 26 def environment @environment end |
#headers ⇒ Object
Returns the value of attribute headers.
26 27 28 |
# File 'lib/focus_nfe/configuration.rb', line 26 def headers @headers end |
#http_adapter ⇒ Object
Returns the value of attribute http_adapter.
26 27 28 |
# File 'lib/focus_nfe/configuration.rb', line 26 def http_adapter @http_adapter end |
#logger ⇒ Object
Returns the value of attribute logger.
26 27 28 |
# File 'lib/focus_nfe/configuration.rb', line 26 def logger @logger end |
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
26 27 28 |
# File 'lib/focus_nfe/configuration.rb', line 26 def open_timeout @open_timeout end |
#timeout ⇒ Object
Returns the value of attribute timeout.
26 27 28 |
# File 'lib/focus_nfe/configuration.rb', line 26 def timeout @timeout end |
#token_conta ⇒ Object
Returns the value of attribute token_conta.
26 27 28 |
# File 'lib/focus_nfe/configuration.rb', line 26 def token_conta @token_conta end |
#token_empresa ⇒ Object
Returns the value of attribute token_empresa.
26 27 28 |
# File 'lib/focus_nfe/configuration.rb', line 26 def token_empresa @token_empresa end |
Instance Method Details
#base_url ⇒ String
Returns URL base correspondente ao ambiente atual.
54 55 56 57 |
# File 'lib/focus_nfe/configuration.rb', line 54 def base_url validate_environment! BASE_URLS.fetch(environment) end |
#token_de(escopo) ⇒ String?
Returns token correspondente ao escopo.
61 62 63 |
# File 'lib/focus_nfe/configuration.rb', line 61 def token_de(escopo) public_send(ESCOPOS_TOKEN.fetch(escopo)) end |
#validate! ⇒ self
Valida o ambiente e a presença de ao menos um token, falhando cedo quando a configuração é inutilizável em qualquer escopo.
70 71 72 73 74 75 |
# File 'lib/focus_nfe/configuration.rb', line 70 def validate! validate_environment! return self if ESCOPOS_TOKEN.keys.any? { |escopo| token_presente?(escopo) } raise Errors::ConfigurationError, "informe token_empresa e/ou token_conta" end |
#validate_token!(escopo) ⇒ self
Valida o ambiente e a presença do token de um escopo específico, no momento em que um recurso daquele escopo é efetivamente usado.
83 84 85 86 87 88 |
# File 'lib/focus_nfe/configuration.rb', line 83 def validate_token!(escopo) validate_environment! return self if token_presente?(escopo) raise Errors::ConfigurationError, "#{ESCOPOS_TOKEN.fetch(escopo)} é obrigatório para esta operação" end |