Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View rails_as_she_is_sold.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
# 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
Something went wrong with that request. Please try again.