From 6ed672e4439cf6be54781dd87abcc815d09cd5d1 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 13 Nov 2014 18:03:41 +0100 Subject: Move reporting to Reports::SalesByDate class. --- lib/reports/sales_by_date.rb | 34 ++++++++++++++++++++++++++++++++++ parse_report.rb | 27 ++------------------------- 2 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 lib/reports/sales_by_date.rb diff --git a/lib/reports/sales_by_date.rb b/lib/reports/sales_by_date.rb new file mode 100644 index 0000000..9e623f1 --- /dev/null +++ b/lib/reports/sales_by_date.rb @@ -0,0 +1,34 @@ +module SalesReporter + module Reports + class SalesByDate + def self.render(sales) + sales_by_date = {} + + sales.each do |s| + d = s.date + if sales_by_date[d] + sales_by_date[d][:count] += s.quantity + sales_by_date[d][:total] += s.amount + else + sales_by_date[d] = {:count => s.quantity, :total => s.amount} + end + end + + puts "Date: Streams: Revenue:" + puts "------------------------------------------" + + total_rev = 0 + total_streams = 0 + + sales_by_date.each do |key, row| + puts "#{key.iso8601}: #{row[:count].to_s.rjust(7)} #{row[:total].round(5).to_s.rjust(10)} EUR" + total_rev += row[:total] + total_streams += row[:count] + end + + puts "------------------------------------------" + puts "Total: #{total_streams.to_s.rjust(7)} #{total_rev.round(5).to_s.rjust(10)} EUR" + end + end + end +end diff --git a/parse_report.rb b/parse_report.rb index efaa231..40df679 100644 --- a/parse_report.rb +++ b/parse_report.rb @@ -1,4 +1,5 @@ require 'sale' +require 'reports/sales_by_date' require 'csv' csv_opts = { @@ -9,7 +10,6 @@ csv_opts = { } sales = [] -sales_by_date = {} CSV.foreach(ARGV[0], csv_opts) do |row| if row.count > 0 @@ -17,27 +17,4 @@ CSV.foreach(ARGV[0], csv_opts) do |row| end end -sales.each do |s| - d = s.date - if sales_by_date[d] - sales_by_date[d][:count] += s.quantity - sales_by_date[d][:total] += s.amount - else - sales_by_date[d] = {:count => s.quantity, :total => s.amount} - end -end - -puts "Date: Streams: Revenue:" -puts "------------------------------------------" - -total_rev = 0 -total_streams = 0 - -sales_by_date.each do |key, row| - puts "#{key.iso8601}: #{row[:count].to_s.rjust(7)} #{row[:total].round(5).to_s.rjust(10)} EUR" - total_rev += row[:total] - total_streams += row[:count] -end - -puts "------------------------------------------" -puts "Total: #{total_streams.to_s.rjust(7)} #{total_rev.round(5).to_s.rjust(10)} EUR" +SalesReporter::Reports::SalesByDate.render(sales) -- cgit v1.2.3