Skip to content

Instantly share code, notes, and snippets.

@nfriend21
Created October 26, 2012 01:11
Show Gist options
  • Save nfriend21/3956428 to your computer and use it in GitHub Desktop.
Save nfriend21/3956428 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'sinatra/activerecord'
require "sinatra/reloader" if development?
require 'active_record'
require 'csv'
require 'uri'
#ActiveRecord::Base.establish_connection(
# :adapter => 'sqlite3',
# :database => 'sinatra_application.sqlite3.db'
#)
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:port => db.port,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
######################## HELPERS
helpers do
def link_to(url,text=url,opts={})
attributes = ""
opts.each { |key,value| attributes << key.to_s << "=\"" << value << "\" "}
"<a href=\"#{url}\" #{attributes}>#{text}</a>"
end
end
######################## CLASSES
class Csvfile < ActiveRecord::Base
attr_accessible :filename, :data
end
######################## ROUTES
get '/' do
@csv_files = Csvfile.all
erb :index
end
post '/' do
#puts params.inspect
@csv = Csvfile.new
@csv.data = params[:csvfile][:tempfile].read
@csv.filename = params[:csvfile][:filename]
@csv.save
start_date = params[:start_date].split('-').join
end_date = params[:end_date].split('-').join
redirect "/#{@csv.id}?start_date=#{start_date}&end_date=#{end_date}"
end
get '/:id' do
#use this to read a file into a string, directly in the console
#s = File.open('filename', 'r') { |f| f.read }
@csv = Csvfile.find(params[:id])
#parses the string of csv data. converts each row of data into an array, within one master array.
def csv_parse
@file = CSV.parse(@csv.data, :headers => true, :header_converters => :symbol)
end
#turns date into an integer, for chronological sorting.
def clean_date(date)
DateTime.strptime(date, "%m/%d/%Y %t").strftime("%Y%m%d")
end
#updates @file with cleaned dates.
def update_file
@file.each do |line|
line[:account_created_] = clean_date(line[:account_created_])
end
end
#creates hash to count the total amount of closes vs. total accounts.
def account_collect(start_date, end_date, rep='nick')
counts = Hash.new(0)
@file.each do |line|
if line[:account_created_].to_i.between?(start_date.to_i, end_date.to_i) && line[:rep] == rep
counts[:all] += 1
counts[:closed] += 1 if line[:_of_orders].to_i >= 6
end
end
closed_percentage = (Float(counts[:closed]) / Float(counts[:all]))
@closed_percentage = "#{rep}:" + sprintf('%.2f', closed_percentage)
end
def collect_reps
@reps = @file[:rep].uniq
@rep_options = %{}
@reps.each do |rep|
@rep_options << %{<option value="#{rep}">#{rep}</option>}
end
@rep_options
end
csv_parse
update_file
account_collect(params[:start_date], params[:end_date], params[:rep])
collect_reps
@start_date = DateTime.strptime(params[:start_date], "%Y%m%d").strftime("%m/%d/%Y")
@end_date = DateTime.strptime(params[:end_date], "%Y%m%d").strftime("%m/%d/%Y")
erb :show
end
post '/:id' do
@csv = Csvfile.find(params[:id])
start_date = params[:start_date].split('-').join
end_date = params[:end_date].split('-').join
rep = params[:rep]
redirect "/#{@csv.id}?start_date=#{start_date}&end_date=#{end_date}&rep=#{rep}"
erb :show
end
require 'app'
run Sinatra::Application
source 'http://rubygems.org'
gem 'sinatra'
gem 'activerecord'
gem 'sinatra-activerecord' # excellent gem that ports ActiveRecord for Sinatra
gem 'thin'
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg' # this gem is required to use postgres on Heroku
end
require_relative 'app'
require 'sinatra/activerecord/rake'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment