Class: OFX::Handler

Inherits:
Ox::Sax
  • Object
show all
Defined in:
lib/ofx/handler.rb

Constant Summary collapse

TRANSACTION_ATTRS =
[:TRNTYPE, :DTPOSTED, :TRNAMT, :FITID, :NAME]
BALANCE_ATTRS =
[:BALAMT]
ATTRS_MAP =
{ TRNTYPE: :as_s, DTPOSTED: :as_time, BALAMT: :as_f,
TRNAMT: :as_f, FITID: :as_s, NAME: :as_s }

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ Handler

Returns a new instance of Handler.



9
10
11
# File 'lib/ofx/handler.rb', line 9

def initialize(parser)
  @parser = parser
end

Instance Method Details

#end_element(name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ofx/handler.rb', line 30

def end_element(name)
  case name
  when :STMTTRN
    @parser.output[:transactions] = [] unless @parser.output[:transactions]
    @parser.output[:transactions].push(transaction)
  when :LEDGERBAL
    @parser.output[:balance] = @balance[:BALAMT]
  when :AVAILBAL
    @parser.output[:pending] = @balance[:BALAMT] - @parser.output[:balance]
  end
end

#start_element(name) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/ofx/handler.rb', line 13

def start_element(name)
  case name
  when :STMTTRN then @transaction = {}
  when :LEDGERBAL || :AVAILBAL then @balance = {}
  end
  @current = name
end

#value(value) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/ofx/handler.rb', line 21

def value(value)
  case
  when TRANSACTION_ATTRS.include?(@current)
    @transaction[@current] = value.send(ATTRS_MAP[@current])
  when BALANCE_ATTRS.include?(@current)
    @balance[@current] = value.send(ATTRS_MAP[@current])
  end
end