Class: BankScrap::Exporter::Json

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/bankscrap/exporters/json.rb

Instance Method Summary collapse

Constructor Details

#initialize(account) ⇒ Json

Returns a new instance of Json.



9
10
11
# File 'lib/bankscrap/exporters/json.rb', line 9

def initialize()
  @account = 
end

Instance Method Details

#check_array_class(data, tclass) ⇒ Object



33
34
35
# File 'lib/bankscrap/exporters/json.rb', line 33

def check_array_class(data, tclass)
  data.all? { |x| x.is_a? tclass }
end

#get_filename(data, output) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bankscrap/exporters/json.rb', line 13

def get_filename(data, output)
  if output
    if output == '-'
      '/dev/stdout'
    else
      output
    end
  else
    if check_array_class(data, Bankscrap::Transaction)
      'transactions.json'
    elsif check_array_class(data, Bankscrap::Account)
      'accounts.json'
    elsif check_array_class(data, Bankscrap::Loan)
      'loans.json'
    elsif check_array_class(data, Bankscrap::Card)
      'cards.json'
    end
  end
end

#write_to_file(data, output) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bankscrap/exporters/json.rb', line 37

def write_to_file(data, output)
  output = get_filename(data, output)
  json_array = []
  if check_array_class(data, Bankscrap::Transaction)
    data.each do |line|
      array = line.to_a
      hash = { date: array[0], description: array[1], amount: array[2] }
      json_array << hash
    end
    json_hash = { account: { description: @account.description, iban: @account.iban }, transactions: json_array }
  elsif check_array_class(data, Bankscrap::Account)
    data.each do |line|
      array = line.to_a

      hash = { iban: array[1], name: array[2], description: array[3], amount: array[4] }
      json_array << hash
    end

    json_hash = { accounts: json_array }
  elsif check_array_class(data, Bankscrap::Loan)
    data.each do |line|
      array = line.to_a

      hash = { id: array[0], name: array[1], description: array[2], amount: array[3] }
      json_array << hash
    end

    json_hash = { loans: json_array }
  elsif check_array_class(data, Bankscrap::Card)
    data.each do |line|
      array = line.to_a

      hash = { id: array[0], name: array[1], description: array[2], pan: array[3], amount: array[4], avaliable: array[5], isCredit: array[6] }
      json_array << hash
    end

    json_hash = { cards: json_array }
  end
  File.open(output, 'w') do |f|
    f.write(JSON.pretty_generate(json_hash) + "\n")
  end
  if check_array_class(data, Bankscrap::Transaction)
    say "Transactions for: #{@account.description} (#{@account.iban}) exported to #{output}", :cyan
  elsif check_array_class(data, Bankscrap::Account)
    say "Accounts exported to #{output}", :cyan
  elsif check_array_class(data, Bankscrap::Loan)
    say "Loans exported to #{output}", :cyan
  elsif check_array_class(data, Bankscrap::Card)
    say "Cards exported to #{output}", :cyan
  end
end