Class: Redd::Assist::DeleteBadlyScoring

Inherits:
Object
  • Object
show all
Defined in:
lib/redd/assist/delete_badly_scoring.rb

Overview

Note:

Works with Submission and Comment.

Helper class for deleting negative links or comments. You can run this as a separate process, as long as you set your rate limit time higher in your main process.

Examples:

assist = Redd::Assist::DeleteBadlyScoring.new(session.client)
assist.track(comment)
loop do
  assist.delete_badly_scoring(under_score: 1, minimum_age: 600)
  sleep 30
end

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ DeleteBadlyScoring

Create a DeleteBadlyScoring assist.

Parameters:


18
19
20
21
# File 'lib/redd/assist/delete_badly_scoring.rb', line 18

def initialize(client)
  @client = client
  @queue = []
end

Instance Method Details

#delete_badly_scoring!(under_score: 1, minimum_age: 15 * 60) ⇒ Array<String>

Delete all items that are older than the minimum age and score 0 or below.

Parameters:

  • under_score (Integer) (defaults to: 1)

    the maximum score that the comment must have to be kept

  • minimum_age (Integer) (defaults to: 15 * 60)

    the minimum age for deletion (seconds)

Returns:

  • (Array<String>)

    the deleted item fullnames


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/redd/assist/delete_badly_scoring.rb', line 32

def delete_badly_scoring!(under_score: 1, minimum_age: 15 * 60)
  delete_if do |comment|
    if comment.created_at + minimum_age > Time.now
      :skip
    elsif comment.score < under_score && !comment.deleted? && !comment.archived?
      :delete
    else
      :keep
    end
  end
end

#delete_if {|comment| ... } ⇒ Array<String>

Delete all items that the block returns true for.

Parameters:

  • minimum_age (Integer)

    the minimum age for deletion

Yield Parameters:

  • comment (Comment)

    the comment to filter

Yield Returns:

  • (:keep, :delete, :skip)

    whether to keep, delete, or check again later

Returns:

  • (Array<String>)

    the deleted item fullnames


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/redd/assist/delete_badly_scoring.rb', line 49

def delete_if
  deleted = []
  @queue.delete_if do |fullname|
    comment = Models::Comment.new(@client, name: fullname).reload
    action = yield comment
    if action == :delete
      comment.delete
      deleted << fullname
    end
    action == :keep || action == :delete
  end
  deleted
end

#track(model) ⇒ Object

Add this model's id to the list of items that are tracked.


24
25
26
# File 'lib/redd/assist/delete_badly_scoring.rb', line 24

def track(model)
  @queue << model.name
end