Skip to content

Instantly share code, notes, and snippets.

@zph
Created December 18, 2012 19:31
Show Gist options
  • Save zph/4331152 to your computer and use it in GitHub Desktop.
Save zph/4331152 to your computer and use it in GitHub Desktop.
diff --git a/lib/trello-archiver/filewriter.rb b/lib/trello-archiver/filewriter.rb
index 2308e43..f570cfb 100644
--- a/lib/trello-archiver/filewriter.rb
+++ b/lib/trello-archiver/filewriter.rb
@@ -1,119 +1,205 @@
+ class Archiver
+ include Trello
+ include Trello::Authorization
+ def initialize(options =
+ {:board => "",
+ :filename => "trello_backup",
+ :format => 'xlsx',
+ :col_sep => ","})
+ @options = options
+ FileUtils.mkdir("archive") unless Dir.exists?("archive")
+ date = DateTime.now.strftime "%Y%m%dT%H%M"
+ @filename = "#{Dir.pwd}/#{date}_"
+ @filename += "#{@options[:filename].upcase}.#{@options[:format]}"
+
+ @lists = @options[:board].lists
+ @row_create = ->(sheet, content){ sheet.add_row(content) }
+ end
+
+ def create_backup
+ case @options[:format]
+ when 'csv' && ( @options[:col_sep] == "\t" )
+ @options[:format] = 'tsv'
+ create_csv
+ when 'tsv'
+ @options[:col_sep] = "\t"
+ create_csv
+ when 'csv'
+ create_csv
+ when 'xlsx'
+ create_xlsx
+ else
+ #
+ message = "Trello-archiver can create csv, tsv, and xlsx backups."
+ message += " Please choose one of these options and try again."
+ puts message
+ end
+ end
+
+ def card_labels_if_existing(card)
+ case card.labels.length
+ when 0
+ "none"
+ else
+ card.labels.map { |c| c.name }.join(" ")
+ end
+ end
+
+ def gather_comments(card)
+ card.actions.map do |action|
+ if action.type == "commentCard"
+ output = "#{Member.find(action.member_creator_id).full_name}"
+ output += " [#{ action.date.strftime('%m/%d/%Y') }]"
+ output += " : #{action.data['text']} \n\n"
+ end
+ end
+ end
- @doc = XlsxWriter.new
+ def gather_labels_and_comments(card)
+ output = {}
+ puts "\t#{card.name}"
+ output[:labels] = card_labels_if_existing(card)
+ output[:comments] = gather_comments(card)
+ output
+ end
- lists.each do |list|
- sheet = @doc.add_sheet(list.name.gsub(/\W+/, '_'))
+ def puts_list_and_output_cards(list)
puts list.name
cards = list.cards
- #
- # Add header row
- sheet.add_row( %w[Name Description Labels Comments])
-
- cards.each do |card|
- # Add title row
- puts card.name
- # sheet.add_row([
- # "Title: #{card.name}",
- # "Desc: #{card.description}",
- # "Labels: #{card.labels.length}"
- # ])
-
- # gather and join the labels if they exist
- labels = case card.labels.length
- when 0
- "none"
- else
- card.labels.map { |c| c.name }.join(" ")
- end
+ end
+
+ def create_csv
+ require 'CSV'
+ header = %w[Name Description Labels Progress Comments]
+ content = "[card.name, card.description, result[:labels],"
+ content += " list.name, result[:comments].join('')]"
- # Gather comments
- comments = card.actions.map do |action|
- if action.type == "commentCard"
- # require 'pry'; binding.pry
- "#{Member.find(action.member_creator_id).full_name} [#{ action.date.strftime('%m/%d/%Y') }] : #{action.data['text']} \n\n"
- end
+ CSV.open(@filename, "w", :col_sep => @options[:col_sep]) do |sheet|
+ sheet.add_row(header)
+ @lists.each do |list|
+ main_process(list, sheet, content)
end
+ end
+ end
+
- def create_xlsx()
- # Filename= filename or default of boardname
- #
- # Board object has been passed into the method
- lists = @options[:board].lists
- filename = "archive/#{DateTime.now.strftime "%Y%m%dT%H%M"}_#{@options[:filename]}.xlsx"
+ def create_xlsx
+ require 'xlsx_writer'
+ header = %w[Name Description Labels Comments]
+ content = "[card.name, card.description, result[:labels], result[:comments].join('')]"
- sheet.add_row([card.name, card.description, labels, comments.join('')])
+ @doc = XlsxWriter.new
+
+ @lists.each do |list|
+ sheet = @doc.add_sheet(list.name.gsub(/\W+/, '_'))
+ sheet.add_row( header )
+ main_process(list, sheet, content)
end
+
+ # Moving file to where I want it
+ require 'fileutils'
+ ::FileUtils.mv @doc.path, @filename
+
+ # Cleanup of temp dir
+ @doc.cleanup
end
- # Moving file to where I want it
- require 'fileutils'
- ::FileUtils.mv @doc.path, File.join(File.dirname(__FILE__), "..", "..", filename)
+ def main_process(list, sheet, content_string)
+ puts_list_and_output_cards(list).each do |card|
+ result = gather_labels_and_comments(card)
+ content = eval(content_string)
+ @row_create.call(sheet, content)
+ end
+ end
- # Cleanup of temp dir
- @doc.cleanup
end
-
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment