Skip to content

Instantly share code, notes, and snippets.

@6ewis
Created January 18, 2014 06:07
Show Gist options
  • Save 6ewis/e795667e71986c31b78a to your computer and use it in GitHub Desktop.
Save 6ewis/e795667e71986c31b78a to your computer and use it in GitHub Desktop.
dl.rb:37:in `block in deserialized_users': undefined method `<<' for nil:NilClass (NoMethodError) from /Users/lewis/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/csv.rb:1717:in `each' from /Users/lewis/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/csv.rb:1120:in `block in foreach' from /Users/lewis/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/csv.rb:126…
require 'csv'
require 'YAML'
class Customers
attr_reader :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 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
@cust_id = cust_id
@elect_or_gas = elect_or_gas
@disconnect_doc = disconnect_doc
@move_in_date = move_in_date
@move_out_date = move_out_date
@bill_year = bill_year
@bill_month = bill_month
@span_days = span_days
@meter_read_date = meter_read_date
@meter_read_type = meter_read_type
@consumption = consumption
@exception_code = exception_code
end
end
class Parse
attr_accessor :customers
def initialize filename
customers = []
deserialized_users filename
end
def deserialized_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(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)
end
customers
end
def serialized_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
end
p = Parse.new "./pulse_data.txt"
#p.serialized_users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment