Class: WunderMarkdown::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/wunder_markdown/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/wunder_markdown/cli.rb', line 7

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/wunder_markdown/cli.rb', line 7

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/wunder_markdown/cli.rb', line 7

def options
  @options
end

Class Method Details

.run(args) ⇒ Object



9
10
11
12
13
14
# 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

Instance Method Details

#call(args) ⇒ Object



16
17
18
19
20
# File 'lib/wunder_markdown/cli.rb', line 16

def call(args)
  args, options = preparse(args)
  command = args.shift
  public_send(command, args, options)
end

#clientObject



71
72
73
# File 'lib/wunder_markdown/cli.rb', line 71

def client
  @client ||= WunderMarkdown::Client.new(token)
end

#config(args, options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# 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.(email, password))
end

#dump(args, options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



63
64
65
66
67
68
69
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

#tokenObject



75
76
77
# File 'lib/wunder_markdown/cli.rb', line 75

def token
  @token ||= WunderMarkdown::Auth.new.get
end