class DK::CLI
Command Line Interface
Public Class Methods
launch_console()
click to toggle source
Launch IRB with tumblr_draftking loaded as $dk
# 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
Private Class Methods
blogs_print_list(dk)
click to toggle source
Print blog list @param dk [DK::Client] Instance of tumblr_draftking
# File lib/draftking/cli/commands/blogs.rb, line 15 def self.blogs_print_list(dk) title = 'Blogs' fields = %w(# blog_name) rows = [] dk.user.blogs.each_with_index { |blog, idx| rows << [idx + 1, blog.name] } report = Reporter.new(title: title, rows: rows, headers: fields) report.show unless dk.simulate report end
Public Instance Methods
accounts()
click to toggle source
# 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
autoposter()
click to toggle source
# 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
blogs()
click to toggle source
# 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_updates()
click to toggle source
# 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)
click to toggle source
# 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
console()
click to toggle source
# File lib/draftking/cli/commands/console.rb, line 4 def console configured? self.class.launch_console end
custom()
click to toggle source
# 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
method_missing(method, *_args)
click to toggle source
Try to execute unrecognized command as User Command
# 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
movedrafts()
click to toggle source
# 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
setup()
click to toggle source
# File lib/draftking/cli.rb, line 26 def setup DK::Config.setup end
status(blog = nil)
click to toggle source
# 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
strip()
click to toggle source
# 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
tag()
click to toggle source
# 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
uploads()
click to toggle source
# 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
version()
click to toggle source
# File lib/draftking/cli.rb, line 32 def version vstr = "tumblr_draftking #{DK::VERSION}" puts vstr unless options[:simulate] vstr end
Private Instance Methods
accounts_action(filename, options)
click to toggle source
# File lib/draftking/cli/commands/accounts.rb, line 44 def accounts_action(filename, options) msg = accounts_delete(filename) if options[:delete] msg = accounts_switch(filename) if options[:switch] puts "#{msg}\n\n" if msg msg end
accounts_delete(file)
click to toggle source
# File lib/draftking/cli/commands/accounts.rb, line 55 def accounts_delete(file) account = accounts_extract_name(file) DK::Config.delete_config(file) ? "Deleted account: #{account}." : "Failed to delete account: #{account}!" end
accounts_extract_name(filename)
click to toggle source
# File lib/draftking/cli/commands/accounts.rb, line 51 def accounts_extract_name(filename) filename.gsub(/(^\.)|(.dkconfig)/, '') end
accounts_input_dialogue(options)
click to toggle source
# File lib/draftking/cli/commands/accounts.rb, line 38 def accounts_input_dialogue(options) return if options[:config] return "\nEnter # to use as DEFAULT ('x' to exit): " if options[:switch] return "\nEnter # to DELETE ('x' to exit): " if options[:delete] end
accounts_switch(file)
click to toggle source
# File lib/draftking/cli/commands/accounts.rb, line 60 def accounts_switch(file) return 'No change made.' if file == DK::CONFIG_FILENAME account = accounts_extract_name(file) DK::Config.switch_default_config(file) ? "New default account: #{account}." : 'Unable to change default account!' end
build_rows(dk)
click to toggle source
# File lib/draftking/cli/commands/status.rb, line 19 def build_rows(dk) dk.user.blogs.map do |b| next unless blog.nil? || b.name == blog [b.name, b.drafts, b.queue, 300 - b.queue.to_i] end.compact rescue [] end
is_caption?(line, caption)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 72 def is_caption?(line, caption) caption.nil? && (!is_file?(line) || !is_url?(line)) end
is_commented?(line)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 76 def is_commented?(line) line.start_with? '#' end
is_file?(line)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 80 def is_file?(line) line.start_with?('/') end
is_url?(line)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 84 def is_url?(line) line.start_with?('http') end
is_url_group?(line)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 68 def is_url_group?(line) line.start_with?('-') end
show_accounts(account_list, _msg = nil)
click to toggle source
Show available accounts @param account_list [[String]] List of configuration file names
# File lib/draftking/cli/commands/accounts.rb, line 25 def show_accounts(account_list, _msg = nil) title = 'Accounts' fields = %w(# name default file) rows = [] account_list.each_with_index do |config, idx| file = DK::Config.home_path_file(config) conf = DK::Config.new(file: file) default = ' (X)' if conf.is_default? rows << [idx, conf.config_name, default, file] end Reporter.new(title: title, rows: rows, headers: fields).show end
ups_opts(line, caption, dk, opts)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 94 def ups_opts(line, caption, dk, opts) post_opts = { caption: caption, state: dk.state, tags: comment_to_tags(caption) } is_url?(line) ? (post_opts[:source] = line) : (post_opts[:data] = [line]) post_opts[:tags] += ",#{dk.tags}" unless dk.tags.nil? post_opts[:link] = opts[:link] unless opts[:link].nil? post_opts end
ups_photo_draft(dk, post_opts)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 88 def ups_photo_draft(dk, post_opts) result = { 'id' => 'success' } result = dk.client.photo(dk.blog_name, post_opts) unless dk.simulate result['id'] ? '√' : result['errors'].first end
ups_progress(mod, caption)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 124 def ups_progress(mod, caption) msg = "Current group: #{caption} • " show_progress(current: mod, total: mod, message: msg) end
ups_report(title, dk, rows)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 106 def ups_report(title, dk, rows) r = DK::Reporter.new( title: title, fields: DK::UPLOAD_FIELDS, simulate: dk.simulate, objects: rows ) print ' ' * 80 + "\r\n" # erase_line puts "\n#{r}\n" unless dk.mute end
ups_title(file, dk)
click to toggle source
# File lib/draftking/cli/commands/uploads.rb, line 117 def ups_title(file, dk) "DK Batch Uploader\n" \ "Input: #{file}\n" \ "Target: #{dk.blog_name} [ #{dk.state.capitalize} ]\n" \ "#{current_date_string}" end