module HabiticaCli::Commands
responsible for adding new todos, tasks, and dailies also for for caching the newly added todo
Clear completed tasks to slim down output
Responsible for completing tasks
Responsible for listing tasks
Functionality common to all commands
Responsible for displaying a “dashboard” of stats and tasks for a user as well as caching them
Public Class Methods
add(env, type, text)
click to toggle source
# File lib/habitica_cli/commands/add.rb, line 5 def self.add(env, type, text) validate_type(type) response = env.api.post('tasks/user', type: type, text: text) if response.success? task = cache_tasks(env, [response.body['data']], type).first puts "Added #{task['text']} [#{task['cid']}]" else puts "Error adding #{text}: #{response.body}" end end
cache_tasks(env, tasks, type)
click to toggle source
# File lib/habitica_cli/commands/shared.rb, line 23 def self.cache_tasks(env, tasks, type) env.cache.store_tasks( select_attributes(filter_tasks(env, tasks, type)) ) end
clear(env)
click to toggle source
# File lib/habitica_cli/commands/clear.rb, line 5 def self.clear(env) env.api.post('tasks/clear-completed') if response.success? puts 'Tasks cleared' else puts "Error #{response.body}" end end
display(env, body, type)
click to toggle source
# File lib/habitica_cli/commands/shared.rb, line 29 def self.display(env, body, type) raw_tasks = body['data'] tasks = cache_tasks(env, raw_tasks, type) puts type.capitalize unless type.nil? tasks.each do |item| output = type.nil? ? "(#{item['type']}) " : '' output += "[#{item['cid']}] #{item['text']}" puts output end end
do(env, cache_ids)
click to toggle source
# File lib/habitica_cli/commands/do.rb, line 4 def self.do(env, cache_ids) items = cache_ids.map { |id| env.cache.get(id) } items.each do |item| response = env.api.post("tasks/#{item['id']}/score/up") if response.success? puts "Completed: #{item['text']}" else puts "Error #{response.body}" end end end
filter_tasks(env, tasks, type = nil)
click to toggle source
# File lib/habitica_cli/commands/shared.rb, line 9 def self.filter_tasks(env, tasks, type = nil) tasks.select do |task| (type.nil? || task['type'] == type) && (env.options['show_completed'] == true || task['completed'] != true) end end
list(env, type = nil)
click to toggle source
# File lib/habitica_cli/commands/list.rb, line 4 def self.list(env, type = nil) validate_type(type) if type response = env.api.get('tasks/user') if response.success? display(env, response.body, type) else puts 'Error connecting to habit api' end end
select_attributes(tasks)
click to toggle source
# File lib/habitica_cli/commands/shared.rb, line 16 def self.select_attributes(tasks) keys = %w(completed id text type) tasks.map do |task| task.select { |k, _| keys.include?(k) } end end
status(env)
click to toggle source
# File lib/habitica_cli/commands/status.rb, line 5 def self.status(env) get_user_tasks(env) get_user_info(env) end
status_display(data)
click to toggle source
# File lib/habitica_cli/commands/status.rb, line 10 def self.status_display(data) stats = data['stats'] puts [ "Gold: #{stats['gp'].round}", "Health: #{stats['hp'].round}/#{stats['maxHealth']}" ].join(' | ') end
validate_type(type)
click to toggle source
# File lib/habitica_cli/commands/shared.rb, line 4 def self.validate_type(type) types = %w(todo habit daily) fail "Not a valid type (#{types})" unless types.include?(type) end
Private Class Methods
get_user_info(env)
click to toggle source
# File lib/habitica_cli/commands/status.rb, line 20 def self.get_user_info(env) user_response = env.api.get('user') status_display(user_response.body['data']) end
get_user_tasks(env)
click to toggle source
# File lib/habitica_cli/commands/status.rb, line 25 def self.get_user_tasks(env) task_response = env.api.get('tasks/user', type: 'dailys') display(env, task_response.body, nil) end