Class: BankScrap::Exporter::Csv
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- BankScrap::Exporter::Csv
- Includes:
- Thor::Actions
- Defined in:
- lib/bankscrap/exporters/csv.rb
Instance Method Summary collapse
- #check_array_class(data, tclass) ⇒ Object
- #get_filename(data, output) ⇒ Object
-
#initialize(account) ⇒ Csv
constructor
A new instance of Csv.
- #write_to_file(data, output) ⇒ Object
Constructor Details
#initialize(account) ⇒ Csv
Returns a new instance of Csv.
9 10 11 |
# File 'lib/bankscrap/exporters/csv.rb', line 9 def initialize(account) @account = account end |
Instance Method Details
#check_array_class(data, tclass) ⇒ Object
33 34 35 |
# File 'lib/bankscrap/exporters/csv.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/csv.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 |
# File 'lib/bankscrap/exporters/csv.rb', line 37 def write_to_file(data, output) output = get_filename(data, output) if check_array_class(data, Bankscrap::Transaction) CSV.open(output, 'wb') do |csv| csv << %w[Date Description Amount] data.each { |line| csv << line.to_a } end say "Transactions for: #{@account.description} (#{@account.iban}) exported to #{output}", :cyan elsif check_array_class(data, Bankscrap::Account) CSV.open(output, 'wb') do |csv| csv << %w[Id Iban Name Description Bank Balance] data.each { |line| csv << line.to_a } end say "Accounts exported to #{output}", :cyan elsif check_array_class(data, Bankscrap::Loan) CSV.open(output, 'wb') do |csv| csv << %w[Id Name Description Amount] data.each { |line| csv << line.to_a } end say "Accounts exported to #{output}", :cyan elsif check_array_class(data, Bankscrap::Card) CSV.open(output, 'wb') do |csv| csv << %w[Id Name Description Pan Amount Avaliable Is_credit] data.each { |line| csv << line.to_a } end say "Accounts exported to #{output}", :cyan end end |