require 'net/http'
namespace :cthit do
  desc "Update glorious printers"
  task update_printers: :environment do
    uri = URI('http://print.chalmers.se/public/pls.cgi')
    res = Net::HTTP.post_form(uri, 'textver' => 'CSV export')
    lines = res.body.split("\n")
    lines.drop(12).each do |printer|
      values = printer.split("\t")
      
      next unless values.length == 6

      p = Printer.create(name: values[0].strip, location: values[2].strip, media: values[4].strip)
      puts p
    end

  end

end
require 'csv'
require 'net/http'

def get_all_csv
  uri = URI 'http://print.chalmers.se/public/pls.cgi'
  res = Net::HTTP.post_form(uri, 'textver' => 'CSV export')
  xml = Nokogiri::XML(res.body)
  text = xml.css('pre').children.text.strip
  CSV.parse(text, {
    col_sep: "\t",
    header_converters: :symbol,
    headers: true,
    return_headers: true
  }).drop(1)
end

namespace :cthit do
  desc "Update glorious printers"
  task update_printers: :environment do
    csv = get_all_csv
    Printer.where('name NOT IN (?)', csv.map{ |c| c[:printer].strip }).delete_all

    csv.each do |printer|
      pp = Printer.find_or_initialize_by(name: printer[:printer].strip)
      pp.update_attributes({
        location: printer[:printer].strip,
        media: printer[:size].strip
      })
      puts pp
    end
  end
end

Make printer adding script repeatable
Make printer adding script repeatable
