Module: EventReporter::Save
- Defined in:
- lib/event_reporter/save.rb
Class Method Summary collapse
- .build_attendees_xml(xml, queue) ⇒ Object
- .route_save_to(filename, queue) ⇒ Object
- .save_to_csv(file_path, queue) ⇒ Object
- .save_to_json(file_path, queue) ⇒ Object
- .save_to_txt(file_path, queue) ⇒ Object
- .save_to_txt_contents(txt, columns_length, queue) ⇒ Object
- .save_to_txt_headers(txt, columns_length) ⇒ Object
- .save_to_xml(file_path, queue) ⇒ Object
- .success(file_path) ⇒ Object
Class Method Details
.build_attendees_xml(xml, queue) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/event_reporter/save.rb', line 87 def build_attendees_xml(xml, queue) queue.each do |attendee| xml.attendee { xml.first_name attendee.first_name xml.last_name attendee.last_name xml.email attendee.email xml.phone_number attendee.phone_number xml.address attendee.address xml.city attendee.city xml.state attendee.state xml.zipcode attendee.zipcode } end end |
.route_save_to(filename, queue) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/event_reporter/save.rb', line 4 def route_save_to(filename, queue) throw(:top, "Please enter a filename to save to") if filename.nil? extension = filename.split(".")[1] method = "save_to_#{extension}" if respond_to? method file_path = File.("../../../bin/#{filename}", __FILE__) send(method, file_path, queue) else throw(:top, "Extension not valid! (try .csv, .xml, .txt, .json)") end end |
.save_to_csv(file_path, queue) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/event_reporter/save.rb', line 18 def save_to_csv(file_path, queue) CSV.open("#{file_path}", "w") do |csv| csv << HEADERS.map do |header| header.downcase == 'phone_number' ? 'HomePhone' : header end queue.each do |attendee| csv << [attendee.last_name, attendee.first_name, attendee.email, attendee.zipcode, attendee.city, attendee.state, attendee.address, attendee.phone_number] end end success(file_path) end |
.save_to_json(file_path, queue) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/event_reporter/save.rb', line 62 def save_to_json(file_path, queue) File.open("#{file_path}", "w") do |json| json.puts queue.to_json end success(file_path) end |
.save_to_txt(file_path, queue) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/event_reporter/save.rb', line 37 def save_to_txt(file_path, queue) columns_length = Print.find_column_lengths(queue) File.open("#{file_path}", "w") do |txt| save_to_txt_headers(txt, columns_length) save_to_txt_contents(txt, columns_length, queue) end success(file_path) end |
.save_to_txt_contents(txt, columns_length, queue) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/event_reporter/save.rb', line 53 def save_to_txt_contents(txt, columns_length, queue) queue.each_with_index do |attendee, i| HEADERS.each do |header| column_width = columns_length[header.to_sym] txt.print "#{attendee[header].ljust(column_width)} " end end end |
.save_to_txt_headers(txt, columns_length) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/event_reporter/save.rb', line 46 def save_to_txt_headers(txt, columns_length) HEADERS.each do |header| column_width = columns_length[header.to_sym] txt.print "#{header.gsub("_", " ").upcase.ljust(column_width)} " end end |
.save_to_xml(file_path, queue) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/event_reporter/save.rb', line 69 def save_to_xml(file_path, queue) queue_xml = Nokogiri::XML::Builder.new do |xml| xml.root { xml.event_attendees { build_attendees_xml(xml, queue) } } end File.open("#{file_path}", "w") do |xml| xml.puts queue_xml.to_xml end success(file_path) end |
.success(file_path) ⇒ Object
83 84 85 |
# File 'lib/event_reporter/save.rb', line 83 def success(file_path) puts "Saved file #{file_path.split('/').last}" end |