Class: StarlingTerminal::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/starling_terminal/statement.rb

Overview

Builds a Starling statement for output to the terminal

Constant Summary collapse

TRANSACTIONS_TABLE_HEADINGS =

The headings to be displayed in our table of transactions

['Narrative',
'Amount',
'Date and time',
'Source'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(access_token: ENV['STARLING_ACCESS_TOKEN']) ⇒ Statement

Returns a new instance of Statement.

Parameters:

  • access_token (String) (defaults to: ENV['STARLING_ACCESS_TOKEN'])

    The Starling Bank API access token

Raises:


14
15
16
17
# File 'lib/starling_terminal/statement.rb', line 14

def initialize(access_token: ENV['STARLING_ACCESS_TOKEN'])
  raise MissingAccessTokenError unless access_token
  @access_token = access_token
end

Instance Method Details

#generate(output: STDOUT) ⇒ #puts

Outputs a complete statement to the provided output

Parameters:

  • output (#puts) (defaults to: STDOUT)

    The output to print the statement to

Returns:

  • (#puts)

    the output

Raises:

  • (InvalidAccessTokenError)

    if the Starling Bank API access token provided is invalid

  • (Starling::Errors::ApiError)

    if something else goes wrong with the Starling Bank API


26
27
28
29
30
31
32
33
34
35
# File 'lib/starling_terminal/statement.rb', line 26

def generate(output: STDOUT)
  output.puts balance_line
  output.puts pending_transactions_line
  output.puts available_to_spend_line
  output.puts "#{transactions_table}\n\n"
  output
rescue Starling::Errors::ApiError => error
  raise InvalidAccessTokenError if [401, 403].include?(error.status)
  raise
end