Create a gist now

Instantly share code, notes, and snippets.

# quick spike to ascertain sales of my book since its publication, and
# the impact (or lack thereof) of sales, i.e., giving a discount during
# a limited time frame. note that this does not address effects of a
# sale on traffic, and that I have several other products, which this
# code automatically excludes from its analysis.
require 'rubygems'
require 'active_support'
require 'awesome_print'
require 'csv'
class Payment < Struct.new(:subject, :product, :net) ; end
@payments = (CSV.read(ARGV[0]).collect do |array|
next if "Date" == array[0] # skip header line
next if array[4].match(/Sent/)
payment = Payment.new
payment.subject = array[4]
payment.product = array[6]
payment.net = array[10].to_f
payment
end).compact
# http://www.ruby-forum.com/topic/132357
class Numeric
def round_to(decimals)
factor = 10.0 ** decimals
(self * factor).round / factor
end
end
class Sales
attr_accessor :units, :net
def initialize
@units = 0
@net = 0.0
end
def to_s
@units.to_s +
" sold, for " +
@net.round_to(2).to_s +
" total"
end
def add(amount)
@units += 1
@net += amount
end
end
full_price = Sales.new
sale = Sales.new
@payments.each do |payment|
next unless payment.product.match(/Rails As She Is Spoke/)
if payment.net < 30.0
sale.add(payment.net)
else
full_price.add(payment.net)
end
end
puts "full price:"
ap full_price
puts "sale:"
ap sale
# full price:
# 368 sold, for 13176.51 total
# sale:
# 71 sold, for 1569.95 total
#
# therefore the totals are: 439 sold, 14,746.46 total
# discounts: 16% of copies sold, 10.6% of total income
#
# (in actuality, I sold a few additional books through other channels.
# so I believe the total so far is at least 444 copies sold.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment