Class: Zensana::Command::View

Inherits:
Zensana::Command show all
Defined in:
lib/zensana/commands/view.rb

Instance Method Summary collapse

Instance Method Details

#export(id) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/zensana/commands/view.rb', line 8

def export(id)
  view = Zensana::Zendesk::View.new
  view.find(id)

  unless yes?("This will export the tickets in the view called '#{view.title}' as a CSV. Proceed?", :yellow)
    say "\nNothing else for me to do, exiting...\n", :red
    exit
  end

  tickets = view.tickets

  csv_file = "zendesk_export_view_#{id}_#{Time.now.strftime('%Y_%m_%d_%H%M')}.csv"
  Dir.chdir file_dir = File.join(Dir.home, 'Downloads')

  CSV.open(csv_file, 'w') do |output|
    output << %w(Id Status Subject Component Description Requester RequestDate SolvedDate Duration)
    tickets.each do |ticket|
      output.puts transform_ticket(ticket)
    end
  end

  say "\nYour view has been successfully exported to '#{File.join(file_dir, csv_file)}'", :green
end