Smile Bank to Ledger

Check-in [5bdd7b804f]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix sorting/ordering of transactions

Need to get things in the right order for checking whether we've gone far
enough back and also for appending to the main transactions array and then
again for writing out.

Also add appropriate Expenses/Income placeholder

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5bdd7b804faa6dd65c5a79597e5b197d4081016b
User & Date: atomicules 2015-07-04 13:36:15
Context
2015-07-07
22:19
Report out balance as a comment at end of the file check-in: 565270916f user: atomicules tags: trunk
2015-07-06
20:53
Create new branch named "cascade_methods" check-in: cdb88b0318 user: atomicules tags: cascade_methods
20:32
Create new branch named "pass_args" Closed-Leaf check-in: 7025864667 user: atomicules tags: pass_args
2015-07-04
13:36
Fix sorting/ordering of transactions

Need to get things in the right order for checking whether we've gone far
enough back and also for appending to the main transactions array and then
again for writing out.

Also add appropriate Expenses/Income placeholder check-in: 5bdd7b804f user: atomicules tags: trunk

11:21
Build it into a proper script

I'm not entirely sure whether a class structure is appropriate for this. At the
very least because I'm only ever creating one of them, but also because
although it's nice having the separate methods for each stage of the logon
process, etc, they don't really make sense, and cna't be used, as standalone
methods. I'm also not sure about the use of instance variables. Splitting
things out into methods almost requires them as passing arguments would be a
bit clumsy, but then it is almost like using global variables. So, all in all,
I've attempted to make it more Ruby-ish, but I think I've failed.

- I'd got credits and debits the wrong way round
- Adds in dealing with memorable date logon details check-in: 5454406454 user: atomicules tags: trunk

Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to smile.rb.

89
90
91
92
93
94
95


96
97
98
99
100
101
102
103
104
105
106
107

108
109
110
111
112
113
114
115
116
117
118
119
120


121
122
123
124
125
126


127
128
129
130

131
132

133
134

135
136
137
138
139
140
141
		#Previous statements
		@page = @a.click(@page.link_with(:text => "previous statements"))
		#Get first page of previous statements
		@page = @a.click(@page.links_with(:href => %r{getDomestic} )[0])
		date = Date.today
		until date < @date_back
			get_transactions


			date = Date.parse(@transactions[-1][:date])
			@page = @a.click(@page.link_with(:text => "previous statement page"))
		end
	end


	def get_transactions
		doc = Nokogiri::HTML(@page.body)
		#Look for summaryTable
		rows = doc.xpath("//table[@class='summaryTable']//tr")
		#Skip first row (table headers, ths)
		rows = rows[1..-1]

		rows.each do |row|
			#Skip last row, maybe
			unless row.elements.length == 1
				#TODO: Need to skip BROUGHT FORWARD as well for previous item pages
				date = row.elements[0].text
				name = row.elements[1].text
				credit = row.elements[2].text
				debit = row.elements[3].text
				unless name.include?("BROUGHT FORWARD")
					@transactions << { :date => date, :name => name, :credit => credit, :debit => debit}
				end
			end
		end	


	end


	#TODO: Might need to sort by date
	def write_ledger
		File.open("smile.dat", "w") do |file|


			@transactions.each do |transaction|
				file << Date.parse(transaction[:date]).strftime("%Y/%m/%d")+"\t"+transaction[:name]+"\n"
				if transaction[:debit].include?("£")
					amount = transaction[:debit].gsub(/£/, "£-")

				else
					amount = transaction[:credit]

				end
				file << "\t"+"Assets:Smile:Current"+"\t"+amount+"\n"

				file << "\n"
			end
		end
	end

	private :logon_start
	private :logon_passcode







>
>
|











>



|
|




|



>
>



<


>
>

|


>


>


>







89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
		#Previous statements
		@page = @a.click(@page.link_with(:text => "previous statements"))
		#Get first page of previous statements
		@page = @a.click(@page.links_with(:href => %r{getDomestic} )[0])
		date = Date.today
		until date < @date_back
			get_transactions
			#On previous statement pages most recent transaction is at bottom
			#This is opposite to recent item page
			date = @transactions[-1][:date]
			@page = @a.click(@page.link_with(:text => "previous statement page"))
		end
	end


	def get_transactions
		doc = Nokogiri::HTML(@page.body)
		#Look for summaryTable
		rows = doc.xpath("//table[@class='summaryTable']//tr")
		#Skip first row (table headers, ths)
		rows = rows[1..-1]
		transactions = []
		rows.each do |row|
			#Skip last row, maybe
			unless row.elements.length == 1
				#Might as well parse date here
				date = Date.parse(row.elements[0].text)
				name = row.elements[1].text
				credit = row.elements[2].text
				debit = row.elements[3].text
				unless name.include?("BROUGHT FORWARD")
					transactions << { :date => date, :name => name, :credit => credit, :debit => debit}
				end
			end
		end	
		#Sort in order of newest date first so can add on
		@transactions += transactions.sort { | t1, t2 | t2[:date] <=> t1[:date] }
	end



	def write_ledger
		File.open("smile.dat", "w") do |file|
			#Reverse for writing out
			@transactions.reverse!
			@transactions.each do |transaction|
				file << transaction[:date].strftime("%Y/%m/%d")+"\t"+transaction[:name]+"\n"
				if transaction[:debit].include?("£")
					amount = transaction[:debit].gsub(/£/, "£-")
					type = "Expenses:"
				else
					amount = transaction[:credit]
					type = "Income:"
				end
				file << "\t"+"Assets:Smile:Current"+"\t"+amount+"\n"
				file << "\t"+type+"\n"
				file << "\n"
			end
		end
	end

	private :logon_start
	private :logon_passcode