Skip to content

Instantly share code, notes, and snippets.

@6ewis
Created January 18, 2014 16:25
Show Gist options
  • Save 6ewis/3e5ac6b5a91515d2ffd2 to your computer and use it in GitHub Desktop.
Save 6ewis/3e5ac6b5a91515d2ffd2 to your computer and use it in GitHub Desktop.
require 'csv'
require 'YAML'
class Customers
attr_reader :data, :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
def initialize *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, = values
@data = values
end
end
class Parse
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?
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
#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 << Customers.new(*row)
end
customers
end
def serialize_users
File.open("./serialized_users.yaml", "w") do |yaml_file|
customers.each do |c|
yaml_file.puts YAML::dump([c.cust_id, c.elect_or_gas, c.disconnect_doc, c.move_in_date, c.move_out_date, c.bill_year, c.bill_month, c.span_days, c.meter_read_date, c.meter_read_type, c.consumption, c.exception_code])
end
end
end
#STATISTICS
def unique_customers
customers.uniq {|customer| customer.cust_id }
end
def customers_with_electricity
customers.select { |customer| customer.elect_or_gas == "1" && customer.elect_or_gas != "2" }
end
def customers_with_gas
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 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
p = Parse.new "./pulse_data.txt"
p.serialize_users
p.show_all_data
p.show_unique_customers
p.show_customers_with_gas
p.show_customers_with_electricity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment