An open source, stand-alone, customisable public spending data web app.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

import2010Q1.rb 1007 B

pirms 14 gadiem
123456789101112131415161718192021222324252627282930313233343536373839
  1. require 'lib/models'
  2. require 'csv'
  3. count = 0
  4. # 2010Q1: 0-Directorate,1-Updated,2-Service,3-Supplier Name,4-Amount excl vat £,5-Type
  5. CSV::Reader.parse(File.open('data/2010Q1.csv', 'rb')) do |row|
  6. count += 1
  7. if (count > 4) # skip first four lines that don't contain data
  8. p row
  9. directorate = Directorate.first_or_create(:name => row[0].strip)
  10. service = Service.first_or_create(:name => row[2].strip)
  11. supplier = Supplier.first_or_create(:name => row[3].strip)
  12. dt = row[1].strip.split('/')
  13. payment = Payment.first_or_create(
  14. 'directorate' => directorate,
  15. 'service' => service,
  16. 'supplier' => supplier,
  17. 'amount' => row[4].strip.gsub(/,/, ''),
  18. 'd' => Date.new(dt[2].to_i, dt[1].to_i, dt[0].to_i),
  19. 'tyype' => row[5].strip
  20. )
  21. unless payment.save
  22. puts "ERROR: Failed to save payment"
  23. payment.errors.each do |e|
  24. puts e
  25. end
  26. end
  27. end
  28. end