Class: OFX::Statement::Output::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ofx/statement/output/base.rb

Direct Known Subclasses

CreditCard, CurrentAccount

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.trntype_hashObject

See OFX 2.0.3 spec, section 11.4.3.1 “Transaction types used in <TRNTYPE>”



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ofx/statement/output/base.rb', line 9

def trntype_hash
  @trntype_hash ||= {
    :debit => "DEBIT",
    :credit => "CREDIT",
    :interest => "INT",
    :dividend => "DIV",
    :fee => "FEE", 
    :service_charge => "SRVCHG", 
    :deposit => "DEP", 
    :atm => "ATM", 
    :point_of_sale => "POS", 
    :transfer => "XFER", 
    :cheque => "CHECK", 
    :check => "CHECK",
    :payment => "PAYMENT", 
    :cash => "CASH", 
    :direct_deposit => "DIRECTDEP",
    :direct_debit => "DIRECTDEBIT", 
    :repeating_payment => "REPEATPMT",
    :standing_order => "REPEATPMT", 
    :other => "OTHER"
  }
end

Instance Method Details

#create_builder(format = :ofx2) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/ofx/statement/output/base.rb', line 119

def create_builder(format = :ofx2)
  case format
  when :ofx1
    OFX::Statement::Output::Builder::OFX1.new(:indent => 2)
  when :ofx2
    OFX::Statement::Output::Builder::OFX2.new(:indent => 2)
  end
end

#fitid_hashObject



51
52
53
# File 'lib/ofx/statement/output/base.rb', line 51

def fitid_hash
  @fitid_hash ||= {}
end

#generate_fitid(time) ⇒ Object



60
61
62
63
64
65
# File 'lib/ofx/statement/output/base.rb', line 60

def generate_fitid(time)
  date = time_to_ofx_dta(time)
  suffix = fitid_hash[date].nil? ? 1 : fitid_hash[date] + 1
  fitid_hash[date] = suffix
  "#{date}#{suffix}"
end

#generate_trntype(trntype) ⇒ Object

Raises:



67
68
69
70
71
# File 'lib/ofx/statement/output/base.rb', line 67

def generate_trntype(trntype)
  output = OFX::Statement::Output::Base.trntype_hash[trntype]
  raise UnknownTrntype, trntype unless output
  output
end

#ledger_balance_block(node, statement) ⇒ Object



93
94
95
96
97
98
# File 'lib/ofx/statement/output/base.rb', line 93

def ledger_balance_block(node, statement)
  node.LEDGERBAL do |ledgerbal|
    ledgerbal.BALAMT statement.ledger_balance
    ledgerbal.DTASOF time_to_ofx_dta(statement.date)
  end
end

#ofx_block(node) ⇒ Object



73
74
75
76
77
# File 'lib/ofx/statement/output/base.rb', line 73

def ofx_block(node)
  return node.OFX unless block_given?
  
  node.OFX { |child| yield(child) }
end

#serialise(statement, format = :ofx2) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ofx/statement/output/base.rb', line 34

def serialise(statement, format = :ofx2)
  builder = create_builder(format)
  builder.ofx_stanza!
  ofx_block(builder) do |ofx|
    signon_block(ofx, statement) do |signon|
      message_set_block(signon) do |message_set|
        statement_block(message_set, statement) do |stmnt|
          statement.transactions.each do |transaction|
            transaction_block(stmnt, transaction)
          end
        end
      end
    end
  end
  builder.target!
end

#signon_block(node, statement) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ofx/statement/output/base.rb', line 79

def signon_block(node, statement)
  node.SIGNONMSGSRSV1 do |signonmsgrsv1|
    signonmsgrsv1.SONRS do |sonrs|
      sonrs.STATUS do |status|
        status.CODE "0"
        status.SEVERITY "INFO"
      end
      sonrs.DTSERVER time_to_ofx_dta(statement.server_response_time, true)
      sonrs.LANGUAGE statement.language
    end
    yield(signonmsgrsv1) if block_given?
  end
end

#time_to_ofx_dta(timeobj, extended = false) ⇒ Object



55
56
57
58
# File 'lib/ofx/statement/output/base.rb', line 55

def time_to_ofx_dta(timeobj, extended=false)
  fmt = '%Y%m%d' + (extended ? '%H%M%S' : '')
  timeobj.strftime(fmt)
end

#transaction_block(node, transaction) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/ofx/statement/output/base.rb', line 108

def transaction_block(node, transaction)
  node.STMTTRN do |stmttrn|
    stmttrn.TRNTYPE generate_trntype(transaction.trntype)
    stmttrn.DTPOSTED time_to_ofx_dta(transaction.date)
    stmttrn.TRNAMT transaction.amount
    stmttrn.FITID transaction.fitid
    stmttrn.NAME transaction.name
    stmttrn.MEMO transaction.memo if transaction.has_memo?
  end
end

#transaction_list(node, statement) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/ofx/statement/output/base.rb', line 100

def transaction_list(node, statement)
  node.BANKTRANLIST do |banktranlist|
    banktranlist.DTSTART time_to_ofx_dta(statement.start_date)
    banktranlist.DTEND time_to_ofx_dta(statement.end_date)
    yield(banktranlist) if block_given?
  end
end