# encoding: utf-8 module TokiCLI class App < Thor package_name "TokiCLI" %w{get_messages search_messages get_channels module status view}.each {|r| require_relative "#{r}"} desc "total", "Shows the total usage of all apps" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki total -a)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' def total toki = create_toki(options) apps = get_total(toki, options) clear puts TokiCLI::View.new.total_table(apps) puts "\n" export(:json, apps, "toki-total-#{Time.now.to_s[0..9]}") if options[:json] end desc "top", "Shows your most used apps" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki top -a)' option :number, aliases: '-n', type: :numeric, desc: 'Specify the number of apps (toki top -n 10)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' def top toki, entries = init(options) clear hits = toki.top(entries, options[:number] || 5) view_hits_table(hits) export(:json, hits, "toki-top-#{Time.now.to_s[0..9]}") if options[:json] end desc "auth", "Input your App.net token for authorization" def auth puts Status.paste_token token = STDIN.gets.chomp! abort(Status.no_token) if token == '' save_token(token) puts Status.done end desc "log APP", "Shows the complete log for an app" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki log -a)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' def log(*args) abort(Status.specify_name) if args.empty? asked = args[0] toki, entries = init(options) app_data = toki.get_app(asked, entries) view_app_table(asked, app_data) export(:json, app_data, "toki-log-#{asked}-#{Time.now.to_s[0..9]}") if options[:json] end desc "day DATE", "Shows all apps used on a specific day (toki day 2014-04-19)" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki day 2014-04-19 -a)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' def day(*args) abort(Status.specify_day) if args.empty? begin date = DateTime.strptime(args[0], '%Y-%m-%d') rescue ArgumentError abort(Status.specify_day) end toki, entries = init(options) day_data = toki.get_day(date, entries, options) clear view_day_table(args[0], day_data) export(:json, day_data, "toki-day-#{args[0]}") if options[:json] end desc "range DAY1 DAY2", "Shows all apps used between day 1 and day 2 (toki range 2014-04-16 2014-04-18)" option :adn, aliases: '-a', type: :boolean, desc: 'Select ADN channel as source (toki range 2014-04-16 2014-04-18 -a)' option :json, aliases: '-j', type: :boolean, desc: 'Export the results as a JSON file' def range(*args) abort(Status.specify_range) if args.empty? || args.length != 2 begin day1 = DateTime.strptime(args[0], '%Y-%m-%d') day2 = DateTime.strptime(args[1], '%Y-%m-%d') rescue ArgumentError abort(Status.specify_range) end toki, entries = init(options) range_data = toki.get_range(day1, day2, entries, options) clear view_range_table(args[0], args[1], range_data) export(:json, range_data, "toki-range-#{args[0]}_#{args[1]}") if options[:json] end private def export(type, data, filename) case type when :json name = Dir.home + "/temp/toki/#{filename}.json" File.write(name, data.to_h.to_json) puts "File converted and exported to #{name}\n\n" end end def view_range_table(start, to, range_data) puts TokiCLI::View.new.range_table(start, to, range_data) puts "\n" end def view_day_table(date, day_data) puts TokiCLI::View.new.day_table(date, day_data) puts "\n" end def view_app_table(asked, app_data) puts TokiCLI::View.new.app_table(asked, app_data) puts "\n" end def view_hits_table(hits) puts TokiCLI::View.new.hits_table(hits.reverse) puts "\n" end def init(options) toki = create_toki(options) return toki, toki.get_content(options) end def create_toki(options) if options[:adn] total_adn else total_db end end def total_adn clear puts Status.get_all init_toki_adn end def total_db clear #puts "getting data from db\n" init_toki_db end def get_total(toki, options) begin apps = get_all(toki, options) rescue Interrupt puts Status.canceled exit rescue => e puts Status.error(e) exit end apps end def init_toki_db TokiCLI::Toki.new(nil, nil) end def init_toki_adn(channel_id = get_channel_id) TokiCLI::Toki.new("#{get_token}", channel_id) end def save_token(token) File.write(Dir.home + '/.toki_token', token) end def get_token if File.exist?(Dir.home + '/.toki_token') File.read(Dir.home + '/.toki_token').chomp else abort Status.run_auth end end def get_channel_id #TODO return ch['counts']['messages'] too channels = ADNChannels::GetChannels.new(get_token).get_channels channel_id = '' channels.each do |ch| if ch['type'] == 'us.kkob.toki.sync-b' channel_id = ch['id'] break end end return channel_id.to_i unless channel_id == '' puts Status.no_channel exit end def clear puts "\e[H\e[2J" end def get_all(t, options) t.all_data(t.get_content(options)) end end end