Class: DK::CLI

Inherits:
Thor
  • Object
show all
Includes:
CliHelpers
Defined in:
lib/draftking/cli.rb,
lib/draftking/cli/commands/tag.rb,
lib/draftking/cli/commands/blogs.rb,
lib/draftking/cli/commands/strip.rb,
lib/draftking/cli/commands/status.rb,
lib/draftking/cli/commands/comment.rb,
lib/draftking/cli/commands/console.rb,
lib/draftking/cli/commands/accounts.rb,
lib/draftking/cli/commands/_template.rb,
lib/draftking/cli/commands/movedrafts.rb

Overview

Command Line Interface

Constant Summary

Constants included from CliHelpers

DK::CliHelpers::VALID_OPTS

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *_args) ⇒ Object

Try to execute unrecognized command as User Command



46
47
48
49
50
51
# File 'lib/draftking/cli.rb', line 46

def method_missing(method, *_args)
  name, attribs = DK::Config.new.user_commands.select { |k, _v| k == method.to_s }.first
  puts "Command '#{method}' not found." && return unless name && attribs
  attribs[:name] = name
  DK::UserCommand.new(attribs).exec!
end

Class Method Details

.launch_consoleObject

Launch IRB with tumblr_draftking loaded as $dk



10
11
12
13
14
15
16
# File 'lib/draftking/cli/commands/console.rb', line 10

def self.launch_console
  require 'irb'
  require 'irb/completion'
  ARGV.clear
  $dk = DK::Client.new simulate: true
  IRB.start
end

Instance Method Details

#accountsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/draftking/cli/commands/accounts.rb', line 7

def accounts
  files = DK::Config.available_configs
  show_accounts(files)
  return if options.empty? # Empty = no action to take
  puts accounts_input_dialogue(options)
  opts = process_options(options)
  choice = config_to_num(opts[:config]) || DK::Config.get_input
  return if /[^0-9]/ =~ choice
  file = files[choice.to_i]
  return if file.nil?
  msg = accounts_action(file, opts)
  show_accounts(DK::Config.available_configs, msg)
end

#blogsObject



6
7
8
9
# File 'lib/draftking/cli/commands/blogs.rb', line 6

def blogs
  configured?
  self.class.blogs_print_list(get_dk_instance(process_options(options)))
end

#check_for_updatesObject



38
39
40
41
42
# File 'lib/draftking/cli.rb', line 38

def check_for_updates
  versions = open('https://rubygems.org/api/v1/versions/tumblr_draftking.json').read
  latest   = JSON.parse(versions, object_class: OpenStruct).first.number
  puts "\n* UPDATE *\n\tDraftKing for Tumblr v#{latest} now available!\n\n" if latest != DK::VERSION
end

#comment(comm) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/draftking/cli/commands/comment.rb', line 26

def comment(comm)
  configured?
  opts = process_options(options)
  opts[:comment] = comm
  dk = get_dk_instance(opts)
  dk.comment_posts(opts)
end

#consoleObject



4
5
6
7
# File 'lib/draftking/cli/commands/console.rb', line 4

def console
  configured?
  self.class.launch_console
end

#customObject



55
56
57
58
59
60
# File 'lib/draftking/cli.rb', line 55

def custom
  title    = 'User Commands'
  commands = DK::Config.new.config.user_commands.map { |n, d| UserCommand.new d.merge(name: n) }
  headers  = %w(name command description config_name)
  Reporter.new(title: title, objects: commands, fields: headers).show
end

#movedraftsObject



25
26
27
28
29
30
# File 'lib/draftking/cli/commands/movedrafts.rb', line 25

def movedrafts
  configured?
  opts   = process_options(options)
  dk     = get_dk_instance(opts)
  dk.drafts_to_queue(opts)
end

#setupObject



24
25
26
# File 'lib/draftking/cli.rb', line 24

def setup
  DK::Config.setup
end

#status(blog = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/draftking/cli/commands/status.rb', line 5

def status(blog = nil)
  configured?
  title  = 'Status Report'
  fields = %w(Blog Drafts Queued Q.Space)
  opts = process_options(options.dup.merge(blog: blog))
  dk   = get_dk_instance(opts)
  rows = build_rows(dk)
  report = Reporter.new(title: title, rows: rows, headers: fields)
  report.show unless dk.simulate
  report
end

#stripObject



12
13
14
15
16
17
# File 'lib/draftking/cli/commands/strip.rb', line 12

def strip
  configured?
  opts = process_options(options)
  dk = get_dk_instance(opts)
  dk.strip_old_comments(opts)
end

#tagObject



15
16
17
18
19
# File 'lib/draftking/cli/commands/tag.rb', line 15

def tag
  opts = process_options(options)
  dk = get_dk_instance(opts)
  dk.tag_posts(opts)
end

#versionObject



30
31
32
33
34
# File 'lib/draftking/cli.rb', line 30

def version
  vstr = "tumblr_draftking #{DK::VERSION}"
  puts vstr unless options[:simulate]
  vstr
end