Class: TokiCLI::App

Inherits:
Thor
  • Object
show all
Defined in:
lib/TokiCLI.rb

Instance Method Summary collapse

Instance Method Details

#activityObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/TokiCLI.rb', line 124

def activity
  init()
  if options.since?
    @toki.log_since(options.since)
  elsif options.day?
    @toki.log_day(options.day)
  else
    @toki.log_since()
  end
  @view.log_activity(@toki.response)
end

#app(*app_name) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/TokiCLI.rb', line 164

def app(*app_name)
  init()
  abort(Status.please_scan) if @toki.bundles.nil?
  candidates = @fileops.get_bundle_from_name(app_name)
  candidates.each.with_index(1) do |bundle_id, index|
    say "\nApp N°#{'%.2d' % index}: #{bundle_id}" if candidates.length > 1
    bundle_log(bundle_id, options)
    if JSON.parse(@toki.response)['meta']['code'] != 200
      say Status.no_data
    else
      if options[:json] || options[:csv]
        export(@toki, options, bundle_id)
      elsif options[:since] || options[:before] || options[:day] || options[:range]
        @view.log(@toki.response, bundle_title(bundle_id, options))
      else
        @view.log_total(@toki.response, bundle_title(bundle_id, options))
      end
    end
  end
end

#before(*args) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/TokiCLI.rb', line 110

def before(*args)
  init()
  @toki.apps_before(args[0])
  exit_with_msg_if_invalid_response()
  if options[:json] || options[:csv]
    export(@toki, options)
  else
    @view.apps(@toki.response, "Toki - All apps used before #{args[0]}")
  end
end

#bundle(bundle_id) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/TokiCLI.rb', line 143

def bundle(bundle_id)
  init()
  title = bundle_title(bundle_id, options)
  bundle_log(bundle_id, options)
  exit_with_msg_if_invalid_response(Status.no_data)
  if options[:json] || options[:csv]
    export(@toki, options)
  elsif options[:since] || options[:before] || options[:day] || options[:range]
    @view.log(@toki.response, title)
  else
    @view.log_total(@toki.response, title)
  end
end

#day(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/TokiCLI.rb', line 68

def day(*args)
  init()
  @toki.apps_day(args[0])
  exit_with_msg_if_invalid_response()
  if options[:json] || options[:csv]
    export(@toki, options)
  else
    @view.apps(@toki.response, "Toki - All apps used on #{args[0]}")
  end
end

#delete(bundle_id) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/TokiCLI.rb', line 208

def delete(bundle_id)
  options['no-backup'] ? init() : init(true)
  name = @toki.bundles[bundle_id]
  confirm_delete(bundle_id, name)
  say "\nDeleting entries..."
  @toki.delete_bundle(bundle_id)
  exit_with_msg_if_invalid_response(Status.wtf)
  say "\nDone.\n"
end

#range(*args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/TokiCLI.rb', line 82

def range(*args)
  init()
  @toki.apps_range(args[0], args[1])
  exit_with_msg_if_invalid_response()
  if options[:json] || options[:csv]
    export(@toki, options)
  else
    @view.apps(@toki.response, "Toki - All apps used between #{args[0]} and #{args[1]}")
  end
end

#restoreObject



186
187
188
189
190
# File 'lib/TokiCLI.rb', line 186

def restore
  init(true)
  adn = ADNImport.new(@fileops.data_path)
  adn.restore
end

#scanObject



19
20
21
22
23
24
25
# File 'lib/TokiCLI.rb', line 19

def scan
  puts Status.scanning
  fileops = FileOps.new
  fileops.save_bundles
  puts Status.file_saved(fileops.bundles_file)
  puts Status.next_launch_with_names
end

#serveObject



194
195
196
197
198
199
200
201
202
# File 'lib/TokiCLI.rb', line 194

def serve
  require_relative '../lib/TokiServer/tokiserver'
  puts "\n\n"
  say_status :starting, "Toki API server"
  say "\n\nPress [CTRL-C] to stop.\n\nThe index page URL is: http://localhost:4567\n\n"
  TokiServer.run!
  puts "\n\n"
  say_status :halt, "Toki API server"
end

#since(*args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/TokiCLI.rb', line 96

def since(*args)
  init()
  @toki.apps_since(args[0])
  exit_with_msg_if_invalid_response()
  if options[:json] || options[:csv]
    export(@toki, options)
  else
    @view.apps(@toki.response, "Toki - All apps used since #{args[0]}")
  end
end

#topObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/TokiCLI.rb', line 54

def top
  init()
  max = options[:number] || 5
  @toki.apps_top(max)
  if options[:json] || options[:csv]
    export(@toki, options)
  else
    @view.apps(@toki.response, "Toki - Total usage of most used apps")
  end
end

#totalObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/TokiCLI.rb', line 30

def total
  # Initializes files and instances
  ## Replace with init(true) to backup the db before using it
  ## Not necessary for read-only commands like this one
  init()
  # Gets the JSON response from TokiAPI
  ## Here the response is stocked locally in 'apps' but using a variable is optional:
  ## the response is memoized in @toki.response when a @toki method is called
  ## (see other commands)
  apps = @toki.apps_total()
  # Export or display
  if options[:json] || options[:csv]
    export(@toki, options)
  else
    # Title is optional: @view.apps_total(apps)
    title = "Toki - Total usage of all apps"
    @view.apps_total(apps, title)
  end
end

#versionObject



14
15
16
# File 'lib/TokiCLI.rb', line 14

def version
  View.new.version
end