diff --git a/lib/gnucash.rb b/lib/gnucash.rb index ed7d9de..c464bc9 100644 --- a/lib/gnucash.rb +++ b/lib/gnucash.rb @@ -1,3 +1,4 @@ +require "gnucash/account" require "gnucash/book" require "gnucash/version" diff --git a/lib/gnucash/account.rb b/lib/gnucash/account.rb new file mode 100644 index 0000000..097ac59 --- /dev/null +++ b/lib/gnucash/account.rb @@ -0,0 +1,9 @@ +module Gnucash + class Account + def initialize(book, node) + @node = node + @name = node.xpath('act:name').text + @type = node.xpath('act:type').text + end + end +end diff --git a/lib/gnucash/book.rb b/lib/gnucash/book.rb index eac9e7a..2e670eb 100644 --- a/lib/gnucash/book.rb +++ b/lib/gnucash/book.rb @@ -3,8 +3,24 @@ require "nokogiri" module Gnucash class Book + attr_accessor :accounts + def initialize(fname) @ng = Nokogiri.XML(Zlib::GzipReader.open(fname).read) + book_nodes = @ng.xpath('/gnc-v2/gnc:book') + if book_nodes.count != 1 + raise "Error: Expected to find one gnc:book entry" + end + @book_node = book_nodes.first + build_accounts + end + + private + + def build_accounts + @accounts = @book_node.xpath('gnc:account').map do |act_node| + Account.new(self, act_node) + end end end end