Class: FE::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/facturacr/cli.rb,
lib/facturacr/cli/generate.rb

Defined Under Namespace

Classes: Generate

Instance Method Summary collapse

Instance Method Details

#check(key) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/facturacr/cli.rb', line 24

def check(key)
  FE::Utils.configure(options[:config_file])
  api = FE::Api.new
  document_status = api.get_document_status(key)
  ap document_status.to_h
  puts "  (check)=> API Logout"
  api.logout
end

#send_document(path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/facturacr/cli.rb', line 52

def send_document(path)
  FE::Utils.configure(options[:config_file])
  xml_document = FE::XmlDocument.new(DataProvider.new :file, path)
  document = xml_document.document
  signed_document = FE::SignedDocument.new(document,path)
  api = FE::Api.new
  payload = signed_document.payload
  if api.send_document(payload)
    puts "Document Sent".green
    puts "KEY: #{document.key}"
    puts "Wait 5 seconds before check..."
    sleep 5
    if document.is_a?(FE::ReceptionMessage)
      check_key = api.check_location.split("/").last
    else
      check_key = document.key
    end
    invoke :check, [check_key], :config_file=>options[:config_file]
  else
    puts "ERROR".red
    request = api.errors[:request]
    response =  request[:response]
    ap request
    puts "Code: #{response.code}".red
    puts "Cause: #{response.headers[:x_error_cause]}".red
    raise "Sending Document Error" 
  end  
  puts "  (send)=> API Logout"
  api.logout
end

#setup(path) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/facturacr/cli.rb', line 85

def setup(path)
  puts "\n\n SETUP FACTURACR \n\n"
  say "A tmp directory will be created at: #{path}"
  say "config.yml file will be copied to #{path}"
  say "data.yml file will be copied to #{path}"
  answer = ask("Are you sure you want to continue?", :yellow, limited_to: ["y","n"])
  if answer.downcase == "y"
    FileUtils.mkdir_p "#{path}/tmp"
    FileUtils.cp "#{FE.root}/resources/data.yml", "#{path}/tmp/data.yml"
    FileUtils.cp "#{FE.root}/config/config.yml", "#{path}/tmp/config.yml"
    say "Done.", :green
  else
    say "Ok. Bye", :green
  end
end

#sign(xml_in, xml_out) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/facturacr/cli.rb', line 38

def sign(xml_in, xml_out)
  start = Time.now
  FE::Utils.configure(options[:config_file])
  
  key_provider = FE::DataProvider.new(:file, FE.configuration.key_path)
  document_provider = FE::DataProvider.new(:file, xml_in)
  signer_args = {xml_provider: document_provider, key_provider: key_provider, pin: FE.configuration.key_password, output_path: xml_out }
  signer = FE::Signer.new signer_args
  signer.sign
  puts "Signature: #{Time.now - start} sec.".blue
end