Class: Nfcom::Utils::Certificate

Inherits:
Object
  • Object
show all
Defined in:
lib/nfcom/utils/certificate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, password = nil) ⇒ Certificate

Returns a new instance of Certificate.



10
11
12
13
14
# File 'lib/nfcom/utils/certificate.rb', line 10

def initialize(path, password = nil)
  @path = path
  @password = password
  carregar_certificado
end

Instance Attribute Details

#certObject (readonly)

Returns the value of attribute cert.



8
9
10
# File 'lib/nfcom/utils/certificate.rb', line 8

def cert
  @cert
end

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/nfcom/utils/certificate.rb', line 8

def key
  @key
end

Instance Method Details

#cnpjObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nfcom/utils/certificate.rb', line 33

def cnpj
  # Extrai CNPJ do subject do certificado
  # Formato pode ser: CN=12345678000100 ou CN=EMPRESA:12345678000100
  subject = @cert.subject.to_s

  # Tenta formato direto: CN=12345678000100
  match = subject.match(/CN=(\d{14})/)
  return match[1] if match

  # Tenta formato com nome: CN=EMPRESA:12345678000100
  match = subject.match(/CN=[^:]+:(\d{14})/)
  return match[1] if match

  # Tenta buscar CNPJ em qualquer lugar do subject
  match = subject.match(/(\d{14})/)
  return match[1] if match

  nil
end

#dias_para_vencerObject



27
28
29
30
31
# File 'lib/nfcom/utils/certificate.rb', line 27

def dias_para_vencer
  return 0 if expirado?

  ((@cert.not_after - Time.now) / 86_400).to_i
end

#expirado?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/nfcom/utils/certificate.rb', line 23

def expirado?
  @cert.not_after < Time.now
end

#to_pemObject



53
54
55
56
57
58
# File 'lib/nfcom/utils/certificate.rb', line 53

def to_pem
  {
    cert: @cert.to_pem,
    key: @key.to_pem
  }
end

#valido?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/nfcom/utils/certificate.rb', line 16

def valido?
  return false if @cert.nil? || @key.nil?
  return false if expirado?

  true
end