Class: Nfcom::Builder::Qrcode

Inherits:
Object
  • Object
show all
Defined in:
lib/nfcom/builder/qrcode.rb

Overview

Gera a URL e SVG do QR Code para consulta da NF-COM.

Esta classe cria a URL que será codificada em um QR Code, permitindo que os clientes validem a NF-COM diretamente no portal da SEFAZ.

A URL gerada contém os seguintes parâmetros:

  • chNFCom: Chave de acesso da NF-COM

  • tpAmb: Código do ambiente (1 = produção, 2 = homologação)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chave, ambiente) ⇒ Qrcode

Inicializa o gerador de QR Code

Parameters:

  • chave (String)

    chave de acesso da NF-COM

  • ambiente (Symbol)

    :producao ou :homologacao

Raises:

  • (ArgumentError)

    se algum argumento estiver ausente ou inválido



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nfcom/builder/qrcode.rb', line 30

def initialize(chave, ambiente)
  raise ArgumentError, 'Chave de acesso não pode ser vazia' if chave.nil? || chave.strip.empty?
  unless Nfcom::Validators::SchemaValidator.valido_por_schema?(chave, :er3)
    raise ArgumentError, "Chave de acesso inválida: #{chave}"
  end

  raise ArgumentError, 'Ambiente deve ser :producao ou :homologacao' unless %i[producao
                                                                               homologacao].include?(ambiente)

  @chave = chave
  @ambiente = ambiente
end

Instance Attribute Details

#ambienteSymbol (readonly)

Returns ambiente :producao ou :homologacao.

Returns:

  • (Symbol)

    ambiente :producao ou :homologacao



22
23
24
# File 'lib/nfcom/builder/qrcode.rb', line 22

def ambiente
  @ambiente
end

#chaveString (readonly)

Returns chave de acesso da NF-COM.

Returns:

  • (String)

    chave de acesso da NF-COM



19
20
21
# File 'lib/nfcom/builder/qrcode.rb', line 19

def chave
  @chave
end

Instance Method Details

#gerar_qrcode_svgString

Gera a imagem SVG do QR Code

Returns:

  • (String)

    SVG do QR Code



56
57
58
59
60
61
62
63
64
65
# File 'lib/nfcom/builder/qrcode.rb', line 56

def gerar_qrcode_svg
  qr = RQRCode::QRCode.new(gerar_url)
  qr.as_svg(
    offset: 0,
    color: '000',
    shape_rendering: 'crispEdges',
    module_size: 6,
    standalone: true
  )
end

#gerar_urlString

Gera a URL do QR Code

Returns:

  • (String)

    URL completa



46
47
48
49
50
51
# File 'lib/nfcom/builder/qrcode.rb', line 46

def gerar_url
  base_url = 'https://dfe-portal.svrs.rs.gov.br/nfcom/qrcode'
  tp_amb = ambiente == :homologacao ? 2 : 1

  "#{base_url}?#{URI.encode_www_form(chNFCom: chave, tpAmb: tp_amb)}"
end