# encoding: utf-8 module TokiCLI class Export def initialize toki, view @toki = toki @view = view end def apps_total options, apps, type arr = [] apps['data']['apps'].each do |obj| arr << { bundle: obj['bundle'], name: obj['name'], total: { seconds: obj['total']['seconds'], time: @view.readable_time(obj['total']['time']) } } end save options, arr, "toki-#{type}__#{Time.now.to_s[0..9]}" end def log options, log, type arr = [] temp = log['data']['log'].map do |obj| [obj[1]['start'], obj[1]['duration']['seconds'], @view.readable_time(obj[1]['duration']['time']), obj[0]] end temp.sort_by! {|obj| obj[0]} temp.each do |obj| arr << { start: obj[0], duration: { seconds: obj[1], time: obj[2] }, sync_id: obj[3] } end save_log options, arr, "toki-#{type}__#{Time.now.to_s[0..9]}" end private def save options, data, filename case when options[:json] name = @toki.helpers.toki_path + "/#{filename}.json" File.write(name, data.to_json) when options[:csv] name = @toki.helpers.toki_path + "/#{filename}.csv" CSV.open(name, "wb") do |csv| csv << ['Bundle Identifier', 'Name', 'Total seconds', 'Total time'] data.each {|line| csv << [line[:bundle], line[:name], line[:total][:seconds], line[:total][:time]]} end else exit end puts "File converted and exported to #{name}\n\n" end def save_log options, data, filename case when options[:json] name = @toki.helpers.toki_path + "/#{filename}.json" File.write(name, data.to_json) when options[:csv] name = @toki.helpers.toki_path + "/#{filename}.csv" CSV.open(name, "wb") do |csv| csv << ['Start', 'Duration (seconds)', 'Duration (time)', 'Sync ID'] data.each {|line| csv << [line[:start], line[:duration][:seconds], line[:duration][:time], line[:sync_id]]} end else exit end puts "File converted and exported to #{name}\n\n" end end end