Class: Fdis2::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/fdis2/config.rb

Direct Known Subclasses

Facturacion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id_servicio, rfc, razon, regimen, doc_key_path, key_pass, doc_cer_path, production = false) ⇒ Config

Returns a new instance of Config.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fdis2/config.rb', line 6

def initialize(id_servicio, rfc, razon, regimen, doc_key_path, key_pass, doc_cer_path, production=false)
puts "---- Fdis:config:initialize"

  @id_servicio = id_servicio.to_s
  @rfc = rfc.to_s
  @razon = razon.to_s
  @regimen_fiscal = regimen
  @doc_key_path = doc_key_path.to_s
  @key_pass = key_pass.to_s
  @doc_cer_path = doc_cer_path
  @production = production

  puts "------ Fdis: Config inicial ---"
  puts "-- id servicio: #{@id_servicio}"
  puts "-- rfc: #{@rfc}"
  puts "-- razon: #{@razon}"
  puts "-- regimen_fiscal: #{@regimen_fiscal}"
  puts "-- key_path: #{@doc_key_path}"
  puts "-- cer_path: #{@doc_cer_path}"
  puts "-- production: #{@production}"

  key_to_pem 
  serial_number 
  cer_cadena

end

Instance Attribute Details

#cadenaObject (readonly)

Returns the value of attribute cadena.



4
5
6
# File 'lib/fdis2/config.rb', line 4

def cadena
  @cadena
end

#dev_tokenObject

Returns the value of attribute dev_token.



3
4
5
# File 'lib/fdis2/config.rb', line 3

def dev_token
  @dev_token
end

#doc_cer_pathObject

Returns the value of attribute doc_cer_path.



3
4
5
# File 'lib/fdis2/config.rb', line 3

def doc_cer_path
  @doc_cer_path
end

#doc_key_pathObject

Returns the value of attribute doc_key_path.



3
4
5
# File 'lib/fdis2/config.rb', line 3

def doc_key_path
  @doc_key_path
end

#key_passObject (readonly)

Returns the value of attribute key_pass.



4
5
6
# File 'lib/fdis2/config.rb', line 4

def key_pass
  @key_pass
end

#pemObject (readonly)

Returns the value of attribute pem.



4
5
6
# File 'lib/fdis2/config.rb', line 4

def pem
  @pem
end

#pem_cadenaObject (readonly)

Returns the value of attribute pem_cadena.



4
5
6
# File 'lib/fdis2/config.rb', line 4

def pem_cadena
  @pem_cadena
end

#production_tokenObject

Returns the value of attribute production_token.



3
4
5
# File 'lib/fdis2/config.rb', line 3

def production_token
  @production_token
end

#serialObject (readonly)

Returns the value of attribute serial.



4
5
6
# File 'lib/fdis2/config.rb', line 4

def serial
  @serial
end

Instance Method Details

#cer_cadenaObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fdis2/config.rb', line 63

def cer_cadena
  puts "---- Fdis:config:cer_cadena"

  file = File.read(@doc_cer_path)
  text_certificate = OpenSSL::X509::Certificate.new(file)
  cert_string = text_certificate.to_s
  cert_string.slice!("-----BEGIN CERTIFICATE-----")
  cert_string.slice!("-----END CERTIFICATE-----")
  cert_string.delete!("\n")
  @cadena = cert_string

end

#key_to_pemObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/fdis2/config.rb', line 33

def key_to_pem
  puts "---- Fdis:config:key_to_pem"

  @pem = %x[openssl pkcs8 -inform DER -in #{@doc_key_path} -passin pass:#{@key_pass}]
  # @pem = %x[openssl rsa -inform DER -in #{@doc_key_path} -passin pass:#{@key_pass}]
  @pem_cadena = @pem.clone
  @pem_cadena.slice!("-----BEGIN PRIVATE KEY-----")
  @pem_cadena.slice!("-----END PRIVATE KEY-----")
  @pem_cadena.delete!("\n")
end

#serial_numberObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fdis2/config.rb', line 44

def serial_number
  puts "---- Fdis:config:serial_number"

  response = %x[openssl x509 -inform DER -in #{@doc_cer_path} -noout -serial]
  d_begin = response.index(/\d/)
  number = (response[d_begin..-1]).chomp
  final_serial = ""

  number.each_char.with_index do |s, index|
    if (index + 1).even?
      final_serial << s
    end
  end

  @serial = final_serial
  
end