Created
December 22, 2011 21:14
-
-
Save russellkt/1511885 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'lib/cleared_check' | |
require 'lib/cleared_check_updater' | |
require 'rubygems' | |
require 'fastercsv' | |
require 'sinatra' | |
require 'json' | |
require 'haml' | |
@@cleared_checks = ClearedCheckUpdater.read | |
get '/' do | |
haml :upload | |
end | |
get '/search' do | |
content_type :json | |
@matching_checks = [] | |
if params[:amount] | |
amount = (params[:amount].to_f * 100).round | |
@matching_checks = @@cleared_checks.select{|check| check.amount == amount} | |
end | |
if params[:check_number] | |
check_number = params[:check_number] | |
if @matching_checks.size > 0 | |
@matching_checks = @matching_checks.select{|check| check.check_number == check_number} | |
elsif | |
@matching_checks = @@cleared_checks.select{|check| check.check_number == check_number } | |
end | |
end | |
@matching_checks.to_json | |
end | |
post '/upload' do | |
unless params[:file] && | |
(tmpfile = params[:file][:tempfile]) && | |
(name = params[:file][:filename]) | |
@error = "No file selected" | |
return haml(:upload) | |
end | |
STDERR.puts "Uploading file, original name #{name.inspect}" | |
ClearedCheckUpdater.update(tmpfile) | |
@@cleared_checks = ClearedCheckUpdater.read() | |
"Upload complete" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClearedCheck | |
attr_accessor :amount,:check_number,:account,:posted_on,:sequence,:image_location,:image_offset,:image_length,:image_type | |
def initialize options | |
@amount = options["amount"].gsub(",","").to_f*100.round | |
@check_number = options["check_number"] | |
@posted_on = Date.parse(options["posted_on"]) | |
end | |
def to_json(options) | |
{ :amount => @amount/100, | |
:check_number => @check_number, | |
:posted_on => @posted_on }.to_json | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClearedCheckUpdater | |
def self.update(tmpfile) | |
filesize = File.size(filepath) | |
file = File.open(filepath, "a") | |
tmpfile.lines.each_with_index do |line,index| | |
#don't write first row headers unless file is empty | |
file.write(line) if(index > 0 || filesize < 100) | |
end | |
file.close | |
end | |
def self.read | |
cleared_checks = [] | |
FasterCSV.foreach(filepath, :headers => true) do |line| | |
cleared_checks << ClearedCheck.new(line) | |
end | |
cleared_checks | |
end | |
def self.filepath | |
data_dir = 'data' | |
filename = 'cleared_checks.csv' | |
path = File.join(data_dir, filename) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%form{:action=>"/upload", :method=>"post", :enctype=>"multipart/form-data"} | |
%input{:type=>"file",:name=>"file"} | |
%input{:type=>"submit",:value=>"Upload"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment