Created
July 13, 2010 16:32
-
-
Save biot023/474133 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "csv" | |
require "json" | |
unless $testing | |
Shoes.setup do | |
# gem "yajl-ruby" | |
gem "json" | |
end | |
end | |
# require "yajl" | |
require "json" | |
class MtaReportFormatter | |
# | |
# Get the csv data from the file and return it as an array of job hashes, with the | |
# "job_number", "job_name", and "properties" keys. The properties values are an array | |
# of property data hashes, with the keys "uprn" and "name". | |
# | |
def do_format( fname ) | |
csv_data = CSV.read( fname )[1..-1] | |
csv_data.sort! do |a, b| | |
result = a[0] <=> b[0] | |
result = a[2] <=> b[2] if result == 0 | |
result | |
end | |
csv_data.inject( {} ) do |acc, (job_number, job_name, property_number, property_name)| | |
acc[job_number] ||= {} | |
acc[job_number]["job_number"] ||= job_number | |
acc[job_number]["job_name"] ||= job_name | |
acc[job_number]["properties"] ||= [] | |
acc[job_number]["properties"] << { "uprn" => property_number, "name" => property_name } | |
acc | |
end.values | |
end | |
# | |
# Do the format and output as json to target file. | |
# | |
def do_format_to_file( orig_fname, target_fname ) | |
data = do_format( orig_fname ) | |
# json_data = Yajl::Encoder.encode( data, :pretty => true, :indent => " " ) | |
json_data = JSON::pretty_generate( data, :indent => " " ) | |
File.open( target_fname, "w" ) do |file| | |
file << json_data | |
end | |
end | |
end | |
unless $testing | |
# orig_fname = ARGV[0] | |
# if orig_fname | |
# target_fname = ARGV[1] | |
# if target_fname | |
# puts "\nProcessing #{ orig_fname.inspect }..." | |
# MtaReportFormatter.new.do_format_to_file( orig_fname, target_fname ) | |
# puts "\nOutput to #{ target_fname.inspect }." | |
# else | |
# puts "\n\nUsage: ./mta_report_formatter <input_filename> <output_filename>\n\n" | |
# end | |
# else | |
# puts "\n\nUsage: ./mta_report_formatter <input_filename> <output_filename>\n\n" | |
# end | |
Shoes.app do | |
@stack = stack do | |
title( "File to convert:", :size => 16 ) | |
@file_in_text = title( "None selected", :size => 12, :color => :blue ) | |
@file_in_button = button( "Select" ) do | |
@file_in = ask_open_file | |
if @file_in | |
@file_in_text.text = @file_in | |
@file_in_button.remove | |
@stack.append do | |
@file_out = @file_in + ".json" | |
button( "Run" ) do | |
begin | |
MtaReportFormatter.new.do_format_to_file( @file_in, @file_out ) | |
rescue => err | |
alert( "Failed with: #{ [ err.inspect, err.backtrace ].join( "\n" ) }" ) | |
end | |
alert( "Output to: #{ @file_out.inspect }" ) | |
exit | |
end # button | |
end | |
end | |
end # file_in_button | |
end # stack | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment