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/uploads.rb,
lib/draftking/cli/commands/accounts.rb,
lib/draftking/cli/commands/_template.rb,
lib/draftking/cli/commands/autoposter.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



48
49
50
51
52
53
# File 'lib/draftking/cli.rb', line 48

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

#autoposterObject



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

def autoposter
  configured?
  opts = process_options(options)
  dk = get_dk_instance(opts)
  dk.auto_poster(opts)
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



40
41
42
43
44
# File 'lib/draftking/cli.rb', line 40

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



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

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



57
58
59
60
61
62
# File 'lib/draftking/cli.rb', line 57

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



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

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

#setupObject



26
27
28
# File 'lib/draftking/cli.rb', line 26

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



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

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

#tagObject



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

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

#uploadsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/draftking/cli/commands/uploads.rb', line 38

def uploads
  configured?
  opts   = process_options(options)
  dk     = get_dk_instance(opts)
  dfile  = opts[:file].chomp.strip

  File.open(dfile, 'r') do |data_file|
    mod = 0
    rows = []
    caption = ''
    row   = Struct.new(:count, :line, :file, :caption, :status)
    title = ups_title(dfile, dk)
    data_file.each_line.with_index do |line, idx|
      line = line.chomp.strip
      next if line.empty? || is_commented?(line)
      (caption = nil) || next if is_url_group?(line)
      (caption = line) && next if is_caption?(line, caption)
      ups_progress(mod, caption) unless dk.mute
      post_opts = ups_opts(line, caption, dk, opts)
      status    = ups_photo_draft(dk, post_opts)
      rows << row.new(mod += 1, idx + 1, File.basename(line), caption, status)
    end # each_line
    ups_report(title, dk, rows)
  end # of data_file
end

#versionObject



32
33
34
35
36
# File 'lib/draftking/cli.rb', line 32

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