Class: BlendSpreadsheetLoanGenerator::Generate

Inherits:
Dry::CLI::Command
  • Object
show all
Includes:
CsvConcern, SpreadsheetConcern
Defined in:
lib/blend_spreadsheet_loan_generator/generate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loanObject

Returns the value of attribute loan.



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

def loan
  @loan
end

Instance Method Details

#call(amount:, duration:, rate:, **options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/blend_spreadsheet_loan_generator/generate.rb', line 25

def call(amount:, duration:, rate:, **options)
  begin
    session = GoogleDrive::Session.from_config(
      File.join(ENV['SPREADSHEET_LOAN_GENERATOR_DIR'], 'config.json')
    )
  rescue StandardError => e
    if ENV['SPREADSHEET_LOAN_GENERATOR_DIR'].blank?
      puts 'please set SPREADSHEET_LOAN_GENERATOR_DIR'
    else
      puts 'Cannot connect to google drive. Did you run slg init CLIENT_ID CLIENT_SECRET ?'
    end
    return
  end

  @loan = Loan.new(
    amount: amount,
    duration: duration,
    period_duration: options.fetch(:period_duration),
    rate: rate,
    due_on: options.fetch(:due_on),
    deferred_and_capitalized: options.fetch(:deferred_and_capitalized),
    deferred: options.fetch(:deferred),
    type: options.fetch(:type),
    interests_type: options.fetch(:interests_type),
    starting_capitalized_interests: options.fetch(:starting_capitalized_interests),
    starting_capitalized_fees: options.fetch(:starting_capitalized_fees),
    fees_rate: options.fetch(:fees_rate)
  )

  spreadsheet = session.create_spreadsheet(loan.name)
  worksheet = spreadsheet.add_worksheet(loan.type, loan.duration + 2, columns.count + 1, index: 0)

  @formula = Formula.new(loan: loan)

  apply_formulas(worksheet: worksheet)
  apply_formats(worksheet: worksheet)

  worksheet.save
  worksheet.reload

  generate_csv(worksheet: worksheet, target_path: options.fetch(:target_path))

  puts worksheet.human_url
end