Class: Complicode::Generate

Inherits:
Object
  • Object
show all
Defined in:
lib/complicode/generate.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authorization_code, key, invoice_attributes = {}) ⇒ Generate

Returns a new instance of Generate

Options Hash (invoice_attributes):

  • :nit (String)
  • :number (String)
  • :amount (String)
  • :issue_date (String)

17
18
19
20
21
# File 'lib/complicode/generate.rb', line 17

def initialize(authorization_code, key, invoice_attributes = {})
  @invoice = Invoice.new(invoice_attributes)
  @authorization_code = authorization_code
  @key = key
end

Class Method Details

.call(*args) ⇒ String


6
7
8
# File 'lib/complicode/generate.rb', line 6

def self.call(*args)
  new(*args).send(:call)
end

Instance Method Details

#callString (private)


26
27
28
29
30
31
32
33
# File 'lib/complicode/generate.rb', line 26

def call
  partial_keys = Generators::PartialKeys.call(@key.dup, verification_digits)
  data = Generators::Data.call(@authorization_code, @invoice, partial_keys)
  encrypted_data = encrypt(data, encryption_key)
  ascii_sums = Generators::AsciiSums.call(encrypted_data, partial_keys.count)
  base64_data = Generators::Base64Data.call(ascii_sums, partial_keys)
  format(encrypt(base64_data, encryption_key))
end

#encrypt(data, encryption_key) ⇒ String (private)


48
49
50
# File 'lib/complicode/generate.rb', line 48

def encrypt(data, encryption_key)
  RC4.new(encryption_key).encrypt(data).unpack1("H*").upcase
end

#encryption_keyString (private)


36
37
38
# File 'lib/complicode/generate.rb', line 36

def encryption_key
  @encryption_key ||= @key + verification_digits
end

#format(code) ⇒ String (private)


54
55
56
# File 'lib/complicode/generate.rb', line 54

def format(code)
  code.scan(/.{2}/).join("-")
end

#verification_digitsString (private)


41
42
43
# File 'lib/complicode/generate.rb', line 41

def verification_digits
  @verification_digits ||= Generators::VerificationDigits.call(@invoice)
end