lib/TokiCLI/app.rb in TokiCLI-0.0.4 vs lib/TokiCLI/app.rb in TokiCLI-0.0.5

- old
+ new

@@ -1,10 +1,10 @@ # 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}"} + %w{get_messages search_messages get_channels module status view authorize}.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' option :csv, aliases: '-c', type: :boolean, desc: 'Export the results as a CSV file' @@ -29,15 +29,16 @@ export(options, hits, "toki-top-#{Time.now.to_s[0..9]}") if (options[:json] || options[:csv]) 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 + begin + ADNAuthorize::Authorize.new.authorize + rescue Interrupt + puts Status.canceled + exit + end 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' @@ -110,15 +111,11 @@ toki = create_toki(options) return toki, toki.get_content(options) end def create_toki(options) - if options[:adn] - total_adn - else - total_db - end + options[:adn] ? total_adn : total_db end def total_adn clear puts Status.get_all @@ -131,54 +128,53 @@ init_toki_db end def get_total(toki, options) begin - apps = get_all(toki, options) + get_all(toki, options) rescue Interrupt - puts Status.canceled - exit + abort Status.canceled rescue => e - puts Status.error(e) - exit + abort Status.error(e) 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 + filepath = Dir.home + '/.TokiCLI/config.json' + if File.exist?(filepath) + content = JSON.parse(File.read(filepath)) + content['token'] else + clear 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 + abort Status.no_channel end + def clear puts "\e[H\e[2J" end + def get_all(t, options) t.all_data(t.get_content(options)) end end end