Class: QuickenParser::Parser
- Inherits:
-
Object
- Object
- QuickenParser::Parser
- 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
- #account_from_xml(xml) ⇒ Object
- #accounts ⇒ Object
-
#add_xml_decl! ⇒ Object
Transform anything to ASCII/UTF-8.
- #build_account_details(xml, params = {}) ⇒ Object
- #cc_account_from_xml(xml) ⇒ Object
- #close_sgml_decl! ⇒ Object
-
#initialize(source) ⇒ Parser
constructor
A new instance of Parser.
- #normalize_line_endings! ⇒ Object
- #parse ⇒ Object
- #parse_date(xmldate) ⇒ Object
- #remove_sgml_options! ⇒ Object
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 account_from_xml(xml) currency = REXML::XPath.first(xml, ".//CURDEF").text bank_id = REXML::XPath.first(xml, ".//BANKID").text account_id = REXML::XPath.first(xml, ".//ACCTID").text account_type = REXML::XPath.first(xml, ".//ACCTTYPE").text build_account_details(xml, :currency => currency, :bank_id => bank_id, :number => account_id, :type => account_type) end |
#accounts ⇒ Object
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 << account_from_xml(xml) end REXML::XPath.each(@doc.root, "//CCSTMTRS") do |xml| @accounts << cc_account_from_xml(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 build_account_details(xml, params={}) account = Account.new(params) xmldatefrom = REXML::XPath.first(xml, ".//DTSTART").text xmldateto = REXML::XPath.first(xml, ".//DTEND").text account.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}" account.transactions << Transaction.new(:type => type, :timestamp => parse_date(date_posted), :amount => amount_and_currency.to_money, :number => txnid, :name => name, :memo => memo) end account end |
#cc_account_from_xml(xml) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/quicken_parser/parser.rb', line 34 def cc_account_from_xml(xml) currency = REXML::XPath.first(xml, ".//CURDEF").text bank_id = nil account_id = REXML::XPath.first(xml, ".//ACCTID").text account_type = "CREDITCARD" build_account_details(xml, :currency => currency, :bank_id => bank_id, :number => account_id, :type => account_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 |
#parse ⇒ Object
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! @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 = Time.parse(xmldate) then 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 @input.gsub!(/^[A-Z]+:[-0-9A-Z]+$/, "") end |