Class: Nfcom::Utils::Certificate
- Inherits:
-
Object
- Object
- Nfcom::Utils::Certificate
- Defined in:
- lib/nfcom/utils/certificate.rb
Instance Attribute Summary collapse
-
#cert ⇒ Object
readonly
Returns the value of attribute cert.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #cnpj ⇒ Object
- #dias_para_vencer ⇒ Object
- #expirado? ⇒ Boolean
-
#initialize(path, password = nil) ⇒ Certificate
constructor
A new instance of Certificate.
- #to_pem ⇒ Object
- #valido? ⇒ Boolean
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
#cert ⇒ Object (readonly)
Returns the value of attribute cert.
8 9 10 |
# File 'lib/nfcom/utils/certificate.rb', line 8 def cert @cert end |
#key ⇒ Object (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
#cnpj ⇒ Object
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_vencer ⇒ Object
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
23 24 25 |
# File 'lib/nfcom/utils/certificate.rb', line 23 def expirado? @cert.not_after < Time.now end |
#to_pem ⇒ Object
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
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 |