Skip to content

Instantly share code, notes, and snippets.

@6ewis
Created January 18, 2014 18:53
Show Gist options
  • Save 6ewis/81364d83f7cd9b3a3726 to your computer and use it in GitHub Desktop.
Save 6ewis/81364d83f7cd9b3a3726 to your computer and use it in GitHub Desktop.
require 'csv'
require 'YAML'
Customer = Struct.new(:cust_id,
:elect_or_gas,
:disconnect_doc,
:move_in_date,
:move_out_date,
:bill_year,
:bill_month,
:span_days,
:meter_read_date,
:meter_read_type,
:consumption,
:exception_code,
:data) {
def initialize *fields
self.data = fields
end
}
class Parser
attr_accessor :customers
def initialize filename
self.customers = []
deserialize_users filename
end
def deserialize_users filename
CSV.foreach(filename, col_sep: "|") do |row|
next if row.empty?
#sanitized_values(cust_id, elect_or_gas, disconnect_doc, move_in_date, move_out_date, bill_year, bill_month, span_days, meter_read_date, meter_read_type, consumption, exception_code = row)
customers << Customer.new(*row)
end
customers
end
def serialize_users
CSV.open("./serialized_users.yaml", "w") do |csv| #is 'a' better?
customers.each do |c|
csv << [c.data] #why an array of hashes is better? and how?
end
end
end
end
class Reporting
def initialize data
@p = Parser.new data
end
#STATISTICS
def unique_customers
@p.customers.uniq {|customer| customer.cust_id }
end
def customers_with_electricity
@p.customers.select { |customer| customer.elect_or_gas == "1" && customer.elect_or_gas != "2" }
end
def customers_with_gas
@p.customers.select { |customer| customer.elect_or_gas != "1" && customer.elect_or_gas == "2" }
end
#REPORT
def show_all_data
print "\n\nALL CUSTOMERS AND DATA\n\n"
puts @p.customers.map { |customer| customer.data.map { |item| item.to_s.center 15 }.join '|' }
end
def show_unique_customers
print "\n\nUNIQUE CUSTOMERS\n\n"
unique_customers.map {|customer| puts customer.data.map { |item| item.to_s.center 15}.join'|' }
puts "\nNumber of unique customers: #{unique_customers.count -1}"
end
def show_customers_with_electricity
print "\n\nCUSTOMERS WITH ELECTRICITY ONLY\n\n"
customers_with_electricity.map {|customer| puts customer.data.map { |item| item.to_s.center 15}.join'|'}
puts "\nNumber of customers with electricity: #{customers_with_electricity.count}"
end
def show_customers_with_gas
print "\n\nCUSTOMER WITH GAS ONLY\n\n"
customers_with_gas.map {|customer| puts customer.data.map { |item| item.to_s.center 15}.join'|'}
puts "\nNumber of customers with gas: #{customers_with_gas.count}"
end
end
r = Reporting.new "./pulse_data.txt"
r.show_all_data
r.show_unique_customers
r.show_customers_with_gas
r.show_customers_with_electricity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment