class WunderMarkdown::CLI

Attributes

args[R]
command[R]
options[R]

Public Class Methods

run(args) click to toggle source
# File lib/wunder_markdown/cli.rb, line 9
def self.run(args)
  new.call(args)
# rescue StandardError
#   $stderr.puts 'Woops, Something went wrong.'
#   exit 1
end

Public Instance Methods

call(args) click to toggle source
# File lib/wunder_markdown/cli.rb, line 16
def call(args)
  args, options = preparse(args)
  command = args.shift
  public_send(command, args, options)
end
client() click to toggle source
# File lib/wunder_markdown/cli.rb, line 71
def client
  @client ||= WunderMarkdown::Client.new(token)
end
config(args, options) click to toggle source
# File lib/wunder_markdown/cli.rb, line 38
def config(args, options)
  $stdout.puts 'Wunderlist Credentials: We will not store your password'
  email = options[:email]
  if ! email
    $stdout.puts 'email:'
    email = $stdin.gets.chomp
  end
  $stdout.puts 'Password:'
  system 'stty -echo'
  password = $stdin.gets.chomp
  system 'stty echo'
  WunderMarkdown::Auth.new.save(*client.login(email, password))
end
dump(args, options) click to toggle source
# File lib/wunder_markdown/cli.rb, line 52
def dump(args, options)
  list_name = args.shift
  unless list_name
    $stderr.puts 'Usage: wundermarkdown dump <list_name>'
    exit 1
  end
  list = client.list(list_name)
  list.tasks = group_tasks(client.tasks(list))
  $stdout.puts list.to_markdown
end
group_tasks(tasks) click to toggle source
# File lib/wunder_markdown/cli.rb, line 63
def group_tasks(tasks)
  root_tasks = tasks.select { |task| task.root? }
  root_tasks.map do |task|
    task.children = tasks.select { |t| t.parent_id == task.id }
    task
  end
end
preparse(args) click to toggle source
# File lib/wunder_markdown/cli.rb, line 22
def preparse(args)
  options = OpenStruct.new
  opt_parse = OptionParser.new do |opts|
    opts.banner = "Usage: wundermarkdown <command> <args> [options]"
    opts.on("-e EMAIL", "--email EMAIL", "Email address") do |email|
      options.email = email
    end
  end
  opt_parse.parse!(args)
  unless args.count >= 1
    $stderr.puts opt_parse
    exit 1
  end
  [args, options]
end
token() click to toggle source
# File lib/wunder_markdown/cli.rb, line 75
def token
  @token ||= WunderMarkdown::Auth.new.get
end