Class: DK::Autofixer

Inherits:
Object
  • Object
show all
Defined in:
lib/autofixer/helpers.rb,
lib/autofixer/results.rb,
lib/autofixer/version.rb,
lib/autofixer/autofixer.rb,
lib/autofixer/data_store.rb,
lib/autofixer/commands/show_help.rb,
lib/autofixer/commands/show_config.rb,
lib/autofixer/commands/open_results.rb,
lib/autofixer/commands/show_version.rb,
lib/autofixer/commands/generate_tags_yml.rb

Defined Under Namespace

Classes: GenerateTagsYml

Constant Summary collapse

VERSION =
'0.1.2'
ERROR =
'**'
POSTS =
'/Users/meis/coding/rails/autofixer/db/test.posts'
CONFIGDIR =
'/config_md/taf/'
INFO =
'info'
WARN =
'warning'
ERRO =
'error'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Autofixer

Returns a new instance of Autofixer.



19
20
21
22
23
# File 'lib/autofixer/autofixer.rb', line 19

def initialize(opts = {})
  set_instance_vars(opts)
  installed_test_configuration?
  read_configuration
end

Instance Attribute Details

#clear=(value) ⇒ Object

Autofixer Processing Options



15
16
17
# File 'lib/autofixer/autofixer.rb', line 15

def clear=(value)
  @clear = value
end

#config_dirObject

Autofixer Required Structure



12
13
14
# File 'lib/autofixer/autofixer.rb', line 12

def config_dir
  @config_dir
end

#dkObject

DraftKing for Tumblr



10
11
12
# File 'lib/autofixer/autofixer.rb', line 10

def dk
  @dk
end

#hide_picsObject

Autofixer Result Options



16
17
18
# File 'lib/autofixer/autofixer.rb', line 16

def hide_pics
  @hide_pics
end

#last_tagObject

Autofixer Tagging Config



13
14
15
# File 'lib/autofixer/autofixer.rb', line 13

def last_tag
  @last_tag
end

#limitObject

DraftKing Settings



11
12
13
# File 'lib/autofixer/autofixer.rb', line 11

def limit
  @limit
end

#postfixObject

Autofixer Tagging Options



14
15
16
# File 'lib/autofixer/autofixer.rb', line 14

def postfix
  @postfix
end

#prefixObject

Autofixer Tagging Options



14
15
16
# File 'lib/autofixer/autofixer.rb', line 14

def prefix
  @prefix
end

#processedObject

Autofixer Results



17
18
19
# File 'lib/autofixer/autofixer.rb', line 17

def processed
  @processed
end

#reprocessObject

Autofixer Processing Options



15
16
17
# File 'lib/autofixer/autofixer.rb', line 15

def reprocess
  @reprocess
end

#reviewObject

Autofixer Results



17
18
19
# File 'lib/autofixer/autofixer.rb', line 17

def review
  @review
end

#show_resultsObject

Autofixer Result Options



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/autofixer/autofixer.rb', line 16

def show_results
  cli_header

  fname1 = confile('processed.html')
  fname2 = confile('review.html')
  fname3 = confile('updated.html')

  @file_index = [
    ["Review (#{@review.size})",          fname2],
    ["Updated (#{@updated.size})",        fname3],
    ["Preprocessed (#{@processed.size})", fname1]
  ]

  msg1  = "(#{@processed.size}) Drafts are ready to be queued."
  msg2  = "(#{@review.size}) Drafts need visual review."
  msg3  = "(#{@updated.size}) Drafts were Autofixed."


  # Already Processed
  @processed.sort_by!{ |x| x.id }
  generate_review_webpage(@processed, fname1, msg1)

  # Need Review
  @review.sort_by! do |x|
    bname = bfrom(x)
    [bname, x.id]
  end
  generate_review_webpage(@review, fname2, msg2)

  # Updated
  @updated.sort_by!{ |x| bfrom(x)}
  generate_review_webpage(@updated, fname3, msg3)

  puts
  puts
  `open #{@file_index.first.last}` if @show_results
end

#simulateObject

DraftKing Settings



11
12
13
# File 'lib/autofixer/autofixer.rb', line 11

def simulate
  @simulate
end

#spliterObject

Autofixer Tagging Options



14
15
16
# File 'lib/autofixer/autofixer.rb', line 14

def spliter
  @spliter
end

#summaryObject

Autofixer Tagging Config



13
14
15
# File 'lib/autofixer/autofixer.rb', line 13

def summary
  @summary
end

#tag_idxObject

Autofixer Tagging Config



13
14
15
# File 'lib/autofixer/autofixer.rb', line 13

def tag_idx
  @tag_idx
end

#updatedObject

Autofixer Results



17
18
19
# File 'lib/autofixer/autofixer.rb', line 17

def updated
  @updated
end

#use_testObject

Autofixer Processing Options



15
16
17
# File 'lib/autofixer/autofixer.rb', line 15

def use_test
  @use_test
end

Instance Method Details

#apply_tag_matcher_to(posts) ⇒ Object

Mass processing based on whitelisted tags return: [Array] Unmatched Posts



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/autofixer/autofixer.rb', line 97

def apply_tag_matcher_to(posts)
  return posts unless @gtags
  return posts if @gtags.empty?
  not_matched = []
  while (post = posts.shift)
    next if post.tags.empty?
    new_comment = tag_matcher(post.tags)
    if new_comment.eql?(ERROR)
      not_matched << post
      next
    end
    success = update_post_comment(post, new_comment)
    (success ? @updated : not_matched) << post
  end
  not_matched
end

#capitalize(s) ⇒ Object



8
9
10
11
12
13
# File 'lib/autofixer/helpers.rb', line 8

def capitalize(s)
  return if s.nil?
  res = s.gsub(/\d/,'').split(' ').map(&:strip).map(&:capitalize)
  return res[0] if res.size < 2
  res.join(' ')
end

#generate_tags_ymlObject

Command



4
5
6
7
8
# File 'lib/autofixer/commands/generate_tags_yml.rb', line 4

def generate_tags_yml
  return unless @options.include?('g:tags')
  GenerateTagsYml.new(ARGV)
  exit(0)
end

#get_filtered_postsObject

Load data and filter out processed or seemingly unprocessible posts



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/autofixer/autofixer.rb', line 48

def get_filtered_posts
  drafts = @usetestdata ? load_test_data : @dk.get_posts
  @total = drafts.size
  drafts.map do |draft|
    draft = DK::Post.new draft
    next if filter_processed? draft
    next if filter_by_info?   draft
    next if filter_by_trail?  draft
    draft
  end.compact
end


15
16
17
# File 'lib/autofixer/helpers.rb', line 15

def link_to_edit(id)
  "https://www.tumblr.com/edit/#{id}"
end

#normalize(tag, from = '') ⇒ Object



3
4
5
6
# File 'lib/autofixer/helpers.rb', line 3

def normalize(tag, from='')
  return ERROR if tag.nil?
  affix(capitalize(tag))
end

#open_resultsObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/autofixer/commands/open_results.rb', line 3

def open_results
  return unless @options.include?('open')
  file = confile('updated.html')
  `open #{file}` && exit(0) if File.exist?(file)
  puts
  puts 'Error:'
  puts '  No results have been generated yet.'
  puts
  exit(0)
end

#pad(value, reference, prefix = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/autofixer/helpers.rb', line 19

def pad(value, reference, prefix=nil)
  s = value.to_s
  r = reference.to_s.length
  if prefix
    s = ' ' + s while (s.length < r)
  else
    s += ' ' while (s.length < r)
  end
  s
end

#perform_command?Boolean

Execute Command

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/autofixer/autofixer.rb', line 35

def perform_command?
  show_help
  show_config
  show_version
  open_results
  generate_tags_yml
  if @options.first && !@options.first[0].eql?('-')
    puts "\nCommand '#{@options.first}' not found.\n\n"
    exit(1)
  end
end

#process(posts) ⇒ Object

Construct new post comments based on available configurations



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/autofixer/autofixer.rb', line 61

def process(posts)
  posts.each_with_index do |post, idx|
    tags    = post.tags
    blog    = post.trail.first.blog.name
    summary = post.summary

    next if ignore? blog, post
    # Scan for configured tag-index
    comment = use_tag_index blog, post
    # Use coded configuration
    comment ||= use_commands blog, summary
    # Use full summary text
    comment ||= use_summary blog, summary
    # Use last tag
    comment ||= use_tag_index blog, post, @last_tag.include?(blog)
    # No automated processing
    comment ||= ERROR

    success = update_post_comment(post, comment)
    (success ? @updated : @review) << post
  end
end

#runObject

Automated Processing



26
27
28
29
30
31
32
# File 'lib/autofixer/autofixer.rb', line 26

def run
  perform_command? # Commands will exit(#)
  process(get_filtered_posts)
  @review = apply_tag_matcher_to(@review)
  @review = clear(@review)
  show_results
end

#show_configObject



3
4
5
6
7
# File 'lib/autofixer/commands/show_config.rb', line 3

def show_config
  return unless @options.include?('config')
  `open #{@config_dir + 'view_config.html'}`
  exit(0)
end

#show_helpObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/autofixer/commands/show_help.rb', line 3

def show_help
  return unless (@options.include?('help') || @options.find_index('-h'))
  puts 'Usage: '
  puts '  $ taf [options*]'
  puts '  $ taf <command> [command-options*]'
  puts
  puts ' Commands:'
  puts '    help    Show this menu.'
  puts '    open    Open a webpage with the latest taf results.'
  puts '  g:tags    Generate a list of tags from your latest posts to serve as a whitelist for the Tag Matcher.'
  puts
  puts ' Options:'
  puts '    -s             Simulate Run (no changes saved)'
  puts '    -p [STRING]    Prefix for generated comments'
  puts '    -P [STRING]    Prefix for generated comments'
  puts '    -S [STRING]    Separator used between prefix/postfix and generated comment text.'
  puts '    -l [INTEGER]   Number of Drafts to select for processing.'
  puts '    --clear        Clear unprocessible Drafts by adding the given prefix (-p [STRING])'
  puts '                    causing these posts to show as "Already Processed" in future runs.'
  puts '    --show         Open the results webpage after processing is complete.'
  puts
  puts ' Command Options:'
  puts '   g:tags'
  puts '     -l [INTEGER]         Number of Drafts to select for processing.'
  puts '     -b [STRING]          Blog name, only needed if targeting secondary blog.'
  puts '     --source [STRING]    draft | queue | published '
  puts
  exit(0)
end

#show_versionObject



3
4
5
6
7
8
# File 'lib/autofixer/commands/show_version.rb', line 3

def show_version
  return unless @options.include?('-v') || @options.include?('--version')
  puts "\ntumblr_autofixer v#{VERSION}"
  puts
  exit(0)
end

#tag_matcher(tags) ⇒ Object

Contruct a comment using any whitelisted tags return: [String] Normalized Comment



86
87
88
89
90
91
92
93
# File 'lib/autofixer/autofixer.rb', line 86

def tag_matcher(tags)
  return ERROR if tags.empty?
  joiner = ' | '
  matches = tags.select{ |tag| @gtags.include? tag.downcase }
  return ERROR if matches.empty?
  new_comment = matches.join(joiner)
  normalize(new_comment)
end