Class: QuickenParser::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/quicken_parser/parser.rb

Overview

:nodoc:

Defined Under Namespace

Classes: UnsupportedEncodingException

Constant Summary collapse

ASCII =
(32..127).to_a + [10, 13, 9]

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Parser

Returns a new instance of Parser.



6
7
8
# File 'lib/quicken_parser/parser.rb', line 6

def initialize(source)
  @input = source.respond_to?(:read) ? source.read : source
end

Instance Method Details

#account_from_xml(xml) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/quicken_parser/parser.rb', line 43

def (xml)
  currency     = REXML::XPath.first(xml, ".//CURDEF").text
  bank_id      = REXML::XPath.first(xml, ".//BANKID").text
     = REXML::XPath.first(xml, ".//ACCTID").text
   = REXML::XPath.first(xml, ".//ACCTTYPE").text

  (xml, :currency => currency, :bank_id => bank_id, :number => , :type => )
end

#accountsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/quicken_parser/parser.rb', line 20

def accounts
  return @account if @account
  @accounts = Array.new
  REXML::XPath.each(@doc.root, "//STMTRS") do |xml|
    @accounts << (xml)
  end

  REXML::XPath.each(@doc.root, "//CCSTMTRS") do |xml|
    @accounts << (xml)
  end

  @accounts
end

#add_xml_decl!Object

Transform anything to ASCII/UTF-8. This is bad, and loses valuable information, but hey, I want something that works NOW, rather than something that might work in 20 years…



92
93
94
# File 'lib/quicken_parser/parser.rb', line 92

def add_xml_decl! #:nodoc:
  @input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n#{@input}"
end

#build_account_details(xml, params = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/quicken_parser/parser.rb', line 52

def (xml, params={})
   = .new(params)

  xmldatefrom  = REXML::XPath.first(xml, ".//DTSTART").text
  xmldateto    = REXML::XPath.first(xml, ".//DTEND").text
  .transactions.timespan = parse_date(xmldatefrom)..parse_date(xmldateto)

  REXML::XPath.each(xml, ".//STMTTRN") do |xmltxn|
    type        = text_or_nil(xmltxn, ".//TRNTYPE")
    date_posted = text_or_nil(xmltxn, ".//DTPOSTED")
    amount      = text_or_nil(xmltxn, ".//TRNAMT")
    txnid       = text_or_nil(xmltxn, ".//FITID")
    name        = text_or_nil(xmltxn, ".//NAME")
    memo        = text_or_nil(xmltxn, ".//MEMO")

    amount_and_currency = "#{amount} #{account.currency}"
    .transactions << Transaction.new(:type => type, :timestamp => parse_date(date_posted), :amount => amount_and_currency.to_money, :number => txnid, :name => name, :memo => memo)
  end

  
end

#cc_account_from_xml(xml) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/quicken_parser/parser.rb', line 34

def (xml)
  currency     = REXML::XPath.first(xml, ".//CURDEF").text
  bank_id      = nil
     = REXML::XPath.first(xml, ".//ACCTID").text
   = "CREDITCARD"

  (xml, :currency => currency, :bank_id => bank_id, :number => , :type => )
end

#close_sgml_decl!Object



96
97
98
# File 'lib/quicken_parser/parser.rb', line 96

def close_sgml_decl!
  @input.gsub!(/<([A-Z.]+)>(.+)$/, "<\\1>\\2</\\1>")
end

#normalize_line_endings!Object



82
83
84
85
# File 'lib/quicken_parser/parser.rb', line 82

def normalize_line_endings!
  @input.gsub!("\r\n", "\n")
  @input.gsub!("\r", "\n")
end

#parseObject



10
11
12
13
14
15
16
17
18
# File 'lib/quicken_parser/parser.rb', line 10

def parse
  normalize_line_endings!
  add_xml_decl!
  close_sgml_decl!
  remove_sgml_options!

  @doc = REXML::Document.new(@input)
  self
end

#parse_date(xmldate) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/quicken_parser/parser.rb', line 74

def parse_date(xmldate)
  if timestamp = Time.parse(xmldate) then
    timestamp
  else
    raise DateParsingError, "Could not parse XML formatted date #{xmldate.inspect}"
  end
end

#remove_sgml_options!Object



100
101
102
# File 'lib/quicken_parser/parser.rb', line 100

def remove_sgml_options!
  @input.gsub!(/^[A-Z]+:[-0-9A-Z]+$/, "")
end